ToggleButtonGroup.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use yii\base\InvalidConfigException;
  9. /**
  10. * ToggleButtonGroup allows rendering form inputs Checkbox/Radio toggle button groups.
  11. *
  12. * You can use this widget in an [[yii\bootstrap\ActiveForm|ActiveForm]] using the [[yii\widgets\ActiveField::widget()|widget()]]
  13. * method, for example like this:
  14. *
  15. * ```php
  16. * <?= $form->field($model, 'item_id')->widget(\yii\bootstrap\ToggleButtonGroup::classname(), [
  17. * // configure additional widget properties here
  18. * ]) ?>
  19. * ```
  20. *
  21. * @see http://getbootstrap.com/javascript/#buttons-checkbox-radio
  22. *
  23. * @author Paul Klimov <klimov.paul@gmail.com>
  24. * @since 2.0.6
  25. */
  26. class ToggleButtonGroup extends InputWidget
  27. {
  28. /**
  29. * @var string input type, can be:
  30. * - 'checkbox'
  31. * - 'radio'
  32. */
  33. public $type;
  34. /**
  35. * @var array the data item used to generate the checkboxes.
  36. * The array values are the labels, while the array keys are the corresponding checkbox or radio values.
  37. */
  38. public $items = [];
  39. /**
  40. * @var array, the HTML attributes for the label (button) tag.
  41. * @see Html::checkbox()
  42. * @see Html::radio()
  43. */
  44. public $labelOptions = [];
  45. /**
  46. * @var bool whether the items labels should be HTML-encoded.
  47. */
  48. public $encodeLabels = true;
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function init()
  53. {
  54. parent::init();
  55. $this->registerPlugin('button');
  56. Html::addCssClass($this->options, 'btn-group');
  57. $this->options['data-toggle'] = 'buttons';
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function run()
  63. {
  64. if (!isset($this->options['item'])) {
  65. $this->options['item'] = [$this, 'renderItem'];
  66. }
  67. switch ($this->type) {
  68. case 'checkbox':
  69. if ($this->hasModel()) {
  70. return Html::activeCheckboxList($this->model, $this->attribute, $this->items, $this->options);
  71. } else {
  72. return Html::checkboxList($this->name, $this->value, $this->items, $this->options);
  73. }
  74. case 'radio':
  75. if ($this->hasModel()) {
  76. return Html::activeRadioList($this->model, $this->attribute, $this->items, $this->options);
  77. } else {
  78. return Html::radioList($this->name, $this->value, $this->items, $this->options);
  79. }
  80. default:
  81. throw new InvalidConfigException("Unsupported type '{$this->type}'");
  82. }
  83. }
  84. /**
  85. * Default callback for checkbox/radio list item rendering.
  86. * @param int $index item index.
  87. * @param string $label item label.
  88. * @param string $name input name.
  89. * @param bool $checked whether value is checked or not.
  90. * @param string $value input value.
  91. * @return string generated HTML.
  92. * @see Html::checkbox()
  93. * @see Html::radio()
  94. */
  95. public function renderItem($index, $label, $name, $checked, $value)
  96. {
  97. $labelOptions = $this->labelOptions;
  98. Html::addCssClass($labelOptions, 'btn');
  99. if ($checked) {
  100. Html::addCssClass($labelOptions, 'active');
  101. }
  102. $type = $this->type;
  103. if ($this->encodeLabels) {
  104. $label = Html::encode($label);
  105. }
  106. return Html::$type($name, $checked, ['label' => $label, 'labelOptions' => $labelOptions, 'value' => $value]);
  107. }
  108. }