File: ComboBoxInputWidget.php

package info (click to toggle)
mediawiki 1%3A1.39.13-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 320,416 kB
  • sloc: php: 815,516; javascript: 601,264; sql: 11,218; python: 4,863; xml: 3,080; sh: 990; ruby: 82; makefile: 78
file content (89 lines) | stat: -rw-r--r-- 2,586 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace OOUI;

/**
 * Combo box input widget, wrapping a text input with `<datalist>`. Intended to be used within a
 * OO.ui.FormLayout.
 */
class ComboBoxInputWidget extends TextInputWidget {
	/**
	 * HTML `<option>` tags for this widget, as Tags.
	 * @var array
	 */
	protected $options = [];

	/** @var bool */
	protected $forceAutocomplete;
	/** @var Widget */
	protected $downIndicator;
	/** @var Tag */
	protected $datalist;

	/**
	 * @param array $config Configuration options
	 *      - array[] $config['options'] Array of menu options in the format
	 *          `[ 'data' => …, 'label' => … ]`
	 */
	public function __construct( array $config = [] ) {
		// ComboBoxInputWidget shouldn't support `multiline`
		$config['multiline'] = false;

		// Parent constructor
		parent::__construct( $config );

		// Initialization
		$this->forceAutocomplete = $config['autocomplete'] ?? false;
		$this->downIndicator = new IndicatorWidget( [ 'indicator' => 'down' ] );
		$this->datalist = new Tag( 'datalist' );
		$this->datalist->setAttributes( [ 'id' => Tag::generateElementId() ] );
		$this->input->setAttributes( [ 'list' => $this->datalist->getAttribute( 'id' ) ] );

		$this->setOptions( $config['options'] ?? [] );
		$this->addClasses( [ 'oo-ui-comboBoxInputWidget', 'oo-ui-comboBoxInputWidget-php' ] );
		$this->appendContent( $this->downIndicator, $this->datalist );
	}

	/**
	 * Set the options available for this input.
	 *
	 * @param array[] $options Array of menu options in the format
	 *   `[ 'data' => …, 'label' => … ]`
	 * @return $this
	 */
	public function setOptions( $options ) {
		$this->options = [];

		$this->datalist->clearContent();
		foreach ( $options as $opt ) {
			$option = ( new Tag( 'option' ) )
				->setAttributes( [ 'value' => $opt['data'] ] )
				->appendContent( $opt['label'] ?? $opt['data'] );

			$this->options[] = $option;
			$this->datalist->appendContent( $option );
		}

		return $this;
	}

	/** @inheritDoc */
	public function getConfig( &$config ) {
		$options = [];
		foreach ( $this->options as $option ) {
			$options[] = [
				'data' => $option->getAttribute( 'value' ),
				'label' => $option->content[0],
			];
		}
		$config['options'] = $options;
		// JS ComboBoxInputWidget has `autocomplete: false` in the defaults. Make sure
		// explicitly passing `autocomplete: true` overrides that. Doing so doesn't make
		// much sense, this is just to make the tests happy.
		if ( $this->forceAutocomplete ) {
			$config['autocomplete'] = true;
		}
		$config['$overlay'] = true;
		return parent::getConfig( $config );
	}
}