]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/oojs/oojs-ui/php/widgets/ButtonInputWidget.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / oojs / oojs-ui / php / widgets / ButtonInputWidget.php
1 <?php
2
3 namespace OOUI;
4
5 /**
6  * A button that is an input widget. Intended to be used within a FormLayout.
7  */
8 class ButtonInputWidget extends InputWidget {
9         use ButtonElement;
10         use IconElement;
11         use IndicatorElement;
12         use LabelElement {
13                 LabelElement::setLabel as setLabelElementLabel;
14         }
15
16         /* Static Properties */
17
18         public static $tagName = 'span';
19
20         /* Properties */
21
22         /**
23          * Whether to use `<input>` rather than `<button>`.
24          *
25          * @var boolean
26          */
27         protected $useInputTag;
28
29         /**
30          * @param array $config Configuration options
31          * @param string $config['type'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
32          *   (default: 'button')
33          * @param bool $config['useInputTag'] Whether to use `<input>` rather than `<button>`. Only
34          *   useful if you need IE 6 support in a form with multiple buttons. If you use this option,
35          *   icons and indicators will not be displayed, it won't be possible to have a non-plaintext
36          *   label, and it won't be possible to set a value (which will internally become identical to the
37          *   label). (default: false)
38          */
39         public function __construct( array $config = [] ) {
40                 // Configuration initialization
41                 $config = array_merge( [ 'type' => 'button', 'useInputTag' => false ], $config );
42
43                 // Properties (must be set before parent constructor, which calls setValue())
44                 $this->useInputTag = $config['useInputTag'];
45
46                 // Parent constructor
47                 parent::__construct( $config );
48
49                 // Traits
50                 $this->initializeButtonElement(
51                         array_merge( $config, [ 'button' => $this->input ] ) );
52                 $this->initializeIconElement( $config );
53                 $this->initializeIndicatorElement( $config );
54                 $this->initializeLabelElement( $config );
55                 $this->initializeTitledElement(
56                         array_merge( $config, [ 'titled' => $this->input ] ) );
57
58                 // Initialization
59                 if ( !$config['useInputTag'] ) {
60                         $this->input->appendContent( $this->icon, $this->label, $this->indicator );
61                 }
62
63                 $this->addClasses( [ 'oo-ui-buttonInputWidget' ] );
64         }
65
66         protected function getInputElement( $config ) {
67                 $type = in_array( $config['type'], [ 'button', 'submit', 'reset' ] ) ?
68                         $config['type'] :
69                         'button';
70                 $tag = $config['useInputTag'] ? 'input' : 'button';
71                 return ( new Tag( $tag ) )->setAttributes( [ 'type' => $type ] );
72         }
73
74         /**
75          * Set label value.
76          *
77          * Overridden to support setting the 'value' of `<input>` elements.
78          *
79          * @param string|null $label Label text
80          * @return $this
81          */
82         public function setLabel( $label ) {
83                 if ( $this->useInputTag ) {
84                         // Discard non-plaintext labels
85                         if ( !is_string( $label ) ) {
86                                 $label = '';
87                         }
88
89                         $this->input->setValue( $label );
90                 }
91
92                 return $this->setLabelElementLabel( $label );
93         }
94
95         /**
96          * Set the value of the input.
97          *
98          * Overridden to disable for `<input>` elements, which have value identical to the label.
99          *
100          * @param string $value New value
101          * @return $this
102          */
103         public function setValue( $value ) {
104                 if ( !$this->useInputTag ) {
105                         parent::setValue( $value );
106                 }
107                 return $this;
108         }
109
110         public function getInputId() {
111                 // Disable generating `<label>` elements for buttons. One would very rarely need additional label
112                 // for a button, and it's already a big clickable target, and it causes unexpected rendering.
113                 return null;
114         }
115
116         public function getConfig( &$config ) {
117                 if ( $this->useInputTag ) {
118                         $config['useInputTag'] = true;
119                 }
120                 $config['type'] = $this->input->getAttribute( 'type' );
121                 return parent::getConfig( $config );
122         }
123 }