3 namespace MediaWiki\HTMLForm\Field
;
5 use MediaWiki\Html\Html
;
6 use MediaWiki\HTMLForm\HTMLFormField
;
12 * Besides the parameters recognized by HTMLFormField, the following are
14 * autocomplete - HTML autocomplete value (a boolean for on/off or a string according to
15 * https://html.spec.whatwg.org/multipage/forms.html#autofill )
19 class HTMLTextField
extends HTMLFormField
{
21 protected $mPlaceholder = '';
23 /** @var bool HTML autocomplete attribute */
24 protected $autocomplete;
29 * @param array $params
30 * - type: HTML textfield type
31 * - size: field size in characters (defaults to 45)
32 * - placeholder/placeholder-message: set HTML placeholder attribute
33 * - spellcheck: set HTML spellcheck attribute
34 * - persistent: upon unsuccessful requests, retain the value (defaults to true, except
35 * for password fields)
37 public function __construct( $params ) {
38 if ( isset( $params['autocomplete'] ) && is_bool( $params['autocomplete'] ) ) {
39 $params['autocomplete'] = $params['autocomplete'] ?
'on' : 'off';
42 parent
::__construct( $params );
44 if ( isset( $params['placeholder-message'] ) ) {
45 $this->mPlaceholder
= $this->getMessage( $params['placeholder-message'] )->text();
46 } elseif ( isset( $params['placeholder'] ) ) {
47 $this->mPlaceholder
= $params['placeholder'];
55 public function getSize() {
56 return $this->mParams
['size'] ??
45;
59 public function getSpellCheck() {
60 $val = $this->mParams
['spellcheck'] ??
null;
61 if ( is_bool( $val ) ) {
62 // "spellcheck" attribute literally requires "true" or "false" to work.
63 return $val ?
'true' : 'false';
68 public function isPersistent() {
69 if ( isset( $this->mParams
['persistent'] ) ) {
70 return $this->mParams
['persistent'];
72 // don't put passwords into the HTML body, they could get cached or otherwise leaked
73 return !( isset( $this->mParams
['type'] ) && $this->mParams
['type'] === 'password' );
80 public function getInputHTML( $value ) {
81 if ( !$this->isPersistent() ) {
87 'name' => $this->mName
,
88 'size' => $this->getSize(),
91 'spellcheck' => $this->getSpellCheck(),
92 ] +
$this->getTooltipAndAccessKey() +
$this->getDataAttribs();
94 if ( $this->mClass
!== '' ) {
95 $attribs['class'] = $this->mClass
;
97 if ( $this->mPlaceholder
!== '' ) {
98 $attribs['placeholder'] = $this->mPlaceholder
;
101 # @todo Enforce pattern, step, required, readonly on the server side as
117 // Only used in HTML mode:
122 $attribs +
= $this->getAttributes( $allowedParams );
125 $type = $this->getType( $attribs );
127 $inputHtml = Html
::input( $this->mName
, $value, $type, $attribs );
131 protected function getType( &$attribs ) {
132 $type = $attribs['type'] ??
'text';
133 unset( $attribs['type'] );
135 # Implement tiny differences between some field variants
136 # here, rather than creating a new class for each one which
137 # is essentially just a clone of this one.
138 if ( isset( $this->mParams
['type'] ) ) {
139 switch ( $this->mParams
['type'] ) {
142 $attribs['step'] = 1;
146 $attribs['step'] = 'any';
152 $type = $this->mParams
['type'];
154 case 'textwithbutton':
155 $type = $this->mParams
['inputtype'] ??
'text';
165 * @stable to override
167 public function getInputOOUI( $value ) {
168 if ( !$this->isPersistent() ) {
172 $attribs = $this->getTooltipAndAccessKeyOOUI();
174 if ( $this->mClass
!== '' ) {
175 $attribs['classes'] = [ $this->mClass
];
177 if ( $this->mPlaceholder
!== '' ) {
178 $attribs['placeholder'] = $this->mPlaceholder
;
181 # @todo Enforce pattern, step, required, readonly on the server side as
197 // Only used in OOUI mode:
203 $attribs +
= \OOUI\Element
::configFromHtmlAttributes(
204 $this->getAttributes( $allowedParams )
207 $type = $this->getType( $attribs );
208 if ( isset( $attribs['step'] ) && $attribs['step'] === 'any' ) {
209 $attribs['step'] = null;
212 return $this->getInputWidget( [
214 'name' => $this->mName
,
217 'dir' => $this->mDir
,
221 public function getInputCodex( $value, $hasErrors ) {
222 if ( !$this->isPersistent() ) {
228 'name' => $this->mName
,
229 'size' => $this->getSize(),
231 'dir' => $this->mDir
,
232 'spellcheck' => $this->getSpellCheck(),
233 ] +
$this->getTooltipAndAccessKey() +
$this->getDataAttribs();
235 if ( $this->mPlaceholder
!== '' ) {
236 $attribs['placeholder'] = $this->mPlaceholder
;
238 $attribs['class'] = $this->mClass ?
[ $this->mClass
] : [];
258 $attribs +
= $this->getAttributes( $allowedParams );
261 $type = $this->getType( $attribs );
263 return static::buildCodexComponent( $value, $hasErrors, $type, $this->mName
, $attribs );
267 * Build the markup of the Codex component
269 * @param string $value The value to set the input to
270 * @param bool $hasErrors Whether there are validation errors.
271 * @param string $type The input's type attribute
272 * @param string $name The input's name attribute
273 * @param array $inputAttribs Other input attributes
274 * @return string Raw HTML
276 public static function buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs ) {
277 // Set up classes for the outer <div>.
278 $wrapperClass = [ 'cdx-text-input' ];
280 $wrapperClass[] = 'cdx-text-input--status-error';
283 $inputAttribs['class'][] = 'cdx-text-input__input';
284 $inputHtml = Html
::input( $name, $value, $type, $inputAttribs );
286 return Html
::rawElement( 'div', [ 'class' => $wrapperClass ], $inputHtml );
290 * @stable to override
292 * @param array $params
296 protected function getInputWidget( $params ) {
297 return new \OOUI\
TextInputWidget( $params );
301 * Returns an array of data-* attributes to add to the field.
302 * @stable to override
306 protected function getDataAttribs() {
311 /** @deprecated class alias since 1.42 */
312 class_alias( HTMLTextField
::class, 'HTMLTextField' );