| 1 |
<?php |
| 2 |
|
| 3 |
namespace OOUI; |
| 4 |
|
| 5 |
/** |
| 6 |
* Combo box input widget, wrapping a text input with `<datalist>`. Intended to be used within a |
| 7 |
* OO.ui.FormLayout. |
| 8 |
*/ |
| 9 |
class ComboBoxInputWidget extends TextInputWidget { |
| 10 |
/** |
| 11 |
* HTML `<option>` tags for this widget, as Tags. |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
protected $options = []; |
| 15 |
|
| 16 |
/** @var bool */ |
| 17 |
protected $forceAutocomplete; |
| 18 |
/** @var Widget */ |
| 19 |
protected $downIndicator; |
| 20 |
/** @var Tag */ |
| 21 |
protected $datalist; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param array $config Configuration options |
| 25 |
* - array[] $config['options'] Array of menu options in the format |
| 26 |
* `[ 'data' => …, 'label' => … ]` |
| 27 |
*/ |
| 28 |
public function __construct( array $config = [] ) { |
| 29 |
// Parent constructor |
| 30 |
parent::__construct( $config ); |
| 31 |
|
| 32 |
// Initialization |
| 33 |
$this->forceAutocomplete = $config['autocomplete'] ?? false; |
| 34 |
$this->downIndicator = new IndicatorWidget( [ 'indicator' => 'down' ] ); |
| 35 |
$this->datalist = new Tag( 'datalist' ); |
| 36 |
$this->datalist->setAttributes( [ 'id' => Tag::generateElementId() ] ); |
| 37 |
$this->input->setAttributes( [ 'list' => $this->datalist->getAttribute( 'id' ) ] ); |
| 38 |
|
| 39 |
$this->setOptions( $config['options'] ?? [] ); |
| 40 |
$this->addClasses( [ 'oo-ui-comboBoxInputWidget', 'oo-ui-comboBoxInputWidget-php' ] ); |
| 41 |
$this->appendContent( $this->downIndicator, $this->datalist ); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Set the options available for this input. |
| 46 |
* |
| 47 |
* @param array[] $options Array of menu options in the format |
| 48 |
* `[ 'data' => …, 'label' => … ]` |
| 49 |
* @return $this |
| 50 |
*/ |
| 51 |
public function setOptions( $options ) { |
| 52 |
$this->options = []; |
| 53 |
|
| 54 |
$this->datalist->clearContent(); |
| 55 |
foreach ( $options as $opt ) { |
| 56 |
$option = ( new Tag( 'option' ) ) |
| 57 |
->setAttributes( [ 'value' => $opt['data'] ] ) |
| 58 |
->appendContent( $opt['label'] ?? $opt['data'] ); |
| 59 |
|
| 60 |
$this->options[] = $option; |
| 61 |
$this->datalist->appendContent( $option ); |
| 62 |
} |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
/** @inheritDoc */ |
| 68 |
public function getConfig( &$config ) { |
| 69 |
$options = []; |
| 70 |
foreach ( $this->options as $option ) { |
| 71 |
$options[] = [ |
| 72 |
'data' => $option->getAttribute( 'value' ), |
| 73 |
'label' => $option->content[0], |
| 74 |
]; |
| 75 |
} |
| 76 |
$config['options'] = $options; |
| 77 |
// JS ComboBoxInputWidget has `autocomplete: false` in the defaults. Make sure |
| 78 |
// explicitly passing `autocomplete: true` overrides that. Doing so doesn't make |
| 79 |
// much sense, this is just to make the tests happy. |
| 80 |
if ( $this->forceAutocomplete ) { |
| 81 |
$config['autocomplete'] = true; |
| 82 |
} |
| 83 |
$config['$overlay'] = true; |
| 84 |
return parent::getConfig( $config ); |
| 85 |
} |
| 86 |
} |