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