summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php')
-rw-r--r--vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php63
1 files changed, 58 insertions, 5 deletions
diff --git a/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php b/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
index a5f31f74..210729fd 100644
--- a/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
+++ b/vendor/oojs/oojs-ui/php/widgets/TextInputWidget.php
@@ -10,6 +10,13 @@ class TextInputWidget extends InputWidget {
/* Properties */
/**
+ * Input field type.
+ *
+ * @var string
+ */
+ protected $type = null;
+
+ /**
* Prevent changes.
*
* @var boolean
@@ -25,27 +32,49 @@ class TextInputWidget extends InputWidget {
/**
* @param array $config Configuration options
- * @param string $config['type'] HTML tag `type` attribute (default: 'text')
+ * @param string $config['type'] HTML tag `type` attribute: 'text', 'password', 'search', 'email'
+ * or 'url'. Ignored if `multiline` is true. (default: 'text')
+ *
+ * Some values of `type` result in additional behaviors:
+ * - `search`: implies `icon: 'search'` and `indicator: 'clear'`; when clicked, the indicator
+ * empties the text field
* @param string $config['placeholder'] Placeholder text
* @param boolean $config['autofocus'] Ask the browser to focus this widget, using the 'autofocus'
* HTML attribute (default: false)
* @param boolean $config['readOnly'] Prevent changes (default: false)
* @param number $config['maxLength'] Maximum allowed number of characters to input
* @param boolean $config['multiline'] Allow multiple lines of text (default: false)
- * @param boolean $config['required'] Mark the field as required (default: false)
+ * @param int $config['rows'] If multiline, number of visible lines in textarea
+ * @param boolean $config['required'] Mark the field as required.
+ * Implies `indicator: 'required'`. (default: false)
+ * @param boolean $config['autocomplete'] If the field should support autocomplete
+ * or not (default: true)
*/
public function __construct( array $config = array() ) {
// Config initialization
$config = array_merge( array(
+ 'type' => 'text',
'readOnly' => false,
'autofocus' => false,
'required' => false,
+ 'autocomplete' => true,
), $config );
+ if ( $config['type'] === 'search' ) {
+ if ( !array_key_exists( 'icon', $config ) ) {
+ $config['icon'] = 'search';
+ }
+ }
+ if ( $config['required'] ) {
+ if ( !array_key_exists( 'indicator', $config ) ) {
+ $config['indicator'] = 'required';
+ }
+ }
// Parent constructor
parent::__construct( $config );
// Properties
+ $this->type = $this->getSaneType( $config );
$this->multiline = isset( $config['multiline'] ) ? (bool)$config['multiline'] : false;
// Mixins
@@ -54,7 +83,7 @@ class TextInputWidget extends InputWidget {
// Initialization
$this
- ->addClasses( array( 'oo-ui-textInputWidget' ) )
+ ->addClasses( array( 'oo-ui-textInputWidget', 'oo-ui-textInputWidget-type-' . $this->type ) )
->appendContent( $this->icon, $this->indicator );
$this->setReadOnly( $config['readOnly'] );
if ( isset( $config['placeholder'] ) ) {
@@ -69,6 +98,12 @@ class TextInputWidget extends InputWidget {
if ( $config['required'] ) {
$this->input->setAttributes( array( 'required' => 'required', 'aria-required' => 'true' ) );
}
+ if ( !$config['autocomplete'] ) {
+ $this->input->setAttributes( array( 'autocomplete' => 'off' ) );
+ }
+ if ( $this->multiline && isset( $config['rows'] ) ) {
+ $this->input->setAttributes( array( 'rows' => $config['rows'] ) );
+ }
}
/**
@@ -101,13 +136,23 @@ class TextInputWidget extends InputWidget {
if ( isset( $config['multiline'] ) && $config['multiline'] ) {
return new Tag( 'textarea' );
} else {
- $type = isset( $config['type'] ) ? $config['type'] : 'text';
$input = new Tag( 'input' );
- $input->setAttributes( array( 'type' => $type ) );
+ $input->setAttributes( array( 'type' => $this->getSaneType( $config ) ) );
return $input;
}
}
+ private function getSaneType( $config ) {
+ if ( isset( $config['multiline'] ) && $config['multiline'] ) {
+ return 'multiline';
+ } else {
+ $type = in_array( $config['type'], array( 'text', 'password', 'search', 'email', 'url' ) ) ?
+ $config['type'] :
+ 'text';
+ return $type;
+ }
+ }
+
/**
* Check if input supports multiple lines.
*
@@ -120,6 +165,10 @@ class TextInputWidget extends InputWidget {
public function getConfig( &$config ) {
if ( $this->isMultiline() ) {
$config['multiline'] = true;
+ $rows = $this->input->getAttribute( 'rows' );
+ if ( $rows !== null ) {
+ $config['rows'] = $rows;
+ }
} else {
$type = $this->input->getAttribute( 'type' );
if ( $type !== 'text' ) {
@@ -146,6 +195,10 @@ class TextInputWidget extends InputWidget {
if ( ( $required !== null ) || ( $ariarequired !== null ) ) {
$config['required'] = true;
}
+ $autocomplete = $this->input->getAttribute( 'autocomplete' );
+ if ( $autocomplete !== null ) {
+ $config['autocomplete'] = false;
+ }
return parent::getConfig( $config );
}
}