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
|
|
|
/** |
12
|
|
|
* ButtonDropdown renders a group or split button dropdown bootstrap component. |
13
|
|
|
* |
14
|
|
|
* For example, |
15
|
|
|
* |
16
|
|
|
* ```php |
17
|
|
|
* // a button group using Dropdown widget |
18
|
|
|
* echo ButtonDropdown::widget() |
19
|
|
|
* ->label('Action') |
20
|
|
|
* ->dropdown'([ |
21
|
|
|
* 'items' => [ |
22
|
|
|
* ['label' => 'DropdownA', 'url' => '/'], |
23
|
|
|
* ['label' => 'DropdownB', 'url' => '#'], |
24
|
|
|
* ], |
25
|
|
|
* ]); |
26
|
|
|
* ``` |
27
|
|
|
*/ |
28
|
|
|
final class ButtonDropdown extends Widget |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* The css class part of dropdown |
32
|
|
|
*/ |
33
|
|
|
public const DIRECTION_DOWN = 'down'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The css class part of dropleft |
37
|
|
|
*/ |
38
|
|
|
public const DIRECTION_LEFT = 'left'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The css class part of dropright |
42
|
|
|
*/ |
43
|
|
|
public const DIRECTION_RIGHT = 'right'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The css class part of dropup |
47
|
|
|
*/ |
48
|
|
|
public const DIRECTION_UP = 'up'; |
49
|
|
|
|
50
|
|
|
private string $label = 'Button'; |
51
|
|
|
private array $options = []; |
52
|
|
|
private array $buttonOptions = []; |
53
|
|
|
private array $dropdown = []; |
54
|
|
|
private string $direction = self::DIRECTION_DOWN; |
55
|
|
|
private bool $split = false; |
56
|
|
|
private string $tagName = 'button'; |
57
|
|
|
private bool $encodeLabels = true; |
58
|
|
|
private bool $encodeTags = false; |
59
|
|
|
private string $dropdownClass = Dropdown::class; |
60
|
|
|
private bool $renderContainer = true; |
61
|
|
|
|
62
|
11 |
|
public function run(): string |
63
|
|
|
{ |
64
|
11 |
|
if (!isset($this->dropdown['items'])) { |
65
|
1 |
|
return ''; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** Set options id to button options id to ensure correct css selector in plugin initialisation */ |
69
|
10 |
|
if (empty($this->options['id'])) { |
70
|
10 |
|
$id = $this->getId(); |
71
|
|
|
|
72
|
10 |
|
$this->options['id'] = "{$id}-button-dropdown"; |
73
|
10 |
|
$this->buttonOptions['id'] = "{$id}-button"; |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
if ($this->encodeTags === false) { |
77
|
9 |
|
$this->options = array_merge($this->options, ['encode' => false]); |
78
|
|
|
} |
79
|
|
|
|
80
|
10 |
|
$html = $this->renderButton() . "\n" . $this->renderDropdown(); |
81
|
|
|
|
82
|
10 |
|
if ($this->renderContainer) { |
83
|
|
|
/** @psalm-suppress InvalidArgument */ |
84
|
9 |
|
Html::addCssClass($this->options, ['widget' => 'drop' . $this->direction, 'btn-group']); |
85
|
|
|
|
86
|
9 |
|
$options = $this->options; |
87
|
9 |
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
88
|
9 |
|
$html = Html::tag($tag, $html, $options); |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
return $html; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* The HTML attributes of the button. |
96
|
|
|
* |
97
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
98
|
|
|
* |
99
|
|
|
* @param array $value |
100
|
|
|
* |
101
|
|
|
* @return $this |
102
|
|
|
*/ |
103
|
1 |
|
public function withButtonOptions(array $value): self |
104
|
|
|
{ |
105
|
1 |
|
$new = clone $this; |
106
|
1 |
|
$new->buttonOptions = $value; |
107
|
|
|
|
108
|
1 |
|
return $new; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* The drop-direction of the widget. |
113
|
|
|
* |
114
|
|
|
* Possible values are 'left', 'right', 'up', or 'down' (default) |
115
|
|
|
* |
116
|
|
|
* @param string $value |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
2 |
|
public function withDirection(string $value): self |
121
|
|
|
{ |
122
|
2 |
|
$new = clone $this; |
123
|
2 |
|
$new->direction = $value; |
124
|
|
|
|
125
|
2 |
|
return $new; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* The configuration array for example: |
130
|
|
|
* |
131
|
|
|
* ```php |
132
|
|
|
* [ |
133
|
|
|
* 'items' => [ |
134
|
|
|
* ['label' => 'DropdownA', 'url' => '/'], |
135
|
|
|
* ['label' => 'DropdownB', 'url' => '#'], |
136
|
|
|
* ], |
137
|
|
|
* ] |
138
|
|
|
* ``` |
139
|
|
|
* |
140
|
|
|
* {@see Dropdown} |
141
|
|
|
* |
142
|
|
|
* @param array $value |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
10 |
|
public function withDropdown(array $value): self |
147
|
|
|
{ |
148
|
10 |
|
$new = clone $this; |
149
|
10 |
|
$new->dropdown = $value; |
150
|
|
|
|
151
|
10 |
|
return $new; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Name of a class to use for rendering dropdowns withing this widget. Defaults to {@see Dropdown}. |
156
|
|
|
* |
157
|
|
|
* @param string $value |
158
|
|
|
* |
159
|
|
|
* @return $this |
160
|
|
|
*/ |
161
|
1 |
|
public function withDropdownClass(string $value): self |
162
|
|
|
{ |
163
|
1 |
|
$new = clone $this; |
164
|
1 |
|
$new->dropdownClass = $value; |
165
|
|
|
|
166
|
1 |
|
return $new; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Whether the label should be HTML-encoded. |
171
|
|
|
* |
172
|
|
|
* @param bool $value |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
1 |
|
public function withoutEncodeLabels(bool $value = false): self |
177
|
|
|
{ |
178
|
1 |
|
$new = clone $this; |
179
|
1 |
|
$new->encodeLabels = $value; |
180
|
|
|
|
181
|
1 |
|
return $new; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* The button label. |
186
|
|
|
* |
187
|
|
|
* @param string $value |
188
|
|
|
* |
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
2 |
|
public function withLabel(string $value): self |
192
|
|
|
{ |
193
|
2 |
|
$new = clone $this; |
194
|
2 |
|
$new->label = $value; |
195
|
|
|
|
196
|
2 |
|
return $new; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* The HTML attributes for the container tag. The following special options are recognized. |
201
|
|
|
* |
202
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
203
|
|
|
* |
204
|
|
|
* @param array $value |
205
|
|
|
* |
206
|
|
|
* @return $this |
207
|
|
|
*/ |
208
|
1 |
|
public function withOptions(array $value): self |
209
|
|
|
{ |
210
|
1 |
|
$new = clone $this; |
211
|
1 |
|
$new->options = $value; |
212
|
|
|
|
213
|
1 |
|
return $new; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Whether to render the container using the {@see options} as HTML attributes. If set to `false`, the container |
218
|
|
|
* element enclosing the button and dropdown will NOT be rendered. |
219
|
|
|
* |
220
|
|
|
* @param bool $value |
221
|
|
|
* |
222
|
|
|
* @return $this |
223
|
|
|
*/ |
224
|
1 |
|
public function withoutRenderContainer(bool $value = false): self |
225
|
|
|
{ |
226
|
1 |
|
$new = clone $this; |
227
|
1 |
|
$new->renderContainer = $value; |
228
|
|
|
|
229
|
1 |
|
return $new; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Whether to display a group of split-styled button group. |
234
|
|
|
* |
235
|
|
|
* @param bool $value |
236
|
|
|
* |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
1 |
|
public function withSplit(bool $value = true): self |
240
|
|
|
{ |
241
|
1 |
|
$new = clone $this; |
242
|
1 |
|
$new->split = $value; |
243
|
|
|
|
244
|
1 |
|
return $new; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* The tag to use to render the button. |
249
|
|
|
* |
250
|
|
|
* @param string $value |
251
|
|
|
* |
252
|
|
|
* @return $this |
253
|
|
|
*/ |
254
|
1 |
|
public function withTagName(string $value): self |
255
|
|
|
{ |
256
|
1 |
|
$new = clone $this; |
257
|
1 |
|
$new->tagName = $value; |
258
|
|
|
|
259
|
1 |
|
return $new; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Allows you to enable or disable the encoding tags html. |
264
|
|
|
* |
265
|
|
|
* @param bool $value |
266
|
|
|
* |
267
|
|
|
* @return self |
268
|
|
|
*/ |
269
|
1 |
|
public function withEncodeTags(bool $value = true): self |
270
|
|
|
{ |
271
|
1 |
|
$new = clone $this; |
272
|
1 |
|
$new->encodeTags = $value; |
273
|
|
|
|
274
|
1 |
|
return $new; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Generates the button dropdown. |
279
|
|
|
* |
280
|
|
|
* @throws InvalidConfigException |
281
|
|
|
* |
282
|
|
|
* @return string the rendering result. |
283
|
|
|
*/ |
284
|
10 |
|
private function renderButton(): string |
285
|
|
|
{ |
286
|
10 |
|
Html::addCssClass($this->buttonOptions, 'btn'); |
287
|
|
|
|
288
|
10 |
|
$label = $this->label; |
289
|
|
|
|
290
|
10 |
|
if ($this->encodeLabels !== false) { |
291
|
10 |
|
$label = Html::encode($label); |
292
|
|
|
} |
293
|
|
|
|
294
|
10 |
|
if ($this->split) { |
295
|
1 |
|
$buttonOptions = $this->buttonOptions; |
296
|
|
|
|
297
|
1 |
|
$this->buttonOptions['data-bs-toggle'] = 'dropdown'; |
298
|
1 |
|
$this->buttonOptions['aria-haspopup'] = 'true'; |
299
|
1 |
|
$this->buttonOptions['aria-expanded'] = 'false'; |
300
|
|
|
|
301
|
1 |
|
Html::addCssClass($this->buttonOptions, ['toggle' => 'dropdown-toggle dropdown-toggle-split']); |
302
|
|
|
|
303
|
1 |
|
unset($buttonOptions['id']); |
304
|
|
|
|
305
|
1 |
|
$splitButton = Button::widget() |
306
|
1 |
|
->withLabel('<span class="sr-only">Toggle Dropdown</span>') |
|
|
|
|
307
|
1 |
|
->withoutEncodeLabels() |
308
|
1 |
|
->withOptions($this->buttonOptions) |
309
|
1 |
|
->render(); |
310
|
|
|
} else { |
311
|
9 |
|
$buttonOptions = $this->buttonOptions; |
312
|
|
|
|
313
|
9 |
|
Html::addCssClass($buttonOptions, ['toggle' => 'dropdown-toggle']); |
314
|
|
|
|
315
|
9 |
|
$buttonOptions['data-bs-toggle'] = 'dropdown'; |
316
|
9 |
|
$buttonOptions['aria-haspopup'] = 'true'; |
317
|
9 |
|
$buttonOptions['aria-expanded'] = 'false'; |
318
|
9 |
|
$splitButton = ''; |
319
|
|
|
} |
320
|
|
|
|
321
|
10 |
|
if (!isset($buttonOptions['href']) && ($this->tagName === 'a')) { |
322
|
1 |
|
$buttonOptions['href'] = '#'; |
323
|
1 |
|
$buttonOptions['role'] = 'button'; |
324
|
|
|
} |
325
|
|
|
|
326
|
10 |
|
return Button::widget() |
327
|
10 |
|
->withTagName($this->tagName) |
|
|
|
|
328
|
10 |
|
->withLabel($label) |
329
|
10 |
|
->withOptions($buttonOptions) |
330
|
10 |
|
->withoutEncodeLabels() |
331
|
10 |
|
->render() |
332
|
10 |
|
. "\n" . $splitButton; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Generates the dropdown menu. |
337
|
|
|
* |
338
|
|
|
* @return string the rendering result. |
339
|
|
|
*/ |
340
|
10 |
|
private function renderDropdown(): string |
341
|
|
|
{ |
342
|
10 |
|
$dropdownClass = $this->dropdownClass; |
343
|
|
|
|
344
|
10 |
|
return $dropdownClass::widget() |
345
|
10 |
|
->withItems($this->dropdown['items']) |
346
|
10 |
|
->withoutEncodeLabels($this->encodeLabels) |
347
|
10 |
|
->render(); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|