summaryrefslogtreecommitdiff
path: root/vendor/oojs/oojs-ui/php/widgets/RadioInputWidget.php
blob: 26da29d09a344034b121d21da4f72aba32232290 (plain)
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
<?php

namespace OOUI;

/**
 * Radio input widget.
 */
class RadioInputWidget extends InputWidget {

	/**
	 * @param array $config Configuration options
	 * @param boolean $config['selected'] Whether the radio button is initially selected
	 *   (default: false)
	 */
	public function __construct( array $config = array() ) {
		// Parent constructor
		parent::__construct( $config );

		// Initialization
		$this->addClasses( array( 'oo-ui-radioInputWidget' ) );
		$this->setSelected( isset( $config['selected'] ) ? $config['selected'] : false );
	}

	protected function getInputElement( $config ) {
		$input = new Tag( 'input' );
		$input->setAttributes( array( 'type' => 'radio' ) );
		return $input;
	}

	/**
	 * Set selection state of this radio button.
	 *
	 * @param boolean $state Whether the button is selected
	 */
	public function setSelected( $state ) {
		// RadioInputWidget doesn't track its state.
		if ( $state ) {
			$this->input->setAttributes( array( 'checked' => 'checked' ) );
		} else {
			$this->input->removeAttributes( array( 'checked' ) );
		}
		return $this;
	}

	/**
	 * Check if this radio button is selected.
	 *
	 * @return boolean Radio is selected
	 */
	public function isSelected() {
		return $this->input->getAttribute( 'checked' ) === 'checked';
	}

	public function getConfig( &$config ) {
		if ( $this->isSelected() ) {
			$config['selected'] = true;
		}
		return parent::getConfig( $config );
	}
}