| 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 FieldLayout[] |
| 21 |
*/ |
| 22 |
protected $fields = []; |
| 23 |
|
| 24 |
/** |
| 25 |
* @param array $config Configuration options |
| 26 |
* - 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( $config['options'] ?? [] ); |
| 39 |
$this->addClasses( [ 'oo-ui-radioSelectInputWidget' ] ); |
| 40 |
} |
| 41 |
|
| 42 |
/** @inheritDoc */ |
| 43 |
protected function getInputElement( $config ) { |
| 44 |
// Actually unused |
| 45 |
return new Tag( 'unused' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** @inheritDoc */ |
| 49 |
public function setValue( $value ) { |
| 50 |
$this->value = $this->cleanUpValue( $value ); |
| 51 |
foreach ( $this->fields as $field ) { |
| 52 |
$widget = $field->getField(); |
| 53 |
'@phan-var RadioInputWidget $widget'; |
| 54 |
$widget->setSelected( $widget->getValue() === $this->value ); |
| 55 |
} |
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Set the options available for this input. |
| 61 |
* |
| 62 |
* @param array[] $options Array of menu options in the format |
| 63 |
* `[ 'data' => …, 'label' => … ]` |
| 64 |
* @return $this |
| 65 |
*/ |
| 66 |
public function setOptions( $options ) { |
| 67 |
$value = $this->getValue(); |
| 68 |
$isValueAvailable = false; |
| 69 |
$this->fields = []; |
| 70 |
|
| 71 |
// Rebuild the radio buttons |
| 72 |
$this->clearContent(); |
| 73 |
// Need a unique name, otherwise more than one radio will be selectable |
| 74 |
// Note: This is not going in the ID attribute, not that it matters |
| 75 |
$name = $this->name ?: Tag::generateElementId(); |
| 76 |
foreach ( $options as $opt ) { |
| 77 |
$optValue = $this->cleanUpValue( $opt['data'] ); |
| 78 |
$field = new FieldLayout( |
| 79 |
new RadioInputWidget( [ |
| 80 |
'name' => $name, |
| 81 |
'value' => $optValue, |
| 82 |
'disabled' => $this->isDisabled(), |
| 83 |
] ), |
| 84 |
[ |
| 85 |
'label' => $opt['label'] ?? $optValue, |
| 86 |
'align' => 'inline', |
| 87 |
] |
| 88 |
); |
| 89 |
|
| 90 |
if ( $value === $optValue ) { |
| 91 |
$isValueAvailable = true; |
| 92 |
} |
| 93 |
|
| 94 |
$this->fields[] = $field; |
| 95 |
$this->appendContent( $field ); |
| 96 |
} |
| 97 |
|
| 98 |
// Restore the previous value, or reset to something sensible |
| 99 |
if ( $isValueAvailable ) { |
| 100 |
// Previous value is still available |
| 101 |
$this->setValue( $value ); |
| 102 |
} else { |
| 103 |
// No longer valid, reset |
| 104 |
if ( count( $options ) ) { |
| 105 |
$this->setValue( $options[0]['data'] ); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
/** @inheritDoc */ |
| 113 |
public function setDisabled( $disabled ) { |
| 114 |
parent::setDisabled( $disabled ); |
| 115 |
foreach ( $this->fields as $field ) { |
| 116 |
$field->getField()->setDisabled( $this->isDisabled() ); |
| 117 |
} |
| 118 |
return $this; |
| 119 |
} |
| 120 |
|
| 121 |
/** @inheritDoc */ |
| 122 |
public function getConfig( &$config ) { |
| 123 |
$options = []; |
| 124 |
foreach ( $this->fields as $field ) { |
| 125 |
$widget = $field->getField(); |
| 126 |
'@phan-var RadioInputWidget $widget'; |
| 127 |
$options[] = [ |
| 128 |
'data' => $widget->getValue(), |
| 129 |
'label' => $field->getLabel(), |
| 130 |
]; |
| 131 |
} |
| 132 |
$config['options'] = $options; |
| 133 |
return parent::getConfig( $config ); |
| 134 |
} |
| 135 |
} |