1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
8
|
|
|
use Yiisoft\Factory\Exceptions\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
|
|
|
* ->withButtons([ |
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
|
|
|
* ->withButtons([ |
31
|
|
|
* Button::widget() |
32
|
|
|
* ->label('A'), |
33
|
|
|
* ['label' => 'B'], |
34
|
|
|
* ]); |
35
|
|
|
* ``` |
36
|
|
|
* |
37
|
|
|
* Pressing on the button should be handled via JavaScript. See the following for details: |
38
|
|
|
*/ |
39
|
|
|
final class ButtonGroup extends Widget |
40
|
|
|
{ |
41
|
|
|
private array $buttons = []; |
42
|
|
|
private bool $encodeLabels = true; |
43
|
|
|
private bool $encodeTags = false; |
44
|
|
|
private array $options = []; |
45
|
5 |
|
|
46
|
|
|
public function run(): string |
47
|
5 |
|
{ |
48
|
5 |
|
if (!isset($this->options['id'])) { |
49
|
|
|
$this->options['id'] = "{$this->getId()}-button-group"; |
50
|
|
|
} |
51
|
|
|
|
52
|
5 |
|
/** @psalm-suppress InvalidArgument */ |
53
|
|
|
Html::addCssClass($this->options, ['widget' => 'btn-group']); |
54
|
5 |
|
|
55
|
5 |
|
if (!isset($this->options['role'])) { |
56
|
|
|
$this->options['role'] = 'group'; |
57
|
|
|
} |
58
|
5 |
|
|
59
|
|
|
if ($this->encodeTags === false) { |
60
|
|
|
$this->options = array_merge($this->options, ['encode' => false]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return Html::div($this->renderButtons(), $this->options); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* List of buttons. Each array element represents a single button which can be specified as a string or an array of |
68
|
5 |
|
* the following structure: |
69
|
|
|
* |
70
|
5 |
|
* - label: string, required, the button label. |
71
|
|
|
* - options: array, optional, the HTML attributes of the button. |
72
|
5 |
|
* - visible: bool, optional, whether this button is visible. Defaults to true. |
73
|
5 |
|
* |
74
|
5 |
|
* @param array $value |
75
|
|
|
* |
76
|
5 |
|
* @return $this |
77
|
1 |
|
*/ |
78
|
|
|
public function withButtons(array $value): self |
79
|
|
|
{ |
80
|
5 |
|
$new = clone $this; |
81
|
5 |
|
$new->buttons = $value; |
82
|
|
|
|
83
|
|
|
return $new; |
84
|
5 |
|
} |
85
|
4 |
|
|
86
|
|
|
/** |
87
|
|
|
* Whether to HTML-encode the button labels. |
88
|
5 |
|
* |
89
|
5 |
|
* @param bool $value |
90
|
5 |
|
* |
91
|
5 |
|
* @return $this |
92
|
5 |
|
*/ |
93
|
|
|
public function withoutEncodeLabels(bool $value = false): self |
94
|
1 |
|
{ |
95
|
|
|
$new = clone $this; |
96
|
|
|
$new->encodeLabels = $value; |
97
|
|
|
|
98
|
5 |
|
return $new; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
103
|
|
|
* |
104
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
105
|
|
|
* |
106
|
|
|
* @param array $value |
107
|
|
|
* |
108
|
|
|
* @return $this |
109
|
|
|
*/ |
110
|
|
|
public function withOptions(array $value): self |
111
|
|
|
{ |
112
|
|
|
$new = clone $this; |
113
|
5 |
|
$new->options = $value; |
114
|
|
|
|
115
|
5 |
|
return $new; |
116
|
|
|
} |
117
|
5 |
|
|
118
|
|
|
/** |
119
|
|
|
* Allows you to enable or disable the encoding tags html. |
120
|
|
|
* |
121
|
|
|
* @param bool $value |
122
|
|
|
* |
123
|
|
|
* @return self |
124
|
|
|
*/ |
125
|
|
|
public function withEncodeTags(bool $value = true): self |
126
|
|
|
{ |
127
|
|
|
$new = clone $this; |
128
|
|
|
$new->encodeTags = $value; |
129
|
|
|
|
130
|
|
|
return $new; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Generates the buttons that compound the group as specified on {@see buttons}. |
135
|
|
|
* |
136
|
|
|
* @throws InvalidConfigException |
137
|
|
|
* |
138
|
|
|
* @return string the rendering result. |
139
|
|
|
*/ |
140
|
|
|
private function renderButtons(): string |
141
|
|
|
{ |
142
|
|
|
$buttons = []; |
143
|
3 |
|
|
144
|
|
|
foreach ($this->buttons as $button) { |
145
|
3 |
|
if (is_array($button)) { |
146
|
|
|
$visible = ArrayHelper::remove($button, 'visible', true); |
147
|
3 |
|
|
148
|
|
|
if ($visible === false) { |
149
|
|
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if (!isset($button['encodeLabel'])) { |
153
|
|
|
$button['encodeLabel'] = $this->encodeLabels; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!isset($button['options']['type'])) { |
157
|
|
|
ArrayHelper::setValueByPath($button, 'options.type', 'button'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$buttons[] = Button::widget() |
161
|
|
|
->withoutEncodeLabels($button['encodeLabel']) |
|
|
|
|
162
|
|
|
->withLabel($button['label']) |
163
|
|
|
->withOptions($button['options']) |
164
|
|
|
->render(); |
165
|
|
|
} else { |
166
|
|
|
$buttons[] = $button; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return implode("\n", $buttons); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|