generated from yiisoft/package-template
/
Dropdown.php
378 lines (334 loc) · 11 KB
/
Dropdown.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
declare(strict_types=1);
namespace Yiisoft\Yii\Bulma;
use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
use function array_key_exists;
use function array_merge;
use function implode;
use function is_array;
/**
* The dropdown component is a container for a dropdown button and a dropdown menu.
*
* @link https://bulma.io/documentation/components/dropdown/
*/
final class Dropdown extends Widget
{
private string $buttonLabel = '';
private array $buttonLabelOptions = [];
private array $buttonIcon = ['class' => 'fas fa-angle-down', 'aria-hidden' => 'true'];
private array $buttonIconOptions = [];
private string $dividerClass = 'dropdown-divider';
private string $itemsClass = 'dropdown-menu';
private string $itemClass = 'dropdown-item';
private bool $encodeLabels = true;
private bool $encloseByContainer = true;
private array $items = [];
private array $itemsOptions = [];
private array $options = [];
private array $buttonOptions = [];
private array $linkOptions = ['aria-haspopup' => 'true', 'aria-expanded' => 'false'];
private array $triggerOptions = [];
protected function run(): string
{
$this->buildOptions();
return $this->buildDropdown();
}
/**
* Set label for the dropdown button.
*
* @param string $value
*
* @return self
*/
public function buttonLabel(string $value): self
{
$new = clone $this;
$new->buttonLabel = $value;
return $new;
}
/**
* The HTML attributes for the dropdown button.
*
* @param array $value
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function buttonLabelOptions(array $value): self
{
$new = clone $this;
$new->buttonLabelOptions = $value;
return $new;
}
/**
* Set CSS class for dropdown divider.
*
* @param $value
*
* @return self
*/
public function dividerClass(string $value): self
{
$new = clone $this;
$new->dividerClass = $value;
return $new;
}
/**
* Set CSS class for dropdown item.
*
* @param $value
*
* @return self
*/
public function itemClass(string $value): self
{
$new = clone $this;
$new->itemClass = $value;
return $new;
}
/**
* Set CSS class for dropdown items container.
*
* @param string $value
*
* @return self
*/
public function itemsClass(string $value): self
{
$new = clone $this;
$new->itemsClass = $value;
return $new;
}
/**
* Whether the labels for header items should be HTML-encoded.
*
* @param bool $value
*
* @return self
*/
public function encodeLabels(bool $value): self
{
$new = clone $this;
$new->encodeLabels = $value;
return $new;
}
/**
* Set enclosed by container dropdown.
*
* @param bool $value
*
* @return self
*/
public function encloseByContainer(bool $value): self
{
$new = clone $this;
$new->encloseByContainer = $value;
return $new;
}
/**
* List of menu items in the dropdown. Each array element can be either an HTML string, or an array representing a
* single menu with the following structure:
*
* - label: string, required, the label of the item link.
* - encode: bool, optional, whether to HTML-encode item label.
* - url: string|array, optional, the URL of the item link. This will be processed by {@see currentPath}.
* If not set, the item will be treated as a menu header when the item has no sub-menu.
* - visible: bool, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item link.
* - itemsOptions: array, optional, the HTML attributes of the item.
* - items: array, optional, the submenu items. The structure is the same as this property.
*
* To insert divider use `-`.
*
* @param array $value
*
* @return self
*/
public function items(array $value): self
{
$new = clone $this;
$new->items = $value;
return $new;
}
/**
* The HTML attributes for the widget container tag.
*
* @param array $value
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function options(array $value): self
{
$new = clone $this;
$new->options = $value;
return $new;
}
/**
* The HTML attributes for the widget button tag.
*
* @param array $value
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function buttonOptions(array $value): self
{
$new = clone $this;
$new->buttonOptions = $value;
return $new;
}
/**
* The HTML attributes for the widget items.
*
* @param array $value
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function itemsOptions(array $value): self
{
$new = clone $this;
$new->itemsOptions = $value;
return $new;
}
/**
* The HTML attributes for the widget container trigger.
*
* @param array $value
*
* @return self
*
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
*/
public function triggerOptions(array $value): self
{
$new = clone $this;
$new->triggerOptions = $value;
return $new;
}
private function buildDropdown(): string
{
if ($this->encloseByContainer) {
$html = Html::beginTag('div', $this->options) . "\n";
$html .= $this->buildDropdownTrigger();
$html .= $this->renderItems($this->items, $this->itemsOptions) . "\n";
$html .= Html::endTag('div');
} else {
$html = $this->renderItems($this->items, $this->itemsOptions);
}
return $html;
}
private function buildDropdownTrigger(): string
{
return
Html::beginTag('div', $this->triggerOptions) . "\n" .
Html::beginTag('button', $this->buttonOptions) . "\n" .
Html::tag('span', $this->buttonLabel, $this->buttonLabelOptions) . "\n" .
Html::beginTag('span', $this->buttonIconOptions) . "\n" .
Html::tag('i', '', $this->buttonIcon) . "\n" .
Html::endTag('span') . "\n" .
Html::endTag('button') . "\n" .
Html::endTag('div') . "\n";
}
private function buildOptions(): void
{
if ($this->encloseByContainer && (!isset($this->options['id']))) {
$this->options['id'] = "{$this->getId()}-dropdown";
$this->options = $this->addOptions($this->options, 'dropdown');
$this->triggerOptions = $this->addOptions($this->triggerOptions, 'dropdown-trigger');
$this->buttonOptions = $this->addOptions(
array_merge(
$this->buttonOptions,
[ 'aria-haspopup' => 'true', 'aria-controls' => 'dropdown-menu']
),
'button'
);
$this->buttonIconOptions = $this->addOptions($this->buttonIconOptions, 'icon is-small');
} elseif (!isset($this->itemsOptions['id'])) {
$this->itemsOptions['id'] = "{$this->getId()}-dropdown";
}
$this->itemsOptions = $this->addOptions($this->itemsOptions, $this->itemsClass);
}
/**
* Renders menu items.
*
* @param array $items the menu items to be rendered
* @param array $itemsOptions the container HTML attributes
*
* @throws InvalidArgumentException|JsonException if the label option is not specified in one of the items.
*
* @return string the rendering result.
*/
private function renderItems(array $items, array $itemsOptions = []): string
{
$lines = [];
foreach ($items as $item) {
if ($item === '-') {
$lines[] = Html::tag('div', '', ['class' => $this->dividerClass]);
continue;
}
if (!isset($item['label']) && $item !== '-') {
throw new InvalidArgumentException('The "label" option is required.');
}
$this->encodeLabels = $item['encode'] ?? $this->encodeLabels;
if ($this->encodeLabels) {
$label = Html::encode($item['label']);
} else {
$label = $item['label'];
}
$iconOptions = [];
$icon = $item['icon'] ?? '';
if (array_key_exists('iconOptions', $item) && is_array($item['iconOptions'])) {
$iconOptions = $this->addOptions($iconOptions, 'icon');
}
$label = $this->renderIcon($label, $icon, $iconOptions);
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$active = ArrayHelper::getValue($item, 'active', false);
$disabled = ArrayHelper::getValue($item, 'disabled', false);
Html::addCssClass($linkOptions, $this->itemClass);
if ($disabled) {
Html::addCssStyle($linkOptions, 'opacity:.65; pointer-events:none;');
}
/** @psalm-suppress ConflictingReferenceConstraint */
if ($active) {
Html::addCssClass($linkOptions, 'is-active');
}
$url = $item['url'] ?? null;
if (empty($item['items'])) {
$lines[] = Html::a($label, $url, $linkOptions);
} else {
$lines[] = Html::a($label, $url, array_merge($this->linkOptions, $linkOptions));
$lines[] = self::widget()
->dividerClass($this->dividerClass)
->itemClass($this->itemClass)
->itemsClass($this->itemsClass)
->encloseByContainer($this->encloseByContainer)
->encodeLabels($this->encodeLabels)
->items($item['items'])
->render();
}
}
return
Html::beginTag('div', $itemsOptions) . "\n" .
implode("\n", $lines) . "\n" .
Html::endTag('div');
}
private function renderIcon(string $label, string $icon, array $iconOptions): string
{
if ($icon !== '') {
$label = Html::beginTag('span', $iconOptions) .
Html::tag('i', '', ['class' => $icon]) .
Html::endTag('span') .
Html::tag('span', $label);
}
return $label;
}
}