Slider.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Slider
  13. *
  14. * @package FormBuilder\components
  15. * @method $this min(float $min) 最小值, 默认 0
  16. * @method $this max(float $max) 最大值, 默认 100
  17. * @method $this step(float $step) 步长,取值建议能被(max - min)整除, 默认 1
  18. * @method $this disabled(Boolean $bool) 是否禁用滑块, 默认 false
  19. * @method $this range(Boolean $bool) 是否开启双滑块模式, 默认
  20. * @method $this showInput(Boolean $bool) 是否显示数字输入框,仅在单滑块模式下有效, 默认 false
  21. * @method $this showStops(Boolean $bool) 是否显示间断点,建议在 step 不密集时使用, 默认 false
  22. * @method $this showTip(String $tip) 提示的显示控制,可选值为 hover(悬停,默认)、always(总是可见)、never(不可见)
  23. * @method $this inputSize(String $size) 数字输入框的尺寸,可选值为large、small、default或者不填,仅在开启 show-input 时有效
  24. *
  25. */
  26. class Slider extends FormComponentDriver
  27. {
  28. /**
  29. * @var string
  30. */
  31. protected $name = 'slider';
  32. /**
  33. * @var array
  34. */
  35. protected $props = [
  36. 'range' => false
  37. ];
  38. /**
  39. * @var array
  40. */
  41. protected static $propsRule = [
  42. 'min' => 'float',
  43. 'max' => 'float',
  44. 'step' => 'float',
  45. 'disabled' => 'boolean',
  46. 'range' => 'boolean',
  47. 'showInput' => 'boolean',
  48. 'showStops' => 'boolean',
  49. 'showTip' => 'string',
  50. 'inputSize' => 'string',
  51. ];
  52. /**
  53. * @param $value
  54. * @return $this
  55. */
  56. public function value($value)
  57. {
  58. $this->value = $value;
  59. return $this;
  60. }
  61. public function getValidateHandler()
  62. {
  63. if ($this->props['range'] == true)
  64. return Validate::arr();
  65. else
  66. return Validate::num();
  67. }
  68. /**
  69. * @return array
  70. */
  71. public function build()
  72. {
  73. $value = $this->value;
  74. if ($this->props['range'] == true) {
  75. $value = is_array($value) ? $value : [0, (int)$value];
  76. } else {
  77. $value = (int)(is_array($value)
  78. ? (isset($value[0])
  79. ? $value[0]
  80. : 0)
  81. : $value);
  82. }
  83. return [
  84. 'type' => $this->name,
  85. 'field' => $this->field,
  86. 'title' => $this->title,
  87. 'value' => $value,
  88. 'props' => (object)$this->props,
  89. 'validate' => $this->validate,
  90. 'col' => $this->col
  91. ];
  92. }
  93. }