| 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 |
use FlaggedElement; |
| 12 |
use RequiredElement; |
| 13 |
|
| 14 |
/* Properties */ |
| 15 |
|
| 16 |
/** |
| 17 |
* Input field type. |
| 18 |
* |
| 19 |
* @var string |
| 20 |
*/ |
| 21 |
protected $type = null; |
| 22 |
|
| 23 |
/** |
| 24 |
* Prevent changes. |
| 25 |
* |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
protected $readOnly = false; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param array $config Configuration options |
| 32 |
* - string $config['type'] HTML tag `type` attribute: 'text', 'password', 'email', |
| 33 |
* 'url' or 'number'. (default: 'text') |
| 34 |
* - string $config['placeholder'] Placeholder text |
| 35 |
* - bool $config['autofocus'] Ask the browser to focus this widget, using the 'autofocus' |
| 36 |
* HTML attribute (default: false) |
| 37 |
* - bool $config['readOnly'] Prevent changes (default: false) |
| 38 |
* - int $config['maxLength'] Maximum allowed number of characters to input |
| 39 |
* For unfortunate historical reasons, this counts the number of UTF-16 code units rather |
| 40 |
* than Unicode codepoints, which means that codepoints outside the Basic Multilingual |
| 41 |
* Plane (e.g. many emojis) count as 2 characters each. |
| 42 |
* - int $config['minLength'] Minimum allowed number of characters to input |
| 43 |
* Same considerations for maxLength apply as mentioned above. |
| 44 |
* - bool|string $config['autocomplete'] If the field should support autocomplete |
| 45 |
* or not (default: true). Can also be an autocomplete type hint. |
| 46 |
* - bool $config['spellcheck'] If the field should support spellcheck |
| 47 |
* or not (default: browser-dependent) |
| 48 |
*/ |
| 49 |
public function __construct( array $config = [] ) { |
| 50 |
// Config initialization |
| 51 |
if ( is_bool( $config['autocomplete'] ?? null ) ) { |
| 52 |
$config['autocomplete'] = $config['autocomplete'] ? 'on' : 'off'; |
| 53 |
} |
| 54 |
|
| 55 |
// Parent constructor |
| 56 |
parent::__construct( $config ); |
| 57 |
|
| 58 |
// Properties |
| 59 |
$this->type = $this->getValidType( $config ); |
| 60 |
|
| 61 |
// Traits |
| 62 |
$this->initializeIconElement( $config ); |
| 63 |
$this->initializeIndicatorElement( $config ); |
| 64 |
$this->initializeFlaggedElement( |
| 65 |
array_merge( [ 'flagged' => $this ], $config ) |
| 66 |
); |
| 67 |
$this->initializeRequiredElement( $config ); |
| 68 |
|
| 69 |
// Initialization |
| 70 |
$this |
| 71 |
->addClasses( [ |
| 72 |
'oo-ui-textInputWidget', |
| 73 |
'oo-ui-textInputWidget-type-' . $this->type, |
| 74 |
'oo-ui-textInputWidget-php', |
| 75 |
] ) |
| 76 |
->appendContent( $this->icon, $this->indicator ); |
| 77 |
$this->setReadOnly( $config['readOnly'] ?? false ); |
| 78 |
if ( isset( $config['placeholder'] ) ) { |
| 79 |
$this->input->setAttributes( [ 'placeholder' => $config['placeholder'] ] ); |
| 80 |
} |
| 81 |
if ( isset( $config['maxLength'] ) ) { |
| 82 |
$this->input->setAttributes( [ 'maxlength' => $config['maxLength'] ] ); |
| 83 |
} |
| 84 |
if ( isset( $config['minLength'] ) ) { |
| 85 |
$this->input->setAttributes( [ 'minlength' => $config['minLength'] ] ); |
| 86 |
} |
| 87 |
if ( $config['autofocus'] ?? false ) { |
| 88 |
$this->input->setAttributes( [ 'autofocus' => 'autofocus' ] ); |
| 89 |
} |
| 90 |
if ( isset( $config['autocomplete'] ) ) { |
| 91 |
$this->input->setAttributes( [ 'autocomplete' => $config['autocomplete'] ] ); |
| 92 |
} |
| 93 |
if ( isset( $config['spellcheck'] ) ) { |
| 94 |
$this->input->setAttributes( [ 'spellcheck' => $config['spellcheck'] ? 'true' : 'false' ] ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Check if the widget is read-only. |
| 100 |
* |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function isReadOnly() { |
| 104 |
return $this->readOnly; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Set the read-only state of the widget. This should probably change the widget's appearance and |
| 109 |
* prevent it from being used. |
| 110 |
* |
| 111 |
* @param bool $state Make input read-only |
| 112 |
* @return $this |
| 113 |
*/ |
| 114 |
public function setReadOnly( $state ) { |
| 115 |
$this->readOnly = (bool)$state; |
| 116 |
if ( $this->readOnly ) { |
| 117 |
$this->input->setAttributes( [ 'readonly' => 'readonly' ] ); |
| 118 |
} else { |
| 119 |
$this->input->removeAttributes( [ 'readonly' ] ); |
| 120 |
} |
| 121 |
return $this; |
| 122 |
} |
| 123 |
|
| 124 |
/** @inheritDoc */ |
| 125 |
protected function getInputElement( $config ) { |
| 126 |
if ( $this->getValidType( $config ) === 'number' ) { |
| 127 |
return ( new Tag( 'input' ) )->setAttributes( [ |
| 128 |
'step' => 'any', |
| 129 |
'type' => 'number', |
| 130 |
] ); |
| 131 |
} else { |
| 132 |
return ( new Tag( 'input' ) )->setAttributes( [ 'type' => $this->getValidType( $config ) ] ); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param array $config |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
protected function getValidType( $config ) { |
| 141 |
$allowedTypes = [ |
| 142 |
'password', |
| 143 |
'email', |
| 144 |
'url', |
| 145 |
'number' |
| 146 |
]; |
| 147 |
return isset( $config['type'] ) && in_array( $config['type'], $allowedTypes ) ? $config['type'] : 'text'; |
| 148 |
} |
| 149 |
|
| 150 |
/** @inheritDoc */ |
| 151 |
public function getConfig( &$config ) { |
| 152 |
if ( $this->type !== 'text' ) { |
| 153 |
$config['type'] = $this->type; |
| 154 |
} |
| 155 |
if ( $this->isReadOnly() ) { |
| 156 |
$config['readOnly'] = true; |
| 157 |
} |
| 158 |
$placeholder = $this->input->getAttribute( 'placeholder' ); |
| 159 |
if ( $placeholder !== null ) { |
| 160 |
$config['placeholder'] = $placeholder; |
| 161 |
} |
| 162 |
$maxlength = $this->input->getAttribute( 'maxlength' ); |
| 163 |
if ( $maxlength !== null ) { |
| 164 |
$config['maxLength'] = $maxlength; |
| 165 |
} |
| 166 |
$minLength = $this->input->getAttribute( 'minlength' ); |
| 167 |
if ( $minLength !== null ) { |
| 168 |
$config['minLength'] = $minLength; |
| 169 |
} |
| 170 |
$autofocus = $this->input->getAttribute( 'autofocus' ); |
| 171 |
if ( $autofocus !== null ) { |
| 172 |
$config['autofocus'] = true; |
| 173 |
} |
| 174 |
$autocomplete = $this->input->getAttribute( 'autocomplete' ); |
| 175 |
if ( $autocomplete !== null ) { |
| 176 |
$config['autocomplete'] = $autocomplete; |
| 177 |
} |
| 178 |
return parent::getConfig( $config ); |
| 179 |
} |
| 180 |
} |