diff --git a/CHANGELOG.md b/CHANGELOG.md index 9979bad..1bde18a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 bootstrap extension Change Log 2.0.7 under development ----------------------- +- Enh #64: Added simplified syntax for specifying `Collapse` widget `$items` (Faryshta, cebe) - Enh #131 Added `tabContentOptions` to set HTML attributes for 'tab-content' container in `Tabs` widget (AndrewKorpusov) - Enh #145: Added the ability to customize the class used to draw dropdowns in `yii\bootstrap\Nav`, `yii\bootstrapButtonDropdown` and `yii\bootstrap\Tab` widgets (PowerGamer1) - Enh #187: Added `yii\bootstrap\Tabs::activateFirstVisibleTab()` to set the first visible tab as active if no active tab is set (nilsburg) diff --git a/Collapse.php b/Collapse.php index 56a72c6..7fa5f2f 100644 --- a/Collapse.php +++ b/Collapse.php @@ -63,6 +63,27 @@ class Collapse extends Widget * - content: array|string|object, required, the content (HTML) of the group * - options: array, optional, the HTML attributes of the group * - contentOptions: optional, the HTML attributes of the group's content + * + * Since version 2.0.7 you may also specify this property as key-value pairs, where the key refers to the + * `label` and the value refers to `content`. If value is a string it is interpreted as label. If it is + * an array, it is interpreted as explained above. + * + * For example: + * + * ```php + * echo Collapse::widget([ + * 'items' => [ + * 'Introduction' => 'This is the first collapsable menu', + * 'Second panel' => [ + * 'content' => 'This is the second collapsable menu', + * ], + * [ + * 'label' => 'Third panel', + * 'content' => 'This is the third collapsable menu', + * ], + * ] + * ]) + * ``` */ public $items = []; /** @@ -102,9 +123,16 @@ class Collapse extends Widget { $items = []; $index = 0; - foreach ($this->items as $item) { + foreach ($this->items as $key => $item) { + if (!is_array($item)) { + $item = ['content' => $item]; + } if (!array_key_exists('label', $item)) { - throw new InvalidConfigException("The 'label' option is required."); + if (is_int($key)) { + throw new InvalidConfigException("The 'label' option is required."); + } else { + $item['label'] = $key; + } } $header = $item['label']; $options = ArrayHelper::getValue($item, 'options', []); diff --git a/tests/CollapseTest.php b/tests/CollapseTest.php index 5717e2a..06c4400 100644 --- a/tests/CollapseTest.php +++ b/tests/CollapseTest.php @@ -1,6 +1,7 @@ '/something']); + ActiveForm::end(); + ob_end_clean(); + + Collapse::$counter = 0; + $output = Collapse::widget([ + 'items' => [ + 'Item1' => 'Content1', + 'Item2' => [ + 'content' => 'Content2', + ], + [ + 'label' => 'Item3', + 'content' => 'Content3', + ], + 'FormField' => $form->field(new DynamicModel(['test']), 'test',['template' => '{input}']), + ] + ]); + + $this->assertEqualsWithoutLE(<< +

Item1 +

+
Content1
+
+

Item2 +

+
Content2
+
+

Item3 +

+
Content3
+
+

FormField +

+
+ +
+
+ + +HTML + , $output); + } + + public function invalidItemsProvider() + { + return [ + [ ['content'] ], // only content without label key + [ [[]] ], // only content array without label + [ [['content' => 'test']] ], // only content array without label + ]; + } + + /** + * @dataProvider invalidItemsProvider + * @expectedException \yii\base\InvalidConfigException + */ + public function testMissingLabel($items) + { + Collapse::widget([ + 'items' => $items, + ]); + } + /** * @see https://github.com/yiisoft/yii2/issues/8357 */