DatePicker.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder\UI\Elm\Components;
  13. use FormBuilder\Driver\FormComponent;
  14. use FormBuilder\Factory\Elm;
  15. /**
  16. * 日期选择器组件
  17. * Class DatePicker
  18. *
  19. * @method $this readonly(bool $readonly) 完全只读, 默认值: false
  20. * @method $this disabled(bool $disabled) 禁用, 默认值: false
  21. * @method $this editable(bool $editable) 文本框可输入, 默认值: true
  22. * @method $this clearable(bool $clearable) 是否显示清除按钮, 默认值: true
  23. * @method $this size(string $size) 输入框尺寸, 可选值: large, small, mini
  24. * @method $this placeholder(string $placeholder) 非范围选择时的占位内容
  25. * @method $this startPlaceholder(string $startPlaceholder) 范围选择时开始日期的占位内容
  26. * @method $this endPlaceholder(string $endPlaceholder) 范围选择时结束日期的占位内容
  27. * @method $this type(string $type) 显示类型, 可选值: year/month/date/dates/ week/datetime/datetimerange/ daterange/monthrange, 默认值: date
  28. * @method $this format(string $format) 显示在输入框中的格式, 可选值: 见日期格式, 默认值: yyyy-MM-dd
  29. * @method $this align(string $align) 对齐方式, 可选值: left, center, right, 默认值: left
  30. * @method $this popperClass(string $popperClass) DatePicker 下拉框的类名
  31. * @method $this pickerOptions(array $pickerOptions) 当前时间日期选择器特有的选项参考下表, 默认值: {
  32. * }
  33. * @method $this rangeSeparator(string $rangeSeparator) 选择范围时的分隔符, 默认值: '-'
  34. * @method $this defaultValue(string $defaultValue) 可选,选择器打开时默认显示的时间, 可选值: 可被new Date()解析
  35. * @method $this defaultTime(array $defaultTime) 范围选择时选中日期所使用的当日内具体时刻, 可选值: 数组,长度为 2,每项值为字符串,形如12:00:00,第一项指定开始日期的时刻,第二项指定结束日期的时刻,不指定会使用时刻 00:00:00
  36. * @method $this valueFormat(string $valueFormat) 可选,绑定值的格式。不指定则绑定值为 Date 对象, 可选值: 见日期格式
  37. * @method $this name(string $name) 原生属性
  38. * @method $this unlinkPanels(bool $unlinkPanels) 在范围选择器里取消两个日期面板之间的联动, 默认值: false
  39. * @method $this prefixIcon(string $prefixIcon) 自定义头部图标的类名, 默认值: el-icon-date
  40. * @method $this clearIcon(string $clearIcon) 自定义清空图标的类名, 默认值: el-icon-circle-close
  41. * @method $this validateEvent(bool $validateEvent) 输入时是否触发表单的校验, 默认值: true
  42. * @method $this timeArrowControl(bool $timeArrowControl) 是否使用箭头进行时间选择, 默认值: false
  43. *
  44. *
  45. */
  46. class DatePicker extends FormComponent
  47. {
  48. /**
  49. * 日期选择
  50. */
  51. const TYPE_DATE = 'date';
  52. const TYPE_DATES = 'dates';
  53. const TYPE_WEEK = 'week';
  54. /**
  55. * 日期区间选择
  56. */
  57. const TYPE_DATE_RANGE = 'daterange';
  58. const TYPE_MONTH_RANGE = 'monthrange';
  59. /**
  60. * 日期+时间选择
  61. */
  62. const TYPE_DATE_TIME = 'datetime';
  63. /**
  64. * 日期+时间区间选择
  65. */
  66. const TYPE_DATE_TIME_RANGE = 'datetimerange';
  67. /**
  68. * 年份选择
  69. */
  70. const TYPE_YEAR = 'year';
  71. /**
  72. * 月份选择
  73. */
  74. const TYPE_MONTH = 'month';
  75. protected $selectComponent = true;
  76. protected $defaultProps = [
  77. 'type' => self::TYPE_DATE,
  78. 'editable' => false
  79. ];
  80. protected static $propsRule = [
  81. 'readonly' => 'bool',
  82. 'disabled' => 'bool',
  83. 'editable' => 'bool',
  84. 'clearable' => 'bool',
  85. 'size' => 'string',
  86. 'placeholder' => 'string',
  87. 'startPlaceholder' => 'string',
  88. 'endPlaceholder' => 'string',
  89. 'timeArrowControl' => 'bool',
  90. 'type' => 'string',
  91. 'format' => 'string',
  92. 'align' => 'string',
  93. 'popperClass' => 'string',
  94. 'pickerOptions' => 'array',
  95. 'rangeSeparator' => 'string',
  96. 'defaultValue' => 'string',
  97. 'defaultTime' => 'array',
  98. 'valueFormat' => 'string',
  99. 'name' => 'string',
  100. 'unlinkPanels' => 'bool',
  101. 'prefixIcon' => 'string',
  102. 'clearIcon' => 'string',
  103. 'validateEvent' => 'bool',
  104. ];
  105. protected function isRange()
  106. {
  107. return in_array(strtolower($this->props['type']), ['datetimerange', 'daterange', 'monthrange']);
  108. }
  109. protected function isMultiple()
  110. {
  111. return isset($this->props['type']) && strtolower($this->props['type']) == 'dates';
  112. }
  113. public function createValidate()
  114. {
  115. if ($this->isRange() || $this->isMultiple())
  116. return Elm::validateArr();
  117. else
  118. return Elm::validateDate();
  119. }
  120. public function required($message = null)
  121. {
  122. if (is_null($message)) $message = $this->getPlaceHolder();
  123. $validate = $this->createValidate();
  124. if ($this->isRange()) {
  125. $dateRequired = Elm::validateStr()->message($message)->required();
  126. $validate->fields([
  127. '0' => $dateRequired,
  128. '1' => $dateRequired
  129. ]);
  130. }
  131. $this->appendValidate($validate->message($message)->required());
  132. return $this;
  133. }
  134. }