Skip to content

Commit ec02c7e

Browse files
committed
created Accordion widget (Close #22)
1 parent 646ae00 commit ec02c7e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Yii2 Gentelella Change Log
44
1.3.0 Under development
55
-----------------------
66

7+
1.2.3 July 4, 2017
8+
------------------
9+
10+
- Enh #22: Created Accordion widget (fps01)
11+
712
1.2.2 May 22, 2017
813
------------------
914

widgets/Accordion.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2015 Yiister
4+
* @license https://github.com/yiister/yii2-gentelella/blob/master/LICENSE
5+
* @link http://gentelella.yiister.ru
6+
*/
7+
8+
namespace yiister\gentelella\widgets;
9+
10+
use yii\base\Widget;
11+
use yii\helpers\ArrayHelper;
12+
use yii\helpers\Html;
13+
14+
class Accordion extends Widget
15+
{
16+
/**
17+
* @var array the list of items to render
18+
* Each item allows the next string attributes:
19+
* title - the string header of item
20+
* active - whether the item is opened
21+
* id - the string identificator for the item
22+
* content - the string content of this item
23+
* options - the array of a HTML options for the content tag
24+
*/
25+
public $items = [];
26+
27+
/**
28+
* @var array the HTML attributes for the widget container tag
29+
*/
30+
public $options = [
31+
'class' => 'accordion',
32+
'role' => 'tablist',
33+
'aria-multiselectable' => 'true',
34+
];
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function run()
40+
{
41+
if (!isset($this->options['id'])) {
42+
$this->options['id'] = $this->getId();
43+
}
44+
echo Html::beginTag('div', $this->options);
45+
foreach ($this->items as $key => $item) {
46+
$itemOptions = ArrayHelper::getValue($item, 'options', []);
47+
if (!isset($itemOptions['id'])) {
48+
$itemOptions['id'] = isset($item['id'])
49+
? $item['id']
50+
: $this->options['id'] . '-' . $key;
51+
}
52+
$itemOptions = ArrayHelper::merge(
53+
$itemOptions,
54+
[
55+
'aria-labelledby' => 'heading-' . $itemOptions['id'],
56+
'class' => 'panel-collapse collapse',
57+
'id' => $itemOptions['id'],
58+
'role' => 'tabpanel',
59+
]
60+
);
61+
$isActive = ArrayHelper::getValue($item, 'active', false);
62+
if ($isActive) {
63+
Html::addCssClass($itemOptions, 'in');
64+
}
65+
echo Html::beginTag('div', ['class' => 'panel']);
66+
echo Html::a(
67+
Html::tag(
68+
'h4',
69+
$item['title'],
70+
[
71+
'class' => 'panel-title',
72+
]
73+
),
74+
'#' . $itemOptions['id'],
75+
[
76+
'class' => 'panel-heading',
77+
'id' => 'heading-' . $itemOptions['id'],
78+
'role' => 'tab',
79+
'data-toggle' => 'collapse',
80+
'data-parent' => '#' . $this->options['id'],
81+
'aria-expanded' => $isActive ? 'true' : 'false',
82+
'aria-controls' => $itemOptions['id'],
83+
]
84+
);
85+
echo Html::tag(
86+
'div',
87+
Html::tag('div', $item['content'], ['class' => 'panel-body']),
88+
$itemOptions
89+
);
90+
echo Html::endTag('div');
91+
}
92+
echo Html::endTag('div');
93+
}
94+
}

0 commit comments

Comments
 (0)