FormStyle.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * FormBuilder表单生成器
  4. * Author: xaboy
  5. * Github: https://github.com/xaboy/form-builder
  6. */
  7. namespace FormBuilder\components;
  8. use FormBuilder\interfaces\FormComponentInterFace;
  9. use FormBuilder\traits\component\CallPropsTrait;
  10. /**
  11. * form表单样式
  12. * Class FormStyle
  13. *
  14. * @package FormBuilder\components
  15. * @method $this inline(Boolean $bool) 是否开启行内表单模式
  16. * @method $this labelPosition(String $labelPosition) 表单域标签的位置,可选值为 left、right、top
  17. * @method $this labelWidth(Number $labelWidth) 表单域标签的宽度,所有的 FormItem 都会继承 Form 组件的 label-width 的值
  18. * @method $this showMessage(Boolean $bool) 是否显示校验错误信息
  19. */
  20. class FormStyle implements FormComponentInterFace
  21. {
  22. use CallPropsTrait;
  23. /**
  24. * @var array
  25. */
  26. protected $props;
  27. /**
  28. * @var array
  29. */
  30. protected static $propsRule = [
  31. 'inline' => 'boolean',
  32. 'labelPosition' => 'string',
  33. 'labelWidth' => 'number',
  34. 'showMessage' => 'boolean',
  35. ];
  36. /**
  37. * FormStyle constructor.
  38. *
  39. * @param bool $inline
  40. * @param string $labelPosition
  41. * @param int $labelWidth
  42. * @param bool $showMessage
  43. * @param string $autocomplete
  44. */
  45. public function __construct($inline = false, $labelPosition = 'right', $labelWidth = 125, $showMessage = true, $autocomplete = 'off')
  46. {
  47. $this->props = compact('inline', 'labelPosition', 'labelWidth', 'showMessage');
  48. $this->autocomplete($autocomplete);
  49. }
  50. /**
  51. * 原生的 autocomplete 属性,可选值为 true = off 或 false = on
  52. *
  53. * @param bool $bool
  54. * @return $this
  55. */
  56. public function autocomplete($bool = false)
  57. {
  58. $this->props['autocomplete'] = $bool === false ? 'on' : 'off';
  59. return $this;
  60. }
  61. /**
  62. * @return array
  63. */
  64. public function build()
  65. {
  66. return $this->props;
  67. }
  68. }