1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
10
|
|
|
use Yiisoft\Factory\Exceptions\InvalidConfigException; |
11
|
|
|
use Yiisoft\Html\Html; |
12
|
|
|
|
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function array_merge; |
15
|
|
|
use function array_merge_recursive; |
16
|
|
|
use function is_string; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Dropdown renders a Bootstrap dropdown menu component. |
20
|
|
|
* |
21
|
|
|
* For example, |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* <div class="dropdown"> |
25
|
|
|
* <?php |
26
|
|
|
* echo Dropdown::widget() |
27
|
|
|
* ->items([ |
28
|
|
|
* ['label' => 'DropdownA', 'url' => '/'], |
29
|
|
|
* ['label' => 'DropdownB', 'url' => '#'], |
30
|
|
|
* ]); |
31
|
|
|
* ?> |
32
|
|
|
* </div> |
33
|
|
|
* ``` |
34
|
|
|
*/ |
35
|
|
|
final class Dropdown extends Widget |
36
|
|
|
{ |
37
|
|
|
private array $items = []; |
38
|
|
|
private bool $encodeLabels = true; |
39
|
|
|
private bool $encodeTags = false; |
40
|
|
|
private array $submenuOptions = []; |
41
|
|
|
private array $options = []; |
42
|
|
|
|
43
|
27 |
|
protected function run(): string |
44
|
|
|
{ |
45
|
27 |
|
if (!isset($this->options['id'])) { |
46
|
26 |
|
$this->options['id'] = "{$this->getId()}-dropdown"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** @psalm-suppress InvalidArgument */ |
50
|
27 |
|
Html::addCssClass($this->options, ['widget' => 'dropdown-menu']); |
51
|
|
|
|
52
|
27 |
|
if ($this->encodeTags === false) { |
53
|
27 |
|
$this->options = array_merge($this->options, ['itemOptions' => ['encode' => false], 'encode' => false]); |
54
|
|
|
} |
55
|
|
|
|
56
|
27 |
|
return $this->renderItems($this->items, $this->options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* List of menu items in the dropdown. Each array element can be either an HTML string, or an array representing a |
61
|
|
|
* single menu with the following structure: |
62
|
|
|
* |
63
|
|
|
* - label: string, required, the label of the item link. |
64
|
|
|
* - encode: bool, optional, whether to HTML-encode item label. |
65
|
|
|
* - url: string|array, optional, the URL of the item link. This will be processed by {@see currentPath}. |
66
|
|
|
* If not set, the item will be treated as a menu header when the item has no sub-menu. |
67
|
|
|
* - visible: bool, optional, whether this menu item is visible. Defaults to true. |
68
|
|
|
* - linkOptions: array, optional, the HTML attributes of the item link. |
69
|
|
|
* - options: array, optional, the HTML attributes of the item. |
70
|
|
|
* - items: array, optional, the submenu items. The structure is the same as this property. |
71
|
|
|
* Note that Bootstrap doesn't support dropdown submenu. You have to add your own CSS styles to support it. |
72
|
|
|
* - submenuOptions: array, optional, the HTML attributes for sub-menu container tag. If specified it will be |
73
|
|
|
* merged with {@see submenuOptions}. |
74
|
|
|
* |
75
|
|
|
* To insert divider use `-`. |
76
|
|
|
* |
77
|
|
|
* @param array $value |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
27 |
|
public function items(array $value): self |
82
|
|
|
{ |
83
|
27 |
|
$new = clone $this; |
84
|
27 |
|
$new->items = $value; |
85
|
|
|
|
86
|
27 |
|
return $new; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* When tags Labels HTML should not be encoded. |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
3 |
|
public function withoutEncodeLabels(): self |
95
|
|
|
{ |
96
|
3 |
|
$new = clone $this; |
97
|
3 |
|
$new->encodeLabels = false; |
98
|
|
|
|
99
|
3 |
|
return $new; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The HTML attributes for sub-menu container tags. |
104
|
|
|
* |
105
|
|
|
* @param array $value |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
5 |
|
public function submenuOptions(array $value): self |
110
|
|
|
{ |
111
|
5 |
|
$new = clone $this; |
112
|
5 |
|
$new->submenuOptions = $value; |
113
|
|
|
|
114
|
5 |
|
return $new; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $value the HTML attributes for the widget container tag. The following special options are |
119
|
|
|
* recognized. |
120
|
|
|
* |
121
|
|
|
* @return $this |
122
|
|
|
* |
123
|
|
|
* {@see Html::renderTagAttributes()} for details on how attributes are being rendered. |
124
|
|
|
*/ |
125
|
15 |
|
public function options(array $value): self |
126
|
|
|
{ |
127
|
15 |
|
$new = clone $this; |
128
|
15 |
|
$new->options = $value; |
129
|
|
|
|
130
|
15 |
|
return $new; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Allows you to enable the encoding tags html. |
135
|
|
|
* |
136
|
|
|
* @return self |
137
|
|
|
*/ |
138
|
1 |
|
public function encodeTags(): self |
139
|
|
|
{ |
140
|
1 |
|
$new = clone $this; |
141
|
1 |
|
$new->encodeTags = true; |
142
|
|
|
|
143
|
1 |
|
return $new; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Renders menu items. |
148
|
|
|
* |
149
|
|
|
* @param array $items the menu items to be rendered |
150
|
|
|
* @param array $options the container HTML attributes |
151
|
|
|
* |
152
|
|
|
* @throws InvalidConfigException|JsonException|RuntimeException if the label option is not specified in one of the |
153
|
|
|
* items. |
154
|
|
|
* |
155
|
|
|
* @return string the rendering result. |
156
|
|
|
*/ |
157
|
27 |
|
private function renderItems(array $items, array $options = []): string |
158
|
|
|
{ |
159
|
27 |
|
$lines = []; |
160
|
|
|
|
161
|
27 |
|
foreach ($items as $item) { |
162
|
27 |
|
if (is_string($item)) { |
163
|
3 |
|
$item = ['label' => $item, 'encode' => false, 'enclose' => false]; |
164
|
|
|
} |
165
|
|
|
|
166
|
27 |
|
if (isset($item['visible']) && !$item['visible']) { |
167
|
3 |
|
continue; |
168
|
|
|
} |
169
|
|
|
|
170
|
27 |
|
if (!array_key_exists('label', $item)) { |
171
|
1 |
|
throw new RuntimeException("The 'label' option is required."); |
172
|
|
|
} |
173
|
|
|
|
174
|
26 |
|
$encodeLabel = $item['encode'] ?? $this->encodeLabels; |
175
|
26 |
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
176
|
26 |
|
$itemOptions = ArrayHelper::getValue($item, 'options', []); |
177
|
26 |
|
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); |
178
|
26 |
|
$active = ArrayHelper::getValue($item, 'active', false); |
179
|
26 |
|
$disabled = ArrayHelper::getValue($item, 'disabled', false); |
180
|
26 |
|
$enclose = ArrayHelper::getValue($item, 'enclose', true); |
181
|
|
|
|
182
|
26 |
|
if ($this->encodeTags === false) { |
183
|
26 |
|
ArrayHelper::setValue($linkOptions, 'encode', false); |
184
|
26 |
|
ArrayHelper::setValue($itemOptions, 'encode', false); |
185
|
|
|
} |
186
|
|
|
|
187
|
26 |
|
Html::addCssClass($linkOptions, ['widget' => 'dropdown-item']); |
188
|
|
|
|
189
|
26 |
|
if ($disabled) { |
190
|
2 |
|
ArrayHelper::setValue($linkOptions, 'tabindex', '-1'); |
191
|
2 |
|
ArrayHelper::setValue($linkOptions, 'aria-disabled', 'true'); |
192
|
2 |
|
Html::addCssClass($linkOptions, ['disable' => 'disabled']); |
193
|
26 |
|
} elseif ($active) { |
194
|
4 |
|
Html::addCssClass($linkOptions, ['active' => 'active']); |
195
|
|
|
} |
196
|
|
|
|
197
|
26 |
|
$url = $item['url'] ?? null; |
198
|
|
|
|
199
|
|
|
/** @psalm-suppress ConflictingReferenceConstraint */ |
200
|
26 |
|
if (empty($item['items'])) { |
201
|
26 |
|
if ($label === '-') { |
202
|
3 |
|
$content = Html::div('', ['class' => 'dropdown-divider']); |
203
|
26 |
|
} elseif ($enclose === false) { |
204
|
1 |
|
$content = $label; |
205
|
26 |
|
} elseif ($url === null) { |
206
|
12 |
|
$content = Html::tag('h6', $label, ['class' => 'dropdown-header']); |
207
|
|
|
} else { |
208
|
18 |
|
$content = Html::a($label, $url, $linkOptions); |
209
|
|
|
} |
210
|
|
|
|
211
|
26 |
|
$lines[] = $content; |
212
|
|
|
} else { |
213
|
5 |
|
$submenuOptions = $this->submenuOptions; |
214
|
|
|
|
215
|
5 |
|
if (isset($item['submenuOptions'])) { |
216
|
1 |
|
$submenuOptions = array_merge($submenuOptions, $item['submenuOptions']); |
217
|
|
|
} |
218
|
|
|
|
219
|
5 |
|
Html::addCssClass($submenuOptions, ['submenu' => 'dropdown-menu']); |
220
|
5 |
|
Html::addCssClass($linkOptions, ['toggle' => 'dropdown-toggle']); |
221
|
|
|
|
222
|
5 |
|
$itemOptions = array_merge_recursive(['class' => ['dropdown'], 'aria-expanded' => 'false'], $itemOptions); |
223
|
|
|
|
224
|
5 |
|
$dropdown = self::widget() |
225
|
5 |
|
->items($item['items']) |
|
|
|
|
226
|
5 |
|
->options($submenuOptions) |
227
|
5 |
|
->submenuOptions($submenuOptions); |
228
|
|
|
|
229
|
5 |
|
if ($this->encodeLabels === false) { |
230
|
1 |
|
$dropdown = $dropdown->withoutEncodeLabels(); |
231
|
|
|
} |
232
|
|
|
|
233
|
5 |
|
ArrayHelper::setValue($linkOptions, 'data-bs-toggle', 'dropdown'); |
234
|
5 |
|
ArrayHelper::setValue($linkOptions, 'aria-haspopup', 'true'); |
235
|
5 |
|
ArrayHelper::setValue($linkOptions, 'aria-expanded', 'false'); |
236
|
5 |
|
ArrayHelper::setValue($linkOptions, 'role', 'button'); |
237
|
|
|
|
238
|
5 |
|
$lines[] = Html::a($label, $url, $linkOptions) . Html::tag('ul', $dropdown->render(), $itemOptions); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
26 |
|
$options = array_merge(['aria-expanded' => 'false'], $options); |
243
|
|
|
|
244
|
26 |
|
return Html::ul($lines, $options); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|