1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Yii\Bootstrap5; |
||
6 | |||
7 | use Yiisoft\Arrays\ArrayHelper; |
||
8 | use Yiisoft\Definitions\Exception\InvalidConfigException; |
||
9 | use Yiisoft\Html\Html; |
||
10 | |||
11 | use function implode; |
||
12 | use function is_array; |
||
13 | |||
14 | /** |
||
15 | * ButtonGroup renders a button group bootstrap component. |
||
16 | * |
||
17 | * For example, |
||
18 | * |
||
19 | * ```php |
||
20 | * // a button group with items configuration |
||
21 | * echo ButtonGroup::widget() |
||
22 | * ->buttons([ |
||
23 | * ['label' => 'A'], |
||
24 | * ['label' => 'B'], |
||
25 | * ['label' => 'C', 'visible' => false], |
||
26 | * ]); |
||
27 | * |
||
28 | * // button group with an item as a string |
||
29 | * echo ButtonGroup::widget() |
||
30 | * ->buttons([ |
||
31 | * Button::widget()->label('A'), |
||
32 | * ['label' => 'B'], |
||
33 | * ]); |
||
34 | * ``` |
||
35 | * |
||
36 | * Pressing on the button should be handled via JavaScript. See the following for details: |
||
37 | */ |
||
38 | final class ButtonGroup extends Widget |
||
39 | { |
||
40 | private array $buttons = []; |
||
41 | private bool $encodeLabels = true; |
||
42 | private bool $encodeTags = false; |
||
43 | private array $options = []; |
||
44 | |||
45 | 7 | public function render(): string |
|
46 | { |
||
47 | 7 | if (!isset($this->options['id'])) { |
|
48 | 7 | $this->options['id'] = "{$this->getId()}-button-group"; |
|
49 | } |
||
50 | |||
51 | /** @psalm-suppress InvalidArgument */ |
||
52 | 7 | Html::addCssClass($this->options, ['widget' => 'btn-group']); |
|
53 | |||
54 | 7 | if (!isset($this->options['role'])) { |
|
55 | 7 | $this->options['role'] = 'group'; |
|
56 | } |
||
57 | |||
58 | 7 | return Html::div($this->renderButtons(), $this->options) |
|
59 | 7 | ->encode($this->encodeTags) |
|
60 | 7 | ->render(); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * List of buttons. Each array element represents a single button which can be specified as a string or an array of |
||
65 | * the following structure: |
||
66 | * |
||
67 | * - label: string, required, the button label. |
||
68 | * - options: array, optional, the HTML attributes of the button. |
||
69 | * - visible: bool, optional, whether this button is visible. Defaults to true. |
||
70 | */ |
||
71 | 7 | public function buttons(array $value): self |
|
72 | { |
||
73 | 7 | $new = clone $this; |
|
74 | 7 | $new->buttons = $value; |
|
75 | |||
76 | 7 | return $new; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * When tags Labels HTML should not be encoded. |
||
81 | */ |
||
82 | 1 | public function withoutEncodeLabels(): self |
|
83 | { |
||
84 | 1 | $new = clone $this; |
|
85 | 1 | $new->encodeLabels = false; |
|
86 | |||
87 | 1 | return $new; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * The HTML attributes for the widget container tag. The following special options are recognized. |
||
92 | * |
||
93 | * {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
||
94 | */ |
||
95 | 4 | public function options(array $value): self |
|
96 | { |
||
97 | 4 | $new = clone $this; |
|
98 | 4 | $new->options = $value; |
|
99 | |||
100 | 4 | return $new; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Generates the buttons that compound the group as specified on {@see buttons}. |
||
105 | * |
||
106 | * @throws InvalidConfigException |
||
107 | * |
||
108 | * @return string the rendering result. |
||
109 | */ |
||
110 | 7 | private function renderButtons(): string |
|
111 | { |
||
112 | 7 | $buttons = []; |
|
113 | |||
114 | 7 | foreach ($this->buttons as $button) { |
|
115 | 7 | if (is_array($button)) { |
|
116 | 7 | $visible = ArrayHelper::remove($button, 'visible', true); |
|
117 | |||
118 | 7 | if ($visible === false) { |
|
119 | 1 | continue; |
|
120 | } |
||
121 | |||
122 | 7 | if (!isset($button['encodeLabel'])) { |
|
123 | 7 | $button['encodeLabel'] = $this->encodeLabels; |
|
124 | } |
||
125 | |||
126 | 7 | if (!isset($button['options']['type'])) { |
|
127 | 6 | ArrayHelper::setValueByPath($button, 'options.type', 'button'); |
|
128 | } |
||
129 | |||
130 | 7 | $buttonWidget = Button::widget() |
|
131 | 7 | ->label($button['label']) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
132 | 7 | ->options($button['options']); |
|
133 | |||
134 | 7 | if ($button['encodeLabel'] === false) { |
|
135 | 1 | $buttonWidget = $buttonWidget->withoutEncodeLabels(); |
|
136 | } |
||
137 | |||
138 | 7 | $buttons[] = $buttonWidget->render(); |
|
139 | } else { |
||
140 | 1 | $buttons[] = $button; |
|
141 | } |
||
142 | } |
||
143 | |||
144 | 7 | return implode("\n", $buttons); |
|
145 | } |
||
146 | } |
||
147 |