Browse Source

Fixes #64: Added simplified syntax for specifying `Collapse` widget `$items`

tags/2.0.7
Carsten Brandt 8 years ago committed by Alexander Makarov
parent
commit
0a549a51a4
  1. 1
      CHANGELOG.md
  2. 32
      Collapse.php
  3. 69
      tests/CollapseTest.php

1
CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 bootstrap extension Change Log
2.0.7 under development 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 #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 #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) - Enh #187: Added `yii\bootstrap\Tabs::activateFirstVisibleTab()` to set the first visible tab as active if no active tab is set (nilsburg)

32
Collapse.php

@ -63,6 +63,27 @@ class Collapse extends Widget
* - content: array|string|object, required, the content (HTML) of the group * - content: array|string|object, required, the content (HTML) of the group
* - options: array, optional, the HTML attributes of the group * - options: array, optional, the HTML attributes of the group
* - contentOptions: optional, the HTML attributes of the group's content * - 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 = []; public $items = [];
/** /**
@ -102,9 +123,16 @@ class Collapse extends Widget
{ {
$items = []; $items = [];
$index = 0; $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)) { 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']; $header = $item['label'];
$options = ArrayHelper::getValue($item, 'options', []); $options = ArrayHelper::getValue($item, 'options', []);

69
tests/CollapseTest.php

@ -1,6 +1,7 @@
<?php <?php
namespace yiiunit\extensions\bootstrap; namespace yiiunit\extensions\bootstrap;
use yii\base\DynamicModel;
use yii\bootstrap\Collapse; use yii\bootstrap\Collapse;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
@ -110,6 +111,74 @@ HTML
, $output); , $output);
} }
public function testLabelKeys()
{
ob_start();
$form = ActiveForm::begin(['action' => '/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(<<<HTML
<div id="w0" class="panel-group">
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse1" data-toggle="collapse" data-parent="#w0">Item1</a>
</h4></div>
<div id="w0-collapse1" class="panel-collapse collapse"><div class="panel-body">Content1</div>
</div></div>
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse2" data-toggle="collapse" data-parent="#w0">Item2</a>
</h4></div>
<div id="w0-collapse2" class="panel-collapse collapse"><div class="panel-body">Content2</div>
</div></div>
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse3" data-toggle="collapse" data-parent="#w0">Item3</a>
</h4></div>
<div id="w0-collapse3" class="panel-collapse collapse"><div class="panel-body">Content3</div>
</div></div>
<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a class="collapse-toggle" href="#w0-collapse4" data-toggle="collapse" data-parent="#w0">FormField</a>
</h4></div>
<div id="w0-collapse4" class="panel-collapse collapse"><div class="panel-body"><div class="form-group field-dynamicmodel-test">
<input type="text" id="dynamicmodel-test" class="form-control" name="DynamicModel[test]">
</div></div>
</div></div>
</div>
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 * @see https://github.com/yiisoft/yii2/issues/8357
*/ */

Loading…
Cancel
Save