Debian Codegraph @

vendor/oojs/oojs-ui/php/widgets/ButtonInputWidget.php

raw
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 use FlaggedElement;
16
17 /* Static Properties */
18
19 /** @var string */
20 public static $tagName = 'span';
21
22 /* Properties */
23
24 /**
25 * Whether to use `<input>` rather than `<button>`.
26 *
27 * @var bool
28 */
29 protected $useInputTag;
30
31 /**
32 * Whether to use `formnovalidate` attribute.
33 *
34 * @var bool
35 */
36 protected $formNoValidate;
37
38 /**
39 * @param array $config Configuration options
40 * - string $config['type'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
41 * (default: 'button')
42 * - bool $config['useInputTag'] Whether to use `<input>` rather than `<button>`. Only
43 * useful if you need IE 6 support in a form with multiple buttons. If you use this
44 * option, icons and indicators will not be displayed, it won't be possible to have a
45 * non-plaintext label, and it won't be possible to set a value (which will internally
46 * become identical to the label). (default: false)
47 * - bool $config['formNoValidate'] Whether to use `formnovalidate` attribute.
48 */
49 public function __construct( array $config = [] ) {
50 // Configuration initialization
51 $config = array_merge( [ 'type' => 'button', 'useInputTag' => false, 'formNoValidate' => false ], $config );
52
53 // Properties (must be set before parent constructor, which calls setValue())
54 $this->useInputTag = $config['useInputTag'];
55
56 // Parent constructor
57 parent::__construct( $config );
58
59 // Traits
60 $this->initializeButtonElement(
61 array_merge( [ 'button' => $this->input ], $config )
62 );
63 $this->initializeIconElement( $config );
64 $this->initializeIndicatorElement( $config );
65 $this->initializeLabelElement( $config );
66 $this->initializeFlaggedElement(
67 array_merge( [ 'flagged' => $this ], $config )
68 );
69
70 // Initialization
71 if ( !$config['useInputTag'] ) {
72 $this->input->appendContent( $this->icon, $this->label, $this->indicator );
73 }
74
75 if ( $config['formNoValidate'] ) {
76 $this->input->setAttributes( [
77 'formnovalidate' => 'formnovalidate'
78 ] );
79 }
80
81 $this->addClasses( [ 'oo-ui-buttonInputWidget' ] );
82 }
83
84 /** @inheritDoc */
85 protected function getInputElement( $config ) {
86 $type = in_array( $config['type'], [ 'button', 'submit', 'reset' ] ) ?
87 $config['type'] :
88 'button';
89 $tag = $config['useInputTag'] ? 'input' : 'button';
90 return ( new Tag( $tag ) )->setAttributes( [ 'type' => $type ] );
91 }
92
93 /**
94 * Set label value.
95 *
96 * Overridden to support setting the 'value' of `<input>` elements.
97 *
98 * @param string|null $label Label text
99 * @return $this
100 */
101 public function setLabel( $label ) {
102 if ( $this->useInputTag ) {
103 // Discard non-plaintext labels
104 if ( !is_string( $label ) ) {
105 $label = '';
106 }
107
108 $this->input->setValue( $label );
109 }
110
111 return $this->setLabelElementLabel( $label );
112 }
113
114 /**
115 * Set the value of the input.
116 *
117 * Overridden to disable for `<input>` elements, which have value identical to the label.
118 *
119 * @param mixed $value New value should be a string
120 * @return $this
121 */
122 public function setValue( $value ) {
123 if ( !$this->useInputTag ) {
124 parent::setValue( $value );
125 }
126 return $this;
127 }
128
129 /**
130 * @return null
131 */
132 public function getInputId() {
133 // Disable generating `<label>` elements for buttons. One would very rarely need additional label
134 // for a button, and it's already a big clickable target, and it causes unexpected rendering.
135 return null;
136 }
137
138 /** @inheritDoc */
139 public function getConfig( &$config ) {
140 if ( $this->useInputTag ) {
141 $config['useInputTag'] = true;
142 }
143 $config['type'] = $this->input->getAttribute( 'type' );
144 if ( $this->input->getAttribute( 'formnovalidate' ) ) {
145 $config['formNoValidate'] = true;
146 }
147 return parent::getConfig( $config );
148 }
149 }