From 8bd6605736c47259bd5f901284cbd2e639cef30c Mon Sep 17 00:00:00 2001 From: Prateek Saxena Date: Mon, 5 Jun 2017 17:43:14 +0530 Subject: [PATCH] HTMLSelectAndOtherField/HTMLSelectOrOtherField: Add OOUI implementation Bug: T106999 Change-Id: I0f437ed8a8808f8090bf466fd939345d74d57c34 --- .../fields/HTMLSelectAndOtherField.php | 64 +++++++++++++++- .../fields/HTMLSelectOrOtherField.php | 74 ++++++++++++++++++- 2 files changed, 136 insertions(+), 2 deletions(-) diff --git a/includes/htmlform/fields/HTMLSelectAndOtherField.php b/includes/htmlform/fields/HTMLSelectAndOtherField.php index 9af60e5c22..38b487af10 100644 --- a/includes/htmlform/fields/HTMLSelectAndOtherField.php +++ b/includes/htmlform/fields/HTMLSelectAndOtherField.php @@ -63,8 +63,70 @@ class HTMLSelectAndOtherField extends HTMLSelectField { return "$select
\n$textbox"; } + protected function getOOUIModules() { + return [ 'mediawiki.widgets.SelectWithInputWidget' ]; + } + public function getInputOOUI( $value ) { - return false; + $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' ); + + # TextInput + $textAttribs = [ + 'id' => $this->mID . '-other', + 'name' => $this->mName . '-other', + 'size' => $this->getSize(), + 'class' => [ 'mw-htmlform-select-and-other-field' ], + 'data-id-select' => $this->mID, + 'value' => $value[2], + ]; + + $allowedParams = [ + 'required', + 'autofocus', + 'multiple', + 'disabled', + 'tabindex', + 'maxlength', + ]; + + $textAttribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); + + if ( $this->mClass !== '' ) { + $textAttribs['classes'] = [ $this->mClass ]; + } + + # DropdownInput + $dropdownInputAttribs = [ + 'name' => $this->mName, + 'id' => $this->mID, + 'options' => $this->getOptionsOOUI(), + 'value' => $value[1], + ]; + + $allowedParams = [ + 'tabindex', + 'disabled', + ]; + + $dropdownInputAttribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); + + if ( $this->mClass !== '' ) { + $dropdownInputAttribs['classes'] = [ $this->mClass ]; + } + + return $this->getInputWidget( [ + 'textinput' => $textAttribs, + 'dropdowninput' => $dropdownInputAttribs, + 'or' => false, + ] ); + } + + public function getInputWidget( $params ) { + return new Mediawiki\Widget\SelectWithInputWidget( $params ); } /** diff --git a/includes/htmlform/fields/HTMLSelectOrOtherField.php b/includes/htmlform/fields/HTMLSelectOrOtherField.php index bb410799ef..a009b287c2 100644 --- a/includes/htmlform/fields/HTMLSelectOrOtherField.php +++ b/includes/htmlform/fields/HTMLSelectOrOtherField.php @@ -64,8 +64,80 @@ class HTMLSelectOrOtherField extends HTMLTextField { return "$select
\n$textbox"; } + protected function shouldInfuseOOUI() { + return true; + } + + protected function getOOUIModules() { + return [ 'mediawiki.widgets.SelectWithInputWidget' ]; + } + public function getInputOOUI( $value ) { - return false; + $this->mParent->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' ); + + $valInSelect = false; + if ( $value !== false ) { + $value = strval( $value ); + $valInSelect = in_array( + $value, HTMLFormField::flattenOptions( $this->getOptions() ), true + ); + } + + # DropdownInput + $dropdownAttribs = [ + 'id' => $this->mID, + 'name' => $this->mName, + 'options' => $this->getOptionsOOUI(), + 'value' => $valInSelect ? $value : 'other', + 'class' => [ 'mw-htmlform-select-or-other' ], + ]; + + $allowedParams = [ + 'disabled', + 'tabindex', + ]; + + $dropdownAttribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); + + # TextInput + $textAttribs = [ + 'id' => $this->mID . '-other', + 'name' => $this->mName . '-other', + 'size' => $this->getSize(), + 'value' => $valInSelect ? '' : $value, + ]; + + $allowedParams = [ + 'required', + 'autofocus', + 'multiple', + 'disabled', + 'tabindex', + 'maxlength', + ]; + + $textAttribs += OOUI\Element::configFromHtmlAttributes( + $this->getAttributes( $allowedParams ) + ); + + if ( $this->mClass !== '' ) { + $textAttribs['classes'] = [ $this->mClass ]; + } + if ( $this->mPlaceholder !== '' ) { + $textAttribs['placeholder'] = $this->mPlaceholder; + } + + return $this->getInputWidget( [ + 'textinput' => $textAttribs, + 'dropdowninput' => $dropdownAttribs, + 'or' => true, + ] ); + } + + public function getInputWidget( $params ) { + return new Mediawiki\Widget\SelectWithInputWidget( $params ); } /** -- 2.20.1