yii2-bootstrap5/src/ToggleButtonGroup.php

140 lines
4.2 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 yii\base\InvalidConfigException;
/**
* ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
*
* You can use this widget in an [[yii\bootstrap5\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
* method, for example like this:
*
* ```php
* <?= $form->field($model, 'item_id')->widget(\yii\bootstrap5\ToggleButtonGroup::class, [
* 'type' => \yii\bootstrap5\ToggleButtonGroup::TYPE_CHECKBOX
* 'items' => [
* 'fooValue' => 'BarLabel',
* 'barValue' => 'BazLabel'
* ]
* ]) ?>
* ```
*
2021-08-05 15:21:05 +08:00
* @see https://getbootstrap.com/docs/5.1/components/buttons/#checkbox-and-radio-buttons
2021-02-10 05:04:59 +08:00
*
* @author Paul Klimov <klimov.paul@gmail.com>
* @author Simon Karlen <simi.albi@outlook.com>
*/
class ToggleButtonGroup extends InputWidget
{
/**
* Checkbox type
*/
const TYPE_CHECKBOX = 'checkbox';
2021-02-10 05:04:59 +08:00
/**
* Radio type
*/
const TYPE_RADIO = 'radio';
2021-02-10 05:04:59 +08:00
/**
* @var string input type, can be [[TYPE_CHECKBOX]] or [[TYPE_RADIO]]
*/
public $type;
2021-02-10 05:04:59 +08:00
/**
* @var array the data item used to generate the checkboxes.
* The array values are the labels, while the array keys are the corresponding checkbox or radio values.
*/
public $items = [];
2021-02-10 05:04:59 +08:00
/**
* @var array, the HTML attributes for the label (button) tag.
* @see Html::checkbox()
* @see Html::radio()
*/
public $labelOptions = [
2021-08-04 14:51:53 +08:00
'class' => ['btn', 'btn-outline-secondary'],
2021-02-10 05:04:59 +08:00
];
/**
* @var bool whether the items labels should be HTML-encoded.
*/
public $encodeLabels = true;
2021-02-10 05:04:59 +08:00
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
$this->registerPlugin('button');
2021-07-12 23:22:29 +08:00
Html::addCssClass($this->options, ['widget' => 'btn-group']);
$this->options['role'] = 'group';
2021-02-10 05:04:59 +08:00
}
/**
* {@inheritdoc}
* @return string
* @throws InvalidConfigException
*/
public function run(): string
{
if (!isset($this->options['item'])) {
$this->options['item'] = [$this, 'renderItem'];
}
switch ($this->type) {
case 'checkbox':
if ($this->hasModel()) {
return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::checkboxList($this->name, $this->value, $this->items, $this->options);
}
case 'radio':
if ($this->hasModel()) {
return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
} else {
return Html::radioList($this->name, $this->value, $this->items, $this->options);
}
default:
throw new InvalidConfigException("Unsupported type '{$this->type}'");
}
}
/**
* Default callback for checkbox/radio list item rendering.
* @param int $index item index.
* @param string $label item label.
* @param string $name input name.
* @param bool $checked whether value is checked or not.
* @param string $value input value.
* @return string generated HTML.
* @see Html::checkbox()
* @see Html::radio()
*/
public function renderItem(int $index, string $label, string $name, bool $checked, string $value): string
{
unset($index);
$labelOptions = $this->labelOptions;
2021-08-04 14:51:53 +08:00
$labelOptions['wrapInput'] = false;
2021-02-10 05:04:59 +08:00
Html::addCssClass($labelOptions, ['widget' => 'btn']);
$type = $this->type;
if ($this->encodeLabels) {
$label = Html::encode($label);
}
2021-08-04 14:51:53 +08:00
$options = [
2021-02-10 05:04:59 +08:00
'label' => $label,
'labelOptions' => $labelOptions,
'autocomplete' => 'off',
'value' => $value,
2021-08-04 14:51:53 +08:00
];
Html::addCssClass($options, ['widget' => 'btn-check']);
return Html::$type($name, $checked, $options);
2021-02-10 05:04:59 +08:00
}
}