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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
<?php
namespace OOUI;
/**
* A button that is an input widget. Intended to be used within a FormLayout.
*/
class ButtonInputWidget extends InputWidget {
use ButtonElement;
use IconElement;
use IndicatorElement;
use LabelElement {
LabelElement::setLabel as setLabelElementLabel;
}
use FlaggedElement;
/* Static Properties */
/** @var string */
public static $tagName = 'span';
/* Properties */
/**
* Whether to use `<input>` rather than `<button>`.
*
* @var bool
*/
protected $useInputTag;
/**
* @param array $config Configuration options
* - string $config['type'] HTML tag `type` attribute, may be 'button', 'submit' or 'reset'
* (default: 'button')
* - bool $config['useInputTag'] Whether to use `<input>` rather than `<button>`. Only
* useful if you need IE 6 support in a form with multiple buttons. If you use this
* option, icons and indicators will not be displayed, it won't be possible to have a
* non-plaintext label, and it won't be possible to set a value (which will internally
* become identical to the label). (default: false)
*/
public function __construct( array $config = [] ) {
// Configuration initialization
$config = array_merge( [ 'type' => 'button', 'useInputTag' => false ], $config );
// Properties (must be set before parent constructor, which calls setValue())
$this->useInputTag = $config['useInputTag'];
// Parent constructor
parent::__construct( $config );
// Traits
$this->initializeButtonElement(
array_merge( [ 'button' => $this->input ], $config )
);
$this->initializeIconElement( $config );
$this->initializeIndicatorElement( $config );
$this->initializeLabelElement( $config );
$this->initializeFlaggedElement(
array_merge( [ 'flagged' => $this ], $config )
);
// Initialization
if ( !$config['useInputTag'] ) {
$this->input->appendContent( $this->icon, $this->label, $this->indicator );
}
$this->addClasses( [ 'oo-ui-buttonInputWidget' ] );
}
/** @inheritDoc */
protected function getInputElement( $config ) {
$type = in_array( $config['type'], [ 'button', 'submit', 'reset' ] ) ?
$config['type'] :
'button';
$tag = $config['useInputTag'] ? 'input' : 'button';
return ( new Tag( $tag ) )->setAttributes( [ 'type' => $type ] );
}
/**
* Set label value.
*
* Overridden to support setting the 'value' of `<input>` elements.
*
* @param string|null $label Label text
* @return $this
*/
public function setLabel( $label ) {
if ( $this->useInputTag ) {
// Discard non-plaintext labels
if ( !is_string( $label ) ) {
$label = '';
}
$this->input->setValue( $label );
}
return $this->setLabelElementLabel( $label );
}
/**
* Set the value of the input.
*
* Overridden to disable for `<input>` elements, which have value identical to the label.
*
* @param mixed $value New value should be a string
* @return $this
*/
public function setValue( $value ) {
if ( !$this->useInputTag ) {
parent::setValue( $value );
}
return $this;
}
/**
* @return null
*/
public function getInputId() {
// Disable generating `<label>` elements for buttons. One would very rarely need additional label
// for a button, and it's already a big clickable target, and it causes unexpected rendering.
return null;
}
/** @inheritDoc */
public function getConfig( &$config ) {
if ( $this->useInputTag ) {
$config['useInputTag'] = true;
}
$config['type'] = $this->input->getAttribute( 'type' );
return parent::getConfig( $config );
}
}
|