]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / widgets / TextInputWidget.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Input widget with a text field.
7  */
8 class TextInputWidget extends InputWidget {
9         use IconElement;
10         use IndicatorElement;
11
12         /* Properties */
13
14         /**
15          * Input field type.
16          *
17          * @var string
18          */
19         protected $type = null;
20
21         /**
22          * Prevent changes.
23          *
24          * @var boolean
25          */
26         protected $readOnly = false;
27
28         /**
29          * Mark as required.
30          *
31          * @var boolean
32          */
33         protected $required = false;
34
35         /**
36          * Allow multiple lines of text.
37          *
38          * @var boolean
39          */
40         protected $multiline = false;
41
42         /**
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')
46          *
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)
61          */
62         public function __construct( array $config = [] ) {
63                 // Config initialization
64                 $config = array_merge( [
65                         'type' => 'text',
66                         'readOnly' => false,
67                         'autofocus' => false,
68                         'required' => false,
69                         'autocomplete' => true,
70                 ], $config );
71                 if ( $config['type'] === 'search' ) {
72                         if ( !array_key_exists( 'icon', $config ) ) {
73                                 $config['icon'] = 'search';
74                         }
75                 }
76
77                 // Parent constructor
78                 parent::__construct( $config );
79
80                 // Properties
81                 $this->type = $this->getSaneType( $config );
82                 $this->multiline = isset( $config['multiline'] ) ? (bool)$config['multiline'] : false;
83
84                 // Traits
85                 $this->initializeIconElement( $config );
86                 $this->initializeIndicatorElement( $config );
87
88                 // Initialization
89                 $this
90                         ->addClasses( [
91                                 'oo-ui-textInputWidget',
92                                 'oo-ui-textInputWidget-type-' . $this->type,
93                                 'oo-ui-textInputWidget-php',
94                         ] )
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'] ] );
100                 }
101                 if ( isset( $config['maxLength'] ) ) {
102                         $this->input->setAttributes( [ 'maxlength' => $config['maxLength'] ] );
103                 }
104                 if ( $config['autofocus'] ) {
105                         $this->input->setAttributes( [ 'autofocus' => 'autofocus' ] );
106                 }
107                 if ( !$config['autocomplete'] ) {
108                         $this->input->setAttributes( [ 'autocomplete' => 'off' ] );
109                 }
110                 if ( $this->multiline && isset( $config['rows'] ) && $config['rows'] ) {
111                         $this->input->setAttributes( [ 'rows' => $config['rows'] ] );
112                 }
113         }
114
115         /**
116          * Check if the widget is read-only.
117          *
118          * @return bool
119          */
120         public function isReadOnly() {
121                 return $this->readOnly;
122         }
123
124         /**
125          * Set the read-only state of the widget. This should probably change the widget's appearance and
126          * prevent it from being used.
127          *
128          * @param bool $state Make input read-only
129          * @return $this
130          */
131         public function setReadOnly( $state ) {
132                 $this->readOnly = (bool)$state;
133                 if ( $this->readOnly ) {
134                         $this->input->setAttributes( [ 'readonly' => 'readonly' ] );
135                 } else {
136                         $this->input->removeAttributes( [ 'readonly' ] );
137                 }
138                 return $this;
139         }
140
141         /**
142          * Check if the widget is required.
143          *
144          * @return bool
145          */
146         public function isRequired() {
147                 return $this->required;
148         }
149
150         /**
151          * Set the required state of the widget.
152          *
153          * @param bool $state Make input required
154          * @return $this
155          */
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' );
162                         }
163                 } else {
164                         $this->input->removeAttributes( [ 'required', 'aria-required' ] );
165                         if ( $this->getIndicator() === 'required' ) {
166                                 $this->setIndicator( null );
167                         }
168                 }
169                 return $this;
170         }
171
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( [
177                                 'step' => 'any',
178                                 'type' => 'number',
179                         ] );
180                 } else {
181                         return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->getSaneType( $config ) ] );
182                 }
183         }
184
185         private function getSaneType( $config ) {
186                 $allowedTypes = [
187                         'text',
188                         'password',
189                         'email',
190                         'url',
191                         'number'
192                 ];
193                 return in_array( $config['type'], $allowedTypes ) ? $config['type'] : 'text';
194         }
195
196         /**
197          * Check if input supports multiple lines.
198          *
199          * @return bool
200          */
201         public function isMultiline() {
202                 return (bool)$this->multiline;
203         }
204
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;
211                         }
212                 }
213                 if ( $this->type !== 'text' ) {
214                         $config['type'] = $this->type;
215                 }
216                 if ( $this->isReadOnly() ) {
217                         $config['readOnly'] = true;
218                 }
219                 $placeholder = $this->input->getAttribute( 'placeholder' );
220                 if ( $placeholder !== null ) {
221                         $config['placeholder'] = $placeholder;
222                 }
223                 $maxlength = $this->input->getAttribute( 'maxlength' );
224                 if ( $maxlength !== null ) {
225                         $config['maxLength'] = $maxlength;
226                 }
227                 $autofocus = $this->input->getAttribute( 'autofocus' );
228                 if ( $autofocus !== null ) {
229                         $config['autofocus'] = true;
230                 }
231                 $required = $this->input->getAttribute( 'required' );
232                 $ariarequired = $this->input->getAttribute( 'aria-required' );
233                 if ( ( $required !== null ) || ( $ariarequired !== null ) ) {
234                         $config['required'] = true;
235                 }
236                 $autocomplete = $this->input->getAttribute( 'autocomplete' );
237                 if ( $autocomplete !== null ) {
238                         $config['autocomplete'] = false;
239                 }
240                 return parent::getConfig( $config );
241         }
242 }