6 * Input widget with a text field.
8 class TextInputWidget extends InputWidget {
19 protected $type = null;
26 protected $readOnly = false;
33 protected $required = false;
36 * Allow multiple lines of text.
40 protected $multiline = false;
43 * @param array $config Configuration options
44 * @param string $config['type'] HTML tag `type` attribute: 'text', 'password', 'search', 'email',
45 * 'url' or 'number'. Ignored if `multiline` is true. (default: 'text')
47 * Some values of `type` result in additional behaviors:
48 * - `search`: implies `icon: 'search'` and `indicator: 'clear'`; when clicked, the indicator
49 * empties the text field
50 * @param string $config['placeholder'] Placeholder text
51 * @param bool $config['autofocus'] Ask the browser to focus this widget, using the 'autofocus'
52 * HTML attribute (default: false)
53 * @param bool $config['readOnly'] Prevent changes (default: false)
54 * @param number $config['maxLength'] Maximum allowed number of characters to input
55 * @param bool $config['multiline'] Allow multiple lines of text (default: false)
56 * @param int $config['rows'] If multiline, number of visible lines in textarea
57 * @param bool $config['required'] Mark the field as required.
58 * Implies `indicator: 'required'`. (default: false)
59 * @param bool $config['autocomplete'] If the field should support autocomplete
60 * or not (default: true)
62 public function __construct( array $config = [] ) {
63 // Config initialization
64 $config = array_merge( [
69 'autocomplete' => true,
71 if ( $config['type'] === 'search' ) {
72 if ( !array_key_exists( 'icon', $config ) ) {
73 $config['icon'] = 'search';
78 parent::__construct( $config );
81 $this->type = $this->getSaneType( $config );
82 $this->multiline = isset( $config['multiline'] ) ? (bool)$config['multiline'] : false;
85 $this->initializeIconElement( $config );
86 $this->initializeIndicatorElement( $config );
91 'oo-ui-textInputWidget',
92 'oo-ui-textInputWidget-type-' . $this->type,
93 'oo-ui-textInputWidget-php',
95 ->appendContent( $this->icon, $this->indicator );
96 $this->setReadOnly( $config['readOnly'] );
97 $this->setRequired( $config['required'] );
98 if ( isset( $config['placeholder'] ) ) {
99 $this->input->setAttributes( [ 'placeholder' => $config['placeholder'] ] );
101 if ( isset( $config['maxLength'] ) ) {
102 $this->input->setAttributes( [ 'maxlength' => $config['maxLength'] ] );
104 if ( $config['autofocus'] ) {
105 $this->input->setAttributes( [ 'autofocus' => 'autofocus' ] );
107 if ( !$config['autocomplete'] ) {
108 $this->input->setAttributes( [ 'autocomplete' => 'off' ] );
110 if ( $this->multiline && isset( $config['rows'] ) && $config['rows'] ) {
111 $this->input->setAttributes( [ 'rows' => $config['rows'] ] );
116 * Check if the widget is read-only.
120 public function isReadOnly() {
121 return $this->readOnly;
125 * Set the read-only state of the widget. This should probably change the widget's appearance and
126 * prevent it from being used.
128 * @param bool $state Make input read-only
131 public function setReadOnly( $state ) {
132 $this->readOnly = (bool)$state;
133 if ( $this->readOnly ) {
134 $this->input->setAttributes( [ 'readonly' => 'readonly' ] );
136 $this->input->removeAttributes( [ 'readonly' ] );
142 * Check if the widget is required.
146 public function isRequired() {
147 return $this->required;
151 * Set the required state of the widget.
153 * @param bool $state Make input required
156 public function setRequired( $state ) {
157 $this->required = (bool)$state;
158 if ( $this->required ) {
159 $this->input->setAttributes( [ 'required' => 'required', 'aria-required' => 'true' ] );
160 if ( $this->getIndicator() === null ) {
161 $this->setIndicator( 'required' );
164 $this->input->removeAttributes( [ 'required', 'aria-required' ] );
165 if ( $this->getIndicator() === 'required' ) {
166 $this->setIndicator( null );
172 protected function getInputElement( $config ) {
173 if ( isset( $config['multiline'] ) && $config['multiline'] ) {
174 return new Tag( 'textarea' );
175 } elseif ( $this->getSaneType( $config ) === 'number' ) {
176 return ( new Tag( 'input' ) )->setAttributes( [
181 return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->getSaneType( $config ) ] );
185 private function getSaneType( $config ) {
193 return in_array( $config['type'], $allowedTypes ) ? $config['type'] : 'text';
197 * Check if input supports multiple lines.
201 public function isMultiline() {
202 return (bool)$this->multiline;
205 public function getConfig( &$config ) {
206 if ( $this->isMultiline() ) {
207 $config['multiline'] = true;
208 $rows = $this->input->getAttribute( 'rows' );
209 if ( $rows !== null ) {
210 $config['rows'] = $rows;
213 if ( $this->type !== 'text' ) {
214 $config['type'] = $this->type;
216 if ( $this->isReadOnly() ) {
217 $config['readOnly'] = true;
219 $placeholder = $this->input->getAttribute( 'placeholder' );
220 if ( $placeholder !== null ) {
221 $config['placeholder'] = $placeholder;
223 $maxlength = $this->input->getAttribute( 'maxlength' );
224 if ( $maxlength !== null ) {
225 $config['maxLength'] = $maxlength;
227 $autofocus = $this->input->getAttribute( 'autofocus' );
228 if ( $autofocus !== null ) {
229 $config['autofocus'] = true;
231 $required = $this->input->getAttribute( 'required' );
232 $ariarequired = $this->input->getAttribute( 'aria-required' );
233 if ( ( $required !== null ) || ( $ariarequired !== null ) ) {
234 $config['required'] = true;
236 $autocomplete = $this->input->getAttribute( 'autocomplete' );
237 if ( $autocomplete !== null ) {
238 $config['autocomplete'] = false;
240 return parent::getConfig( $config );