yii2-bootstrap5/src/ButtonToolbar.php

119 lines
3.5 KiB
PHP
Raw Normal View History

2021-02-10 05:04:59 +08:00
<?php
2021-08-07 05:38:53 +08:00
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
2021-02-10 05:04:59 +08:00
declare(strict_types=1);
namespace yii\bootstrap5;
use Throwable;
2021-02-10 05:04:59 +08:00
/**
* ButtonToolbar Combines sets of button groups into button toolbars for more complex components.
* Use utility classes as needed to space out groups, buttons, and more.
*
* For example,
*
* ```php
* // a button toolbar with items configuration
* echo ButtonToolbar::widget([
* 'buttonGroups' => [
* [
* 'buttons' => [
* ['label' => '1', 'options' => ['class' => ['btn-secondary']]],
* ['label' => '2', 'options' => ['class' => ['btn-secondary']]],
* ['label' => '3', 'options' => ['class' => ['btn-secondary']]],
* ['label' => '4', 'options' => ['class' => ['btn-secondary']]]
* ],
* 'class' => ['mr-2']
* ],
* [
* 'buttons' => [
* ['label' => '5', 'options' => ['class' => ['btn-secondary']]],
* ['label' => '6', 'options' => ['class' => ['btn-secondary']]],
* ['label' => '7', 'options' => ['class' => ['btn-secondary']]]
* ],
* 'class' => ['mr-2']
* ],
* [
* 'buttons' => [
* ['label' => '8', 'options' => ['class' => ['btn-secondary']]]
* ]
* ]
* ]
* ]);
* ```
*
* Pressing on the button should be handled via JavaScript. See the following for details:
*
2021-08-05 15:21:05 +08:00
* @see https://getbootstrap.com/docs/5.1/components/buttons/
* @see https://getbootstrap.com/docs/5.1/components/button-group/#button-toolbar
2021-02-10 05:04:59 +08:00
*
* @author Simon Karlen <simi.albi@outlook.com>
*/
class ButtonToolbar extends Widget
{
/**
* @var array list of buttons groups. Each array element represents a single group
* which can be specified as a string or an array of the following structure:
*
* - buttons: array list of buttons. Either as array or string representation
* - options: array optional, the HTML attributes of the button group.
* - encodeLabels: bool whether to HTML-encode the button labels.
*/
public $buttonGroups = [];
2021-02-10 05:04:59 +08:00
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, ['widget' => 'btn-toolbar']);
if (!isset($this->options['role'])) {
$this->options['role'] = 'toolbar';
}
}
/**
* {@inheritdoc}
* @return string
* @throws Throwable
2021-02-10 05:04:59 +08:00
*/
public function run(): string
{
BootstrapAsset::register($this->getView());
2021-02-10 05:04:59 +08:00
return Html::tag('div', $this->renderButtonGroups(), $this->options);
}
/**
* Generates the button groups that compound the toolbar as specified on [[buttonGroups]].
* @return string the rendering result.
* @throws Throwable
2021-02-10 05:04:59 +08:00
*/
protected function renderButtonGroups(): string
{
$buttonGroups = [];
foreach ($this->buttonGroups as $group) {
if (is_array($group)) {
$group['view'] = $this->getView();
if (!isset($group['buttons'])) {
continue;
}
$buttonGroups[] = ButtonGroup::widget($group);
} else {
$buttonGroups[] = $group;
}
}
return implode("\n", $buttonGroups);
}
}