6 * A button that is an input widget. Intended to be used within a FormLayout.
8 class ButtonInputWidget extends InputWidget {
13 LabelElement::setLabel as setLabelElementLabel;
16 /* Static Properties */
18 public static $tagName = 'span';
23 * Whether to use `<input>` rather than `<button>`.
27 protected $useInputTag;
30 * @param array $config Configuration options
31 * @param string $config['type'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
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)
39 public function __construct( array $config = [] ) {
40 // Configuration initialization
41 $config = array_merge( [ 'type' => 'button', 'useInputTag' => false ], $config );
43 // Properties (must be set before parent constructor, which calls setValue())
44 $this->useInputTag = $config['useInputTag'];
47 parent::__construct( $config );
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 ] ) );
59 if ( !$config['useInputTag'] ) {
60 $this->input->appendContent( $this->icon, $this->label, $this->indicator );
63 $this->addClasses( [ 'oo-ui-buttonInputWidget' ] );
66 protected function getInputElement( $config ) {
67 $type = in_array( $config['type'], [ 'button', 'submit', 'reset' ] ) ?
70 $tag = $config['useInputTag'] ? 'input' : 'button';
71 return ( new Tag( $tag ) )->setAttributes( [ 'type' => $type ] );
77 * Overridden to support setting the 'value' of `<input>` elements.
79 * @param string|null $label Label text
82 public function setLabel( $label ) {
83 if ( $this->useInputTag ) {
84 // Discard non-plaintext labels
85 if ( !is_string( $label ) ) {
89 $this->input->setValue( $label );
92 return $this->setLabelElementLabel( $label );
96 * Set the value of the input.
98 * Overridden to disable for `<input>` elements, which have value identical to the label.
100 * @param string $value New value
103 public function setValue( $value ) {
104 if ( !$this->useInputTag ) {
105 parent::setValue( $value );
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.
116 public function getConfig( &$config ) {
117 if ( $this->useInputTag ) {
118 $config['useInputTag'] = true;
120 $config['type'] = $this->input->getAttribute( 'type' );
121 return parent::getConfig( $config );