From 6a4f64aa11f4b868093c1d59d4be77f6697c51c9 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Tue, 18 Apr 2017 22:48:13 +0200 Subject: [PATCH] Fixes #107: Added `yii\bootstrap\Collapse::$autoCloseItems` to allow keeping multiple items open at the same time --- CHANGELOG.md | 2 ++ Collapse.php | 19 ++++++++++++++----- tests/CollapseTest.php | 24 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 532fe49..513c385 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Yii Framework 2 bootstrap extension Change Log 2.0.7 under development ----------------------- +- Enh #107: Added `yii\bootstrap\Collapse::$autoCloseItems` to allow keeping multiple items open at the same time (cebe) +- Enh #131: Added `tabContentOptions` to set HTML attributes for 'tab-content' container in `Tabs` widget (AndrewKorpusov) - Bug #157: Active status of multilevel submenu items of the `yii\bootstrap\Nav` class is determined correctly now (PowerGamer1) - 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) diff --git a/Collapse.php b/Collapse.php index 7fa5f2f..3c18c14 100644 --- a/Collapse.php +++ b/Collapse.php @@ -90,6 +90,12 @@ class Collapse extends Widget * @var boolean whether the labels for header items should be HTML-encoded. */ public $encodeLabels = true; + /** + * @var boolean whether to close other items if an item is opened. Defaults to `true` which causes an + * accordion effect. Set this to `false` to allow keeping multiple items open at once. + * @since 2.0.7 + */ + public $autoCloseItems = true; /** @@ -164,11 +170,14 @@ class Collapse extends Widget $header = Html::encode($header); } - $headerToggle = Html::a($header, '#' . $id, [ - 'class' => 'collapse-toggle', - 'data-toggle' => 'collapse', - 'data-parent' => '#' . $this->options['id'] - ]) . "\n"; + $headerOptions = [ + 'class' => 'collapse-toggle', + 'data-toggle' => 'collapse', + ]; + if ($this->autoCloseItems) { + $headerOptions['data-parent'] = '#' . $this->options['id']; + } + $headerToggle = Html::a($header, '#' . $id, $headerOptions) . "\n"; $header = Html::tag('h4', $headerToggle, ['class' => 'panel-title']); diff --git a/tests/CollapseTest.php b/tests/CollapseTest.php index 06c4400..e550200 100644 --- a/tests/CollapseTest.php +++ b/tests/CollapseTest.php @@ -214,4 +214,28 @@ HTML HTML , $output); } + + public function testAutoCloseItems() + { + $items = [ + [ + 'label' => 'Item 1', + 'content' => 'Content 1', + ], + [ + 'label' => 'Item 2', + 'content' => 'Content 2', + ], + ]; + + $output = Collapse::widget([ + 'items' => $items + ]); + $this->assertContains('data-parent="', $output); + $output = Collapse::widget([ + 'autoCloseItems' => false, + 'items' => $items + ]); + $this->assertNotContains('data-parent="', $output); + } }