Input.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Input组件,支持类型text、password、textarea、url、email、date
  12. * Class Input
  13. *
  14. * @package FormBuilder\components
  15. * @method $this type(String $type) 输入框类型,可选值为 text、password、textarea、url、email、date;
  16. * @method $this size(String $size) 输入框尺寸,可选值为large、small、default或者不设置;
  17. * @method $this placeholder(String $placeholder) 占位文本
  18. * @method $this clearable(Boolean $bool) 是否显示清空按钮, 默认为false
  19. * @method $this disabled(Boolean $bool) 设置输入框为禁用状态, 默认为false
  20. * @method $this readonly(Boolean $bool) 设置输入框为只读, 默认为false
  21. * @method $this maxlength(int $length) 最大输入长度
  22. * @method $this icon(String $icon) 输入框尾部图标,仅在 text 类型下有效
  23. * @method $this rows(int $rows) 文本域默认行数,仅在 textarea 类型下有效, 默认为2
  24. * @method $this number(Boolean $bool) 将用户的输入转换为 Number 类型, 默认为false
  25. * @method $this autofocus(Boolean $bool) 自动获取焦点, 默认为false
  26. * @method $this autocomplete(Boolean $bool) 原生的自动完成功能, 默认为false
  27. * @method $this spellcheck(Boolean $bool) 原生的 spellcheck 属性, 默认为false
  28. * @method $this wrap(String $warp) 原生的 wrap 属性,可选值为 hard 和 soft, 默认为soft
  29. */
  30. class Input extends FormComponentDriver
  31. {
  32. /**
  33. * @var string
  34. */
  35. protected $name = 'input';
  36. /**
  37. * 组件类型
  38. */
  39. const TYPE_TEXT = 'text';
  40. /**
  41. *
  42. */
  43. const TYPE_PASSWORD = 'password';
  44. /**
  45. *
  46. */
  47. const TYPE_TEXTAREA = 'textarea';
  48. /**
  49. *
  50. */
  51. const TYPE_URL = 'url';
  52. /**
  53. *
  54. */
  55. const TYPE_EMAIL = 'email';
  56. /**
  57. *
  58. */
  59. const TYPE_DATE = 'date';
  60. /**
  61. * @var array
  62. */
  63. protected $props = [
  64. 'type' => self::TYPE_TEXT
  65. ];
  66. /**
  67. * @var array
  68. */
  69. protected static $propsRule = [
  70. 'type' => 'string',
  71. 'size' => 'string',
  72. 'placeholder' => 'string',
  73. 'clearable' => 'boolean',
  74. 'disabled' => 'boolean',
  75. 'readonly' => 'boolean',
  76. 'maxlength' => 'int',
  77. 'icon' => 'string',
  78. 'rows' => 'int',
  79. 'number' => 'boolean',
  80. 'autofocus' => 'boolean',
  81. 'autocomplete' => 'boolean',
  82. 'spellcheck' => 'boolean',
  83. 'wrap' => 'string',
  84. ];
  85. /**
  86. *
  87. */
  88. protected function init()
  89. {
  90. $this->placeholder($this->getPlaceHolder());
  91. }
  92. protected function getPlaceHolder($pre = '请输入')
  93. {
  94. return parent::getPlaceHolder($pre);
  95. }
  96. public function getValidateHandler()
  97. {
  98. return Validate::str(Validate::TRIGGER_BLUR);
  99. }
  100. /**
  101. * 自适应内容高度,仅在 textarea 类型下有效
  102. *
  103. * @param Bool|Number $minRows
  104. * @param null|Number $maxRows
  105. * @return $this
  106. */
  107. public function autoSize($minRows = false, $maxRows = null)
  108. {
  109. $this->props['autosize'] = $maxRows === null ? boolval($minRows) : compact('minRows', 'maxRows');
  110. return $this;
  111. }
  112. /**
  113. * 生成表单规则
  114. *
  115. * @return array
  116. */
  117. public function build()
  118. {
  119. return [
  120. 'type' => $this->name,
  121. 'field' => $this->field,
  122. 'title' => $this->title,
  123. 'value' => $this->value,
  124. 'props' => (object)$this->props,
  125. 'validate' => $this->validate,
  126. 'col' => $this->col
  127. ];
  128. }
  129. }