wiki.techinc.nl/includes/htmlform/fields/HTMLToggleSwitchField.php
Func 942d288f52 Implement HTMLToggleSwitchField for mobile layout of preferences
This is the more native way of doing things, which avoids syncing
different states between the toggle and the hidden widget.
The DOM structure is also better and don't confuse screen readers.

Bug: T333496
Bug: T334705
Bug: T336107
Change-Id: I47de69459b96f172153065094eb4113584bb435a
2023-05-18 02:23:28 +08:00

49 lines
1 KiB
PHP

<?php
use MediaWiki\Widget\ToggleSwitchWidget;
class HTMLToggleSwitchField extends HTMLCheckField {
/**
* Get the OOUI version of this field.
*
* @since 1.41
* @param string $value
* @return ToggleSwitchWidget
*/
public function getInputOOUI( $value ) {
if ( !empty( $this->mParams['invert'] ) ) {
$value = !$value;
}
$attr = $this->getTooltipAndAccessKeyOOUI();
$attr['id'] = $this->mID;
$attr['name'] = $this->mName;
$attr += OOUI\Element::configFromHtmlAttributes(
$this->getAttributes( [ 'disabled', 'tabindex' ] )
);
if ( $this->mClass !== '' ) {
$attr['classes'] = [ $this->mClass ];
}
// For the underlaying CheckboxInputWidget
$attr['selected'] = $value;
$attr['value'] = '1';
return new ToggleSwitchWidget( $attr );
}
/**
* @inheritDoc
*/
protected function shouldInfuseOOUI() {
// Always infuse, as we want a toggle widget when JS is enabled.
return true;
}
protected function getOOUIModules() {
return [ 'mediawiki.widgets.ToggleSwitchWidget' ];
}
}