]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/widgets/RadioSelectInputWidget.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / widgets / RadioSelectInputWidget.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * Multiple radio buttons input widget. Intended to be used within a OO.ui.FormLayout.
7  */
8 class RadioSelectInputWidget extends InputWidget {
9
10         /* Properties */
11
12         /**
13          * @var string|null
14          */
15         protected $name = null;
16
17         /**
18          * Layouts for this input, as FieldLayouts.
19          *
20          * @var array
21          */
22         protected $fields = [];
23
24         /**
25          * @param array $config Configuration options
26          * @param array[] $config['options'] Array of menu options in the format
27          *   `[ 'data' => …, 'label' => … ]`
28          */
29         public function __construct( array $config = [] ) {
30                 // Parent constructor
31                 parent::__construct( $config );
32
33                 if ( isset( $config['name'] ) ) {
34                         $this->name = $config['name'];
35                 }
36
37                 // Initialization
38                 $this->setOptions( isset( $config['options'] ) ? $config['options'] : [] );
39                 $this->addClasses( [ 'oo-ui-radioSelectInputWidget' ] );
40         }
41
42         protected function getInputElement( $config ) {
43                 // Actually unused
44                 return new Tag( 'unused' );
45         }
46
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 );
51                 }
52                 return $this;
53         }
54
55         /**
56          * Set the options available for this input.
57          *
58          * @param array[] $options Array of menu options in the format
59          *   `[ 'data' => …, 'label' => … ]`
60          * @return $this
61          */
62         public function setOptions( $options ) {
63                 $value = $this->getValue();
64                 $isValueAvailable = false;
65                 $this->fields = [];
66
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( [
76                                         'name' => $name,
77                                         'value' => $optValue,
78                                         'disabled' => $this->isDisabled(),
79                                 ] ),
80                                 [
81                                         'label' => isset( $opt['label'] ) ? $opt['label'] : $optValue,
82                                         'align' => 'inline',
83                                 ]
84                         );
85
86                         if ( $value === $optValue ) {
87                                 $isValueAvailable = true;
88                         }
89
90                         $this->fields[] = $field;
91                         $this->appendContent( $field );
92                 }
93
94                 // Restore the previous value, or reset to something sensible
95                 if ( $isValueAvailable ) {
96                         // Previous value is still available
97                         $this->setValue( $value );
98                 } else {
99                         // No longer valid, reset
100                         if ( count( $options ) ) {
101                                 $this->setValue( $options[0]['data'] );
102                         }
103                 }
104
105                 return $this;
106         }
107
108         public function setDisabled( $state ) {
109                 parent::setDisabled( $state );
110                 foreach ( $this->fields as $field ) {
111                         $field->getField()->setDisabled( $this->isDisabled() );
112                 }
113                 return $this;
114         }
115
116         public function getConfig( &$config ) {
117                 $o = [];
118                 foreach ( $this->fields as $field ) {
119                         $label = $field->getLabel();
120                         $data = $field->getField()->getValue();
121                         $o[] = [ 'data' => $data, 'label' => $label ];
122                 }
123                 $config['options'] = $o;
124                 return parent::getConfig( $config );
125         }
126 }