4 * Select dropdown field, with an additional "other" textbox.
6 * HTMLComboboxField implements the same functionality using a single form field
7 * and should be used instead.
9 class HTMLSelectOrOtherField
extends HTMLTextField
{
10 public function __construct( $params ) {
11 parent
::__construct( $params );
13 if ( !in_array( 'other', $this->mOptions
, true ) ) {
15 $params['other'] ??
wfMessage( 'htmlform-selectorother-other' )->text();
16 // Have 'other' always as first element
17 $this->mOptions
= [ $msg => 'other' ] +
$this->mOptions
;
21 public function getInputHTML( $value ) {
24 if ( $value !== false ) {
25 $value = strval( $value );
26 $valInSelect = in_array(
27 $value, HTMLFormField
::flattenOptions( $this->getOptions() ), true
31 $selected = $valInSelect ?
$value : 'other';
33 $select = new XmlSelect( $this->mName
, $this->mID
, $selected );
34 $select->addOptions( $this->getOptions() );
36 $select->setAttribute( 'class', 'mw-htmlform-select-or-other' );
38 $tbAttribs = [ 'id' => $this->mID
. '-other', 'size' => $this->getSize() ];
40 if ( !empty( $this->mParams
['disabled'] ) ) {
41 $select->setAttribute( 'disabled', 'disabled' );
42 $tbAttribs['disabled'] = 'disabled';
45 if ( isset( $this->mParams
['tabindex'] ) ) {
46 $select->setAttribute( 'tabindex', $this->mParams
['tabindex'] );
47 $tbAttribs['tabindex'] = $this->mParams
['tabindex'];
50 $select = $select->getHTML();
52 if ( isset( $this->mParams
['maxlength'] ) ) {
53 $tbAttribs['maxlength'] = $this->mParams
['maxlength'];
56 if ( $this->mClass
!== '' ) {
57 $tbAttribs['class'] = $this->mClass
;
60 $textbox = Html
::input( $this->mName
. '-other', $valInSelect ?
'' : $value, 'text', $tbAttribs );
62 return "$select<br />\n$textbox";
65 protected function shouldInfuseOOUI() {
69 protected function getOOUIModules() {
70 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
73 public function getInputOOUI( $value ) {
74 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
77 if ( $value !== false ) {
78 $value = strval( $value );
79 $valInSelect = in_array(
80 $value, HTMLFormField
::flattenOptions( $this->getOptions() ), true
86 'name' => $this->mName
,
87 'options' => $this->getOptionsOOUI(),
88 'value' => $valInSelect ?
$value : 'other',
89 'class' => [ 'mw-htmlform-select-or-other' ],
97 $dropdownAttribs +
= OOUI\Element
::configFromHtmlAttributes(
98 $this->getAttributes( $allowedParams )
103 'name' => $this->mName
. '-other',
104 'size' => $this->getSize(),
105 'value' => $valInSelect ?
'' : $value,
117 $textAttribs +
= OOUI\Element
::configFromHtmlAttributes(
118 $this->getAttributes( $allowedParams )
121 if ( $this->mClass
!== '' ) {
122 $textAttribs['classes'] = [ $this->mClass
];
124 if ( $this->mPlaceholder
!== '' ) {
125 $textAttribs['placeholder'] = $this->mPlaceholder
;
129 if ( isset( $this->mParams
[ 'disabled' ] ) && $this->mParams
[ 'disabled' ] ) {
133 return $this->getInputWidget( [
135 'disabled' => $disabled,
136 'textinput' => $textAttribs,
137 'dropdowninput' => $dropdownAttribs,
142 public function getInputWidget( $params ) {
143 return new MediaWiki\Widget\
SelectWithInputWidget( $params );
147 * @param WebRequest $request
151 public function loadDataFromRequest( $request ) {
152 if ( $request->getCheck( $this->mName
) ) {
153 $val = $request->getText( $this->mName
);
155 if ( $val === 'other' ) {
156 $val = $request->getText( $this->mName
. '-other' );
161 return $this->getDefault();