DatePicker.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. const TYPE_TIME = 'time';
  61. /**
  62. * @var array
  63. */
  64. protected $props = [
  65. 'type' => self::TYPE_DATE,
  66. 'editable' => false,
  67. 'multiple' => false
  68. ];
  69. /**
  70. * @var array
  71. */
  72. protected static $propsRule = [
  73. 'type' => 'string',
  74. 'format' => 'string',
  75. 'placement' => 'string',
  76. 'placeholder' => 'string',
  77. 'size' => 'string',
  78. 'confirm' => 'boolean',
  79. 'disabled' => 'boolean',
  80. 'clearable' => 'boolean',
  81. 'readonly' => 'boolean',
  82. 'editable' => 'boolean',
  83. 'transfer' => 'boolean',
  84. 'splitPanels' => 'boolean',
  85. 'showWeekNumbers' => 'boolean'
  86. ];
  87. /**
  88. *
  89. */
  90. protected function init()
  91. {
  92. $this->placeholder($this->getPlaceHolder());
  93. }
  94. /**
  95. * 开启后, 可以选择多个日期, 仅在 date 下可用, 默认为false
  96. *
  97. * @param bool $bool
  98. * @return $this
  99. */
  100. public function multiple($bool = true)
  101. {
  102. if ($this->props['type'] == 'date')
  103. $this->props['multiple'] = (bool)$bool;
  104. else
  105. $this->props['multiple'] = false;
  106. return $this;
  107. }
  108. /**
  109. * @param $value
  110. * @return $this
  111. */
  112. public function value($value)
  113. {
  114. if (is_array($value)) {
  115. foreach ($value as $k => $v) {
  116. $value[$k] = Helper::getDate($v);
  117. }
  118. } else {
  119. $value = Helper::getDate($value);
  120. }
  121. $this->value = $value;
  122. return $this;
  123. }
  124. public function getValidateHandler()
  125. {
  126. if (in_array($this->props['type'], ['datetimerange', 'daterange']) || $this->props['multiple'])
  127. return Validate::arr();
  128. else
  129. return Validate::date();
  130. }
  131. public function required($message = null)
  132. {
  133. $message = $message ?: $this->getPlaceHolder();
  134. if (in_array($this->props['type'], ['datetimerange', 'daterange'])) {
  135. $this->validate()->fields([
  136. '0' => ['required' => true, 'type' => 'date', 'message' => $message],
  137. '1' => ['required' => true, 'type' => 'date', 'message' => $message]
  138. ], true, $message);
  139. return $this;
  140. } else
  141. return parent::required($message);
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function build()
  147. {
  148. return [
  149. 'type' => $this->name,
  150. 'field' => $this->field,
  151. 'title' => $this->title,
  152. 'value' => $this->value,
  153. 'props' => (object)$this->props,
  154. 'validate' => $this->validate,
  155. 'col' => $this->col
  156. ];
  157. }
  158. }