Select.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. use FormBuilder\traits\component\ComponentOptionsTrait;
  11. /**
  12. * 选择器组件
  13. * Class Select
  14. * @package FormBuilder\components
  15. * @method $this multiple(Boolean $bool) 是否支持多选, 默认为false
  16. * @method $this disabled(Boolean $bool) 是否禁用, 默认为false
  17. * @method $this clearable(Boolean $bool) 是否可以清空选项,只在单选时有效, 默认为false
  18. * @method $this filterable(Boolean $bool) 是否支持搜索, 默认为false
  19. * @method $this size(String $size) 选择框大小,可选值为large、small、default或者不填
  20. * @method $this placeholder(String $placeholder) 选择框默认文字
  21. * @method $this transfer(String $transfer) 是否将弹层放置于 body 内,在 Tabs、带有 fixed 的 Table 列内使用时,建议添加此属性,它将不受父级样式影响,从而达到更好的效果, 默认为false
  22. * @method $this placement(String $placement) 弹窗的展开方向,可选值为 bottom 和 top, 默认为bottom
  23. * @method $this notFoundText(String $text) 当下拉列表为空时显示的内容, 默认为 无匹配数据
  24. *
  25. */
  26. class Select extends FormComponentDriver
  27. {
  28. use ComponentOptionsTrait;
  29. /**
  30. * @var string
  31. */
  32. protected $name = 'select';
  33. /**
  34. * @var array
  35. */
  36. protected $value = [];
  37. /**
  38. * @var array
  39. */
  40. protected $props = [
  41. 'multiple' => false
  42. ];
  43. /**
  44. * @var array
  45. */
  46. protected static $propsRule = [
  47. 'multiple' => 'boolean',
  48. 'disabled' => 'boolean',
  49. 'clearable' => 'boolean',
  50. 'filterable' => 'boolean',
  51. 'size' => 'string',
  52. 'placeholder' => 'string',
  53. 'transfer' => 'string',
  54. 'placement' => 'string',
  55. 'notFoundText' => 'string',
  56. ];
  57. /**
  58. *
  59. */
  60. protected function init()
  61. {
  62. $this->placeholder($this->getPlaceHolder());
  63. }
  64. /**
  65. * @param $value
  66. * @return $this
  67. */
  68. public function value($value)
  69. {
  70. if ($value === null) return $this;
  71. if (!is_array($value))
  72. $this->value[] = $value;
  73. else {
  74. foreach ($value as $v) {
  75. $this->value[] = (string)$v;
  76. }
  77. }
  78. return $this;
  79. }
  80. protected function getValidateHandler()
  81. {
  82. if($this->props['multiple'] == true)
  83. return Validate::arr();
  84. else
  85. return Validate::str();
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function build()
  91. {
  92. $options = [];
  93. foreach ($this->options as $option) {
  94. if ($option instanceof Option)
  95. $options[] = $option->build();
  96. }
  97. $value = $this->value;
  98. $isArr = is_array($value);
  99. if ($this->props['multiple'] == false && $isArr)
  100. $value = isset($value[0]) ? $value[0] : '';
  101. else if($isArr){
  102. $value = array_unique($value);
  103. foreach ($value as $k=>$v){
  104. $value[$k] = (string)$v;
  105. }
  106. }
  107. return [
  108. 'type' => $this->name,
  109. 'field' => $this->field,
  110. 'title' => $this->title,
  111. 'value' => $value,
  112. 'props' => (object)$this->props,
  113. 'options' => $options,
  114. 'validate' => $this->validate,
  115. 'col'=>$this->col
  116. ];
  117. }
  118. }