You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.4KB

  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\helpers\Html;
  9. /**
  10. * ButtonGroup renders a button group bootstrap component.
  11. *
  12. * For example,
  13. *
  14. * ```php
  15. * // a button group with items configuration
  16. * echo ButtonGroup::widget([
  17. * 'buttons' => [
  18. * ['label' => 'A'],
  19. * ['label' => 'B'],
  20. * ]
  21. * ]);
  22. *
  23. * // button group with an item as a string
  24. * echo ButtonGroup::widget([
  25. * 'buttons' => [
  26. * Button::widget(['label' => 'A']),
  27. * ['label' => 'B'],
  28. * ]
  29. * ]);
  30. * ```
  31. * @see http://getbootstrap.com/javascript/#buttons
  32. * @see http://getbootstrap.com/components/#btn-groups
  33. * @author Antonio Ramirez <amigo.cobos@gmail.com>
  34. * @since 2.0
  35. */
  36. class ButtonGroup extends Widget
  37. {
  38. /**
  39. * @var array list of buttons. Each array element represents a single button
  40. * which can be specified as a string or an array of the following structure:
  41. *
  42. * - label: string, required, the button label.
  43. * - options: array, optional, the HTML attributes of the button.
  44. */
  45. public $buttons = [];
  46. /**
  47. * @var boolean whether to HTML-encode the button labels.
  48. */
  49. public $encodeLabels = true;
  50. /**
  51. * Initializes the widget.
  52. * If you override this method, make sure you call the parent implementation first.
  53. */
  54. public function init()
  55. {
  56. parent::init();
  57. Html::addCssClass($this->options, 'btn-group');
  58. }
  59. /**
  60. * Renders the widget.
  61. */
  62. public function run()
  63. {
  64. BootstrapAsset::register($this->getView());
  65. return Html::tag('div', $this->renderButtons(), $this->options);
  66. }
  67. /**
  68. * Generates the buttons that compound the group as specified on [[buttons]].
  69. * @return string the rendering result.
  70. */
  71. protected function renderButtons()
  72. {
  73. $buttons = [];
  74. foreach ($this->buttons as $button) {
  75. if (is_array($button)) {
  76. $button['view'] = $this->getView();
  77. if (!isset($button['encodeLabel'])) {
  78. $button['encodeLabel'] = $this->encodeLabels;
  79. }
  80. $buttons[] = Button::widget($button);
  81. } else {
  82. $buttons[] = $button;
  83. }
  84. }
  85. return implode("\n", $buttons);
  86. }
  87. }