DatePicker.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder\components;
  8. use FormBuilder\FormComponentDriver;
  9. use FormBuilder\Helper;
  10. /**
  11. * 日期选择器组件
  12. * Class DatePicker
  13. *
  14. * @package FormBuilder\components
  15. * @method $this type(String $type) 显示类型,可选值为 date、daterange、datetime、datetimerange、year、month
  16. * @method $this format(String $format) 展示的日期格式, 默认为yyyy-MM-dd HH:mm:ss
  17. * @method $this placement(String $placement) 日期选择器出现的位置,可选值为top, top-start, top-end, bottom, bottom-start, bottom-end, left, left-start, left-end, right, right-start, right-end, 默认为bottom-start
  18. * @method $this placeholder(String $placeholder) 占位文本
  19. * @method $this confirm(Boolean $bool) 是否显示底部控制栏,开启后,选择完日期,选择器不会主动关闭,需用户确认后才可关闭, 默认为false
  20. * @method $this size(String $size) 尺寸,可选值为large、small、default或者不设置
  21. * @method $this disabled(Boolean $bool) 是否禁用选择器
  22. * @method $this clearable(Boolean $bool) 是否显示清除按钮
  23. * @method $this readonly(Boolean $bool) 完全只读,开启后不会弹出选择器,只在没有设置 open 属性下生效
  24. * @method $this editable(Boolean $bool) 文本框是否可以输入, 默认为false
  25. * @method $this transfer(Boolean $bool) 是否将弹层放置于 body 内,在 Tabs、带有 fixed 的 Table 列内使用时,建议添加此属性,它将不受父级样式影响,从而达到更好的效果, 默认为false
  26. * @method $this splitPanels(Boolean $bool) 开启后,左右面板不联动,仅在 daterange 和 datetimerange 下可用。
  27. * @method $this showWeekNumbers(Boolean $bool) 开启后,可以显示星期数。
  28. *
  29. */
  30. class DatePicker extends FormComponentDriver
  31. {
  32. /**
  33. * @var string
  34. */
  35. protected $name = 'datePicker';
  36. /**
  37. *
  38. */
  39. const TYPE_DATE = 'date';
  40. /**
  41. *
  42. */
  43. const TYPE_DATE_RANGE = 'daterange';
  44. /**
  45. *
  46. */
  47. const TYPE_DATE_TIME = 'datetime';
  48. /**
  49. *
  50. */
  51. const TYPE_DATE_TIME_RANGE = 'datetimerange';
  52. /**
  53. *
  54. */
  55. const TYPE_YEAR = 'year';
  56. /**
  57. *
  58. */
  59. const TYPE_MONTH = 'month';
  60. /**
  61. * @var array
  62. */
  63. protected $props = [
  64. 'type' => self::TYPE_DATE,
  65. 'editable' => false,
  66. 'multiple' => false
  67. ];
  68. /**
  69. * @var array
  70. */
  71. protected static $propsRule = [
  72. 'type' => 'string',
  73. 'format' => 'string',
  74. 'placement' => 'string',
  75. 'placeholder' => 'string',
  76. 'size' => 'string',
  77. 'confirm' => 'boolean',
  78. 'disabled' => 'boolean',
  79. 'clearable' => 'boolean',
  80. 'readonly' => 'boolean',
  81. 'editable' => 'boolean',
  82. 'transfer' => 'boolean',
  83. 'splitPanels' => 'boolean',
  84. 'showWeekNumbers' => 'boolean'
  85. ];
  86. /**
  87. *
  88. */
  89. protected function init()
  90. {
  91. $this->placeholder($this->getPlaceHolder());
  92. }
  93. /**
  94. * 开启后, 可以选择多个日期, 仅在 date 下可用, 默认为false
  95. *
  96. * @param bool $bool
  97. * @return $this
  98. */
  99. public function multiple($bool = true)
  100. {
  101. if ($this->props['type'] == 'date')
  102. $this->props['multiple'] = (bool)$bool;
  103. else
  104. $this->props['multiple'] = false;
  105. return $this;
  106. }
  107. /**
  108. * @param $value
  109. * @return $this
  110. */
  111. public function value($value)
  112. {
  113. if (is_array($value)) {
  114. foreach ($value as $k => $v) {
  115. $value[$k] = Helper::getDate($v);
  116. }
  117. } else {
  118. $value = Helper::getDate($value);
  119. }
  120. $this->value = $value;
  121. return $this;
  122. }
  123. public function getValidateHandler()
  124. {
  125. if (in_array($this->props['type'], ['datetimerange', 'daterange']) || $this->props['multiple'])
  126. return Validate::arr();
  127. else
  128. return Validate::date();
  129. }
  130. public function required($message = null)
  131. {
  132. $message = $message ?: $this->getPlaceHolder();
  133. if (in_array($this->props['type'], ['datetimerange', 'daterange'])) {
  134. $this->validate()->fields([
  135. '0' => ['required' => true, 'type' => 'date', 'message' => $message],
  136. '1' => ['required' => true, 'type' => 'date', 'message' => $message]
  137. ], true, $message);
  138. return $this;
  139. } else
  140. return parent::required($message);
  141. }
  142. /**
  143. * @return array
  144. */
  145. public function build()
  146. {
  147. return [
  148. 'type' => $this->name,
  149. 'field' => $this->field,
  150. 'title' => $this->title,
  151. 'value' => $this->value,
  152. 'props' => (object)$this->props,
  153. 'validate' => $this->validate,
  154. 'col' => $this->col
  155. ];
  156. }
  157. }