6 * Multiple radio buttons input widget. Intended to be used within a OO.ui.FormLayout.
8 class RadioSelectInputWidget extends InputWidget {
15 protected $name = null;
18 * Layouts for this input, as FieldLayouts.
22 protected $fields = [];
25 * @param array $config Configuration options
26 * @param array[] $config['options'] Array of menu options in the format
27 * `[ 'data' => …, 'label' => … ]`
29 public function __construct( array $config = [] ) {
31 parent::__construct( $config );
33 if ( isset( $config['name'] ) ) {
34 $this->name = $config['name'];
38 $this->setOptions( isset( $config['options'] ) ? $config['options'] : [] );
39 $this->addClasses( [ 'oo-ui-radioSelectInputWidget' ] );
42 protected function getInputElement( $config ) {
44 return new Tag( 'unused' );
47 public function setValue( $value ) {
48 $this->value = $this->cleanUpValue( $value );
49 foreach ( $this->fields as &$field ) {
50 $field->getField()->setSelected( $field->getField()->getValue() === $this->value );
56 * Set the options available for this input.
58 * @param array[] $options Array of menu options in the format
59 * `[ 'data' => …, 'label' => … ]`
62 public function setOptions( $options ) {
63 $value = $this->getValue();
64 $isValueAvailable = false;
67 // Rebuild the radio buttons
68 $this->clearContent();
69 // Need a unique name, otherwise more than one radio will be selectable
70 // Note: This is not going in the ID attribute, not that it matters
71 $name = $this->name ?: Tag::generateElementId();
72 foreach ( $options as $opt ) {
73 $optValue = $this->cleanUpValue( $opt['data'] );
74 $field = new FieldLayout(
75 new RadioInputWidget( [
78 'disabled' => $this->isDisabled(),
81 'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue,
86 if ( $value === $optValue ) {
87 $isValueAvailable = true;
90 $this->fields[] = $field;
91 $this->appendContent( $field );
94 // Restore the previous value, or reset to something sensible
95 if ( $isValueAvailable ) {
96 // Previous value is still available
97 $this->setValue( $value );
99 // No longer valid, reset
100 if ( count( $options ) ) {
101 $this->setValue( $options[0]['data'] );
108 public function setDisabled( $state ) {
109 parent::setDisabled( $state );
110 foreach ( $this->fields as $field ) {
111 $field->getField()->setDisabled( $this->isDisabled() );
116 public function getConfig( &$config ) {
118 foreach ( $this->fields as $field ) {
119 $label = $field->getLabel();
120 $data = $field->getField()->getValue();
121 $o[] = [ 'data' => $data, 'label' => $label ];
123 $config['options'] = $o;
124 return parent::getConfig( $config );