1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Bootstrap5; |
6
|
|
|
|
7
|
|
|
use function array_key_exists; |
8
|
|
|
use function array_merge; |
9
|
|
|
use function implode; |
10
|
|
|
use function is_array; |
11
|
|
|
|
12
|
|
|
use function is_int; |
13
|
|
|
use function is_numeric; |
14
|
|
|
use function is_object; |
15
|
|
|
use function is_string; |
16
|
|
|
use JsonException; |
17
|
|
|
use Yiisoft\Arrays\ArrayHelper; |
18
|
|
|
use Yiisoft\Html\Html; |
19
|
|
|
use Yiisoft\Widget\Exception\InvalidConfigException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Accordion renders an accordion bootstrap javascript component. |
23
|
|
|
* |
24
|
|
|
* For example: |
25
|
|
|
* |
26
|
|
|
* ```php |
27
|
|
|
* echo Accordion::widget() |
28
|
|
|
* ->items([ |
29
|
|
|
* // equivalent to the above |
30
|
|
|
* [ |
31
|
|
|
* 'label' => 'Collapsible Group Item #1', |
32
|
|
|
* 'content' => 'Anim pariatur cliche...', |
33
|
|
|
* // open its content by default |
34
|
|
|
* 'contentOptions' => ['class' => 'in'], |
35
|
|
|
* ], |
36
|
|
|
* // another group item |
37
|
|
|
* [ |
38
|
|
|
* 'label' => 'Collapsible Group Item #1', |
39
|
|
|
* 'content' => 'Anim pariatur cliche...', |
40
|
|
|
* 'contentOptions' => [...], |
41
|
|
|
* 'options' => [...], |
42
|
|
|
* ], |
43
|
|
|
* // if you want to swap out .accordion-body with .list-group, you may provide an array |
44
|
|
|
* [ |
45
|
|
|
* 'label' => 'Collapsible Group Item #1', |
46
|
|
|
* 'content' => [ |
47
|
|
|
* 'Anim pariatur cliche...', |
48
|
|
|
* 'Anim pariatur cliche...', |
49
|
|
|
* ], |
50
|
|
|
* 'contentOptions' => [...], |
51
|
|
|
* 'options' => [...], |
52
|
|
|
* ], |
53
|
|
|
* ]); |
54
|
|
|
* ``` |
55
|
|
|
*/ |
56
|
|
|
class Accordion extends Widget |
57
|
|
|
{ |
58
|
|
|
private array $items = []; |
59
|
|
|
private bool $encodeLabels = true; |
60
|
|
|
private bool $autoCloseItems = true; |
61
|
|
|
private array $itemToggleOptions = []; |
62
|
|
|
private array $options = []; |
63
|
|
|
|
64
|
6 |
|
protected function run(): string |
65
|
|
|
{ |
66
|
6 |
|
if (!isset($this->options['id'])) { |
67
|
6 |
|
$this->options['id'] = "{$this->getId()}-accordion"; |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
$this->registerPlugin('collapse', $this->options); |
71
|
|
|
|
72
|
6 |
|
Html::addCssClass($this->options, 'accordion'); |
73
|
|
|
|
74
|
6 |
|
return implode("\n", [ |
75
|
6 |
|
Html::beginTag('div', $this->options), |
76
|
6 |
|
$this->renderItems(), |
77
|
3 |
|
Html::endTag('div'), |
78
|
3 |
|
]) . "\n"; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Renders collapsible items as specified on {@see items}. |
83
|
|
|
* |
84
|
|
|
* @throws InvalidConfigException|JsonException |
85
|
|
|
* |
86
|
|
|
* @return string the rendering result |
87
|
|
|
*/ |
88
|
6 |
|
public function renderItems(): string |
89
|
|
|
{ |
90
|
6 |
|
$items = []; |
91
|
6 |
|
$index = 0; |
92
|
|
|
|
93
|
6 |
|
foreach ($this->items as $key => $item) { |
94
|
6 |
|
if (!is_array($item)) { |
95
|
1 |
|
$item = ['content' => $item]; |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
if (!array_key_exists('label', $item)) { |
99
|
3 |
|
if (is_int($key)) { |
100
|
3 |
|
throw new InvalidConfigException("The 'label' option is required."); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$item['label'] = $key; |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
$header = ArrayHelper::remove($item, 'label'); |
107
|
3 |
|
$options = ArrayHelper::getValue($item, 'options', []); |
108
|
|
|
|
109
|
3 |
|
Html::addCssClass($options, ['panel' => 'accordion-item']); |
110
|
|
|
|
111
|
3 |
|
$items[] = Html::tag('div', $this->renderItem($header, $item, $index++), $options); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
return implode("\n", $items); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Renders a single collapsible item group. |
119
|
|
|
* |
120
|
|
|
* @param string $header a label of the item group {@see items} |
121
|
|
|
* @param array $item a single item from {@see items} |
122
|
|
|
* @param int $index the item index as each item group content must have an id |
123
|
|
|
* |
124
|
|
|
* @throws InvalidConfigException|JsonException |
125
|
|
|
* |
126
|
|
|
* @return string the rendering result |
127
|
|
|
*/ |
128
|
3 |
|
public function renderItem(string $header, array $item, int $index): string |
129
|
|
|
{ |
130
|
3 |
|
if (array_key_exists('content', $item)) { |
131
|
3 |
|
$id = $this->options['id'] . '-collapse' . $index; |
132
|
3 |
|
$options = ArrayHelper::getValue($item, 'contentOptions', []); |
133
|
3 |
|
$options['id'] = $id; |
134
|
|
|
|
135
|
3 |
|
Html::addCssClass($options, ['widget' => 'collapse']); |
136
|
|
|
|
137
|
3 |
|
if ($index === 0) { |
138
|
3 |
|
Html::addCssClass($options, 'show'); |
139
|
|
|
} |
140
|
|
|
|
141
|
3 |
|
if (!isset($options['aria-label'], $options['aria-labelledby'])) { |
142
|
3 |
|
$options['aria-labelledby'] = $options['id'] . '-heading'; |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$encodeLabel = $item['encode'] ?? $this->encodeLabels; |
146
|
|
|
|
147
|
3 |
|
if ($encodeLabel) { |
148
|
3 |
|
$header = Html::encode($header); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
$itemToggleOptions = array_merge([ |
152
|
3 |
|
'tag' => 'button', |
153
|
3 |
|
'type' => 'button', |
154
|
3 |
|
'data-toggle' => 'collapse', |
155
|
3 |
|
'data-target' => '#' . $options['id'], |
156
|
3 |
|
'aria-expanded' => ($index === 0) ? 'true' : 'false', |
157
|
3 |
|
'aria-controls' => $options['id'], |
158
|
3 |
|
], $this->itemToggleOptions); |
159
|
3 |
|
$itemToggleTag = ArrayHelper::remove($itemToggleOptions, 'tag', 'button'); |
160
|
|
|
|
161
|
|
|
/** @psalm-suppress ConflictingReferenceConstraint */ |
162
|
3 |
|
if ($itemToggleTag === 'a') { |
163
|
1 |
|
ArrayHelper::remove($itemToggleOptions, 'data-target'); |
164
|
1 |
|
$headerToggle = Html::a($header, '#' . $id, $itemToggleOptions) . "\n"; |
165
|
|
|
} else { |
166
|
2 |
|
Html::addCssClass($itemToggleOptions, 'btn-link'); |
167
|
2 |
|
$headerToggle = Button::widget() |
168
|
2 |
|
->label($header) |
|
|
|
|
169
|
2 |
|
->encodeLabels(false) |
170
|
2 |
|
->options($itemToggleOptions) |
171
|
2 |
|
->render() . "\n"; |
172
|
|
|
} |
173
|
|
|
|
174
|
3 |
|
$header = Html::tag('h5', $headerToggle, ['class' => 'mb-0']); |
175
|
|
|
|
176
|
3 |
|
if (is_string($item['content']) || is_numeric($item['content']) || is_object($item['content'])) { |
177
|
3 |
|
$content = Html::tag('div', $item['content'], ['class' => 'accordion-body']) . "\n"; |
178
|
1 |
|
} elseif (is_array($item['content'])) { |
179
|
1 |
|
$content = Html::ul($item['content'], [ |
180
|
1 |
|
'class' => 'list-group', |
181
|
|
|
'itemOptions' => [ |
182
|
|
|
'class' => 'list-group-item', |
183
|
|
|
], |
184
|
|
|
'encode' => false, |
185
|
1 |
|
]) . "\n"; |
186
|
|
|
} else { |
187
|
3 |
|
throw new InvalidConfigException('The "content" option should be a string, array or object.'); |
188
|
|
|
} |
189
|
|
|
} else { |
190
|
|
|
throw new InvalidConfigException('The "content" option is required.'); |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
$group = []; |
194
|
|
|
|
195
|
3 |
|
if ($this->autoCloseItems) { |
196
|
3 |
|
$options['data-parent'] = '#' . $this->options['id']; |
197
|
|
|
} |
198
|
|
|
|
199
|
3 |
|
$group[] = Html::tag('div', $header, ['class' => 'accordion-header', 'id' => $options['id'] . '-heading']); |
200
|
3 |
|
$group[] = Html::beginTag('div', $options); |
201
|
3 |
|
$group[] = $content; |
202
|
|
|
|
203
|
3 |
|
$group[] = Html::endTag('div'); |
204
|
|
|
|
205
|
3 |
|
return implode("\n", $group); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Whether to close other items if an item is opened. Defaults to `true` which causes an accordion effect. |
210
|
|
|
* |
211
|
|
|
* Set this to `false` to allow keeping multiple items open at once. |
212
|
|
|
* |
213
|
|
|
* @param bool $value |
214
|
|
|
* |
215
|
|
|
* @return $this |
216
|
|
|
*/ |
217
|
1 |
|
public function autoCloseItems(bool $value): self |
218
|
|
|
{ |
219
|
1 |
|
$this->autoCloseItems = $value; |
220
|
|
|
|
221
|
1 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Whether the labels for header items should be HTML-encoded. |
226
|
|
|
* |
227
|
|
|
* @param bool $value |
228
|
|
|
* |
229
|
|
|
* @return $this |
230
|
|
|
*/ |
231
|
|
|
public function encodeLabels(bool $value): self |
232
|
|
|
{ |
233
|
|
|
$this->encodeLabels = $value; |
234
|
|
|
|
235
|
|
|
return $this; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* List of groups in the collapse widget. Each array element represents a single group with the following structure: |
240
|
|
|
* |
241
|
|
|
* - label: string, required, the group header label. |
242
|
|
|
* - encode: bool, optional, whether this label should be HTML-encoded. This param will override global |
243
|
|
|
* `$this->encodeLabels` param. |
244
|
|
|
* - content: array|string|object, required, the content (HTML) of the group |
245
|
|
|
* - options: array, optional, the HTML attributes of the group |
246
|
|
|
* - contentOptions: optional, the HTML attributes of the group's content |
247
|
|
|
* |
248
|
|
|
* You may also specify this property as key-value pairs, where the key refers to the `label` and the value refers |
249
|
|
|
* to `content`. If value is a string it is interpreted as label. If it is an array, it is interpreted as explained |
250
|
|
|
* above. |
251
|
|
|
* |
252
|
|
|
* For example: |
253
|
|
|
* |
254
|
|
|
* ```php |
255
|
|
|
* echo Accordion::widget([ |
256
|
|
|
* 'items' => [ |
257
|
|
|
* 'Introduction' => 'This is the first collapsible menu', |
258
|
|
|
* 'Second panel' => [ |
259
|
|
|
* 'content' => 'This is the second collapsible menu', |
260
|
|
|
* ], |
261
|
|
|
* [ |
262
|
|
|
* 'label' => 'Third panel', |
263
|
|
|
* 'content' => 'This is the third collapsible menu', |
264
|
|
|
* ], |
265
|
|
|
* ] |
266
|
|
|
* ]) |
267
|
|
|
* ``` |
268
|
|
|
* |
269
|
|
|
* @param array $value |
270
|
|
|
* |
271
|
|
|
* @return $this |
272
|
|
|
*/ |
273
|
6 |
|
public function items(array $value): self |
274
|
|
|
{ |
275
|
6 |
|
$this->items = $value; |
276
|
|
|
|
277
|
6 |
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* The HTML options for the item toggle tag. Key 'tag' might be used here for the tag name specification. |
282
|
|
|
* |
283
|
|
|
* For example: |
284
|
|
|
* |
285
|
|
|
* ```php |
286
|
|
|
* [ |
287
|
|
|
* 'tag' => 'div', |
288
|
|
|
* 'class' => 'custom-toggle', |
289
|
|
|
* ] |
290
|
|
|
* ``` |
291
|
|
|
* |
292
|
|
|
* @param array $value |
293
|
|
|
* |
294
|
|
|
* @return $this |
295
|
|
|
*/ |
296
|
1 |
|
public function itemToggleOptions(array $value): self |
297
|
|
|
{ |
298
|
1 |
|
$this->itemToggleOptions = $value; |
299
|
|
|
|
300
|
1 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
305
|
|
|
* |
306
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
307
|
|
|
* |
308
|
|
|
* @param array $value |
309
|
|
|
* |
310
|
|
|
* @return $this |
311
|
|
|
*/ |
312
|
|
|
public function options(array $value): self |
313
|
|
|
{ |
314
|
|
|
$this->options = $value; |
315
|
|
|
|
316
|
|
|
return $this; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|