Cascader.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Cascader
  13. *
  14. * @package FormBuilder\components
  15. * @method $this type(String $type) 数据类型, 支持 city_area(省市区三级联动), city (省市二级联动), other (自定义)
  16. * @method $this disabled(Boolean $bool) 是否禁用选择器
  17. * @method $this clearable(Boolean $bool) 是否支持清除
  18. * @method $this placeholder(String $placeholder)
  19. * @method $this trigger(String $trigger) 次级菜单展开方式,可选值为 click 或 hover
  20. * @method $this changeOnSelect(Boolean $bool) 当此项为 true 时,点选每级菜单选项值都会发生变化, 默认为 false
  21. * @method $this size(String $size) 输入框大小,可选值为large和small或者不填
  22. * @method $this filterable(Boolean $bool) 是否支持搜索
  23. * @method $this notFoundText(String $text) 当搜索列表为空时显示的内容
  24. * @method $this transfer(Boolean $bool) /是否将弹层放置于 body 内,在 Tabs、带有 fixed 的 Table 列内使用时,建议添加此属性,它将不受父级样式影响,从而达到更好的效果
  25. */
  26. class Cascader extends FormComponentDriver
  27. {
  28. /**
  29. * @var string
  30. */
  31. protected $name = 'cascader';
  32. /**
  33. *
  34. */
  35. const TYPE_CITY_AREA = 'city_area';
  36. /**
  37. *
  38. */
  39. const TYPE_CITY = 'city';
  40. /**
  41. *
  42. */
  43. const TYPE_OTHER = 'other';
  44. /**
  45. * @var array
  46. */
  47. protected $props = [
  48. 'type' => self::TYPE_OTHER,
  49. 'data' => []
  50. ];
  51. /**
  52. * @var array
  53. */
  54. protected static $propsRule = [
  55. 'type' => 'string',
  56. 'disabled' => 'boolean',
  57. 'clearable' => 'boolean',
  58. 'changeOnSelect' => 'boolean',
  59. 'filterable' => 'boolean',
  60. 'transfer' => 'boolean',
  61. 'placeholder' => 'string',
  62. 'trigger' => 'string',
  63. 'size' => 'string',
  64. 'notFoundText' => 'string',
  65. ];
  66. protected function init()
  67. {
  68. $this->placeholder($this->getPlaceHolder());
  69. }
  70. /**
  71. * @param array $value
  72. * @return $this
  73. */
  74. public function value($value)
  75. {
  76. if (!is_array($value)) $value = [];
  77. $this->value = $value;
  78. return $this;
  79. }
  80. /**
  81. * 可选项的数据源
  82. * 例如:{
  83. * "value":"北京市", "label":"北京市", "children":[{
  84. * "value":"东城区", "label":"东城区"
  85. * }]
  86. * }
  87. *
  88. * @param array $data
  89. * @return $this
  90. */
  91. public function data(array $data)
  92. {
  93. if (!is_array($this->props['data'])) $this->props['data'] = [];
  94. $this->props['data'] = array_merge($this->props['data'], $data);
  95. return $this;
  96. }
  97. /**
  98. * @param $var
  99. * @return $this
  100. */
  101. public function jsData($var)
  102. {
  103. $this->props['data'] = 'js.' . $var;
  104. return $this;
  105. }
  106. /**
  107. * 获取组件类型
  108. *
  109. * @return mixed
  110. */
  111. public function getType()
  112. {
  113. return $this->props['type'];
  114. }
  115. /**
  116. * @return Validate
  117. */
  118. public function getValidateHandler()
  119. {
  120. return Validate::arr();
  121. }
  122. /**
  123. * @return array
  124. */
  125. public function build()
  126. {
  127. return [
  128. 'type' => $this->name,
  129. 'field' => $this->field,
  130. 'title' => $this->title,
  131. 'value' => $this->value,
  132. 'props' => (object)$this->props,
  133. 'validate' => $this->validate,
  134. 'col' => $this->col
  135. ];
  136. }
  137. }