ColorPicker.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 ColorPicker
  13. *
  14. * @package FormBuilder\components
  15. * @method $this disabled(Boolean $bool) 是否禁用
  16. * @method $this alpha(Boolean $bool) 是否支持透明度选择, 默认为false
  17. * @method $this hue(Boolean $bool) 是否支持色彩选择, 默认为true
  18. * @method $this recommend(Boolean $bool) 是否显示推荐的颜色预设, 默认为false
  19. * @method $this size(String $size) 尺寸,可选值为large、small、default或者不设置
  20. * @method $this format(String $format) 颜色的格式,可选值为 hsl、hsv、hex、rgb String 开启 alpha 时为 rgb,其它为 hex
  21. */
  22. class ColorPicker extends FormComponentDriver
  23. {
  24. /**
  25. * @var string
  26. */
  27. protected $name = 'colorPicker';
  28. /**
  29. * @var array
  30. */
  31. protected $props = [
  32. 'colors' => []
  33. ];
  34. /**
  35. * @var array
  36. */
  37. protected static $propsRule = [
  38. 'disabled' => 'boolean',
  39. 'alpha' => 'boolean',
  40. 'hue' => 'boolean',
  41. 'recommend' => 'boolean',
  42. 'size' => 'string',
  43. 'format' => 'string',
  44. ];
  45. /**
  46. * 自定义颜色预设
  47. *
  48. * @param $colors
  49. * @return $this
  50. */
  51. public function colors($colors)
  52. {
  53. $colors = Helper::toType($colors, 'array');
  54. $this->props['colors'] = array_merge($this->props['colors'], $colors);
  55. return $this;
  56. }
  57. public function getValidateHandler()
  58. {
  59. return Validate::str();
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function build()
  65. {
  66. return [
  67. 'type' => $this->name,
  68. 'field' => $this->field,
  69. 'title' => $this->title,
  70. 'value' => $this->value,
  71. 'props' => (object)$this->props,
  72. 'validate' => $this->validate,
  73. 'col' => $this->col
  74. ];
  75. }
  76. }