Class yii\bootstrap5\ButtonDropdown
| Inheritance | yii\ |
|---|---|
| Uses Traits | yii\ |
| Source Code | https://github.com/yiisoft/yii2-bootstrap5/blob/master/src/ButtonDropdown.php |
ButtonDropdown renders a group or split button dropdown bootstrap component.
For example,
// a button group using Dropdown widget
echo ButtonDropdown::widget([
'label' => 'Action',
'dropdown' => [
'items' => [
['label' => 'DropdownA', 'url' => '/'],
['label' => 'DropdownB', 'url' => '#'],
],
],
]);
See also:
Public Properties
| Property | Type | Description | Defined By |
|---|---|---|---|
| $buttonOptions | array | The HTML attributes of the button. | yii\ |
| $clientEvents | array | The event handlers for the underlying Bootstrap JS plugin. | yii\ |
| $clientOptions | array|false | The options for the underlying Bootstrap JS plugin/component. | yii\ |
| $direction | string | The drop-direction of the widget Possible values are 'left', 'right', 'up', or 'down' (default) | yii\ |
| $dropdown | array | The configuration array for yii\ |
yii\ |
| $dropdownClass | string | Name of a class to use for rendering dropdowns withing this widget. | yii\ |
| $encodeLabel | boolean | Whether the label should be HTML-encoded. | yii\ |
| $label | string|null | The button label | yii\ |
| $options | array | The HTML attributes for the container tag. | yii\ |
| $renderContainer | boolean | Whether to render the container using the $options as HTML attributes. | yii\ |
| $split | boolean | Whether to display a group of split-styled button group. | yii\ |
| $tagName | string | The tag to use to render the button | yii\ |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| init() | yii\ |
|
| run() | yii\ |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| registerClientEvents() | Registers JS event handlers that are listed in $clientEvents. | yii\ |
| registerPlugin() | Registers a specific Bootstrap plugin/component and the related events. | yii\ |
| renderButton() | Generates the button dropdown. | yii\ |
| renderDropdown() | Generates the dropdown menu. | yii\ |
Constants
| Constant | Value | Description | Defined By |
|---|---|---|---|
| DIRECTION_DOWN | 'down' | The css class part of dropdown | yii\ |
| DIRECTION_LEFT | 'left' | The css class part of dropleft | yii\ |
| DIRECTION_RIGHT | 'right' | The css class part of dropright | yii\ |
| DIRECTION_UP | 'up' | The css class part of dropup | yii\ |
Property Details
The drop-direction of the widget
Possible values are 'left', 'right', 'up', or 'down' (default)
The configuration array for yii\
Name of a class to use for rendering dropdowns withing this widget. Defaults to yii\
Whether the label should be HTML-encoded.
The HTML attributes for the container tag. The following special options are recognized:
- tag: string, defaults to "div", the name of the container tag.
See also \
Whether to render the container using the $options as HTML attributes. If set to false,
the container element enclosing the button and dropdown will NOT be rendered.
Whether to display a group of split-styled button group.
Method Details
| public void init ( ) |
public function init(): void
{
parent::init();
if (!isset($this->buttonOptions['id'])) {
$this->buttonOptions['id'] = $this->options['id'] . '-button';
}
if ($this->label === null) {
$this->label = Yii::t('yii/bootstrap5', 'Button');
}
}
Defined in:
yii\
Registers JS event handlers that are listed in $clientEvents.
| protected void registerClientEvents ( ?string $name = null ) | ||
| $name | ?string | |
protected function registerClientEvents(?string $name = null): void
{
if (!empty($this->clientEvents)) {
$id = $this->options['id'];
$js = [];
$appendix = ($name === 'dropdown') ? '.parentElement' : '';
foreach ($this->clientEvents as $event => $handler) {
$js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);";
}
$this->getView()->registerJs(implode("\n" , $js));
}
}
Defined in:
yii\
Registers a specific Bootstrap plugin/component and the related events.
| protected void registerPlugin ( string $name ) | ||
| $name | string |
The name of the Bootstrap plugin |
protected function registerPlugin(string $name): void
{
/**
* @see https://github.com/twbs/bootstrap/blob/v5.2.0/js/index.esm.js
*/
$jsPlugins = [
'alert',
'button',
'carousel',
'collapse',
'dropdown',
'modal',
'offcanvas',
'popover',
'scrollspy',
'tab',
'toast',
'tooltip',
];
if (in_array($name, $jsPlugins, true)) {
$view = $this->getView();
BootstrapPluginAsset::register($view);
// 'popover', 'toast' and 'tooltip' plugins not activates via data attributes
if ($this->clientOptions !== false || in_array($name, ['popover', 'toast', 'tooltip'], true)) {
$name = ucfirst($name);
$id = $this->options['id'];
$options = empty($this->clientOptions) ? '{}' : Json::htmlEncode($this->clientOptions);
$view->registerJs("(new bootstrap.$name('#$id', $options));");
}
$this->registerClientEvents($name);
}
}
Generates the button dropdown.
| protected string renderButton ( ) | ||
| return | string |
The rendering result. |
|---|---|---|
| throws | Throwable | |
protected function renderButton(): string
{
Html::addCssClass($this->buttonOptions, [
'widget' => 'btn',
]);
$label = $this->label;
if ($this->encodeLabel) {
$label = Html::encode($label);
}
if ($this->split) {
$buttonOptions = $this->buttonOptions;
$this->buttonOptions['data'] = [
'bs-toggle' => 'dropdown',
];
$this->buttonOptions['aria'] = [
'expanded' => 'false',
];
Html::addCssClass($this->buttonOptions, [
'toggle' => 'dropdown-toggle dropdown-toggle-split',
]);
unset($buttonOptions['id']);
$splitButton = Button::widget([
'label' => '<span class="visually-hidden">' . Yii::t('yii/bootstrap5', 'Toggle Dropdown') . '</span>',
'encodeLabel' => false,
'options' => $this->buttonOptions,
'view' => $this->getView(),
]);
} else {
$buttonOptions = $this->buttonOptions;
Html::addCssClass($buttonOptions, [
'toggle' => 'dropdown-toggle',
]);
$buttonOptions['data'] = [
'bs-toggle' => 'dropdown',
];
$buttonOptions['aria'] = [
'expanded' => 'false',
];
$splitButton = '';
}
if (isset($buttonOptions['href'])) {
if (is_array($buttonOptions['href'])) {
$buttonOptions['href'] = Url::to($buttonOptions['href']);
}
} else {
if ($this->tagName === 'a') {
$buttonOptions['href'] = '#';
$buttonOptions['role'] = 'button';
}
}
return Button::widget([
'tagName' => $this->tagName,
'label' => $label,
'options' => $buttonOptions,
'clientOptions' => false,
'encodeLabel' => false,
'view' => $this->getView(),
]) . "\n" . $splitButton;
}
Generates the dropdown menu.
| protected string renderDropdown ( ) | ||
| return | string |
The rendering result. |
|---|---|---|
| throws | Throwable | |
protected function renderDropdown(): string
{
$config = $this->dropdown;
$config['clientOptions'] = [];
$config['view'] = $this->getView();
/** @var Widget $dropdownClass */
$dropdownClass = $this->dropdownClass;
return $dropdownClass::widget($config);
}
| public string run ( ) | ||
| throws | Throwable | |
|---|---|---|
public function run(): string
{
$html = $this->renderButton() . "\n" . $this->renderDropdown();
if ($this->renderContainer) {
Html::addCssClass($this->options, [
'widget' => 'drop' . $this->direction,
'btn-group',
]);
$options = $this->options;
$tag = ArrayHelper::remove($options, 'tag', 'div');
$html = Html::tag($tag, $html, $options);
}
// Set options id to button options id to ensure correct css selector in plugin initialisation
$this->options['id'] = $this->buttonOptions['id'];
$this->registerPlugin('dropdown');
return $html;
}