From 63ddbe87dd4bb56660294565ae361be3530bc986 Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Fri, 15 May 2015 12:17:22 +0300 Subject: [PATCH] Added `visible` option to `yii\bootstrap\Tab` widget items --- CHANGELOG.md | 1 + Tabs.php | 10 +++++++++- tests/TabsTest.php | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c12d53..094c6e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 bootstrap extension Change Log ----------------------- - Enh #38: Added object support for `content` option in `Collapse` class (pana1990, ItsReddi) +- Enh #40: Added `visible` option to `yii\bootstrap\Tab` widget items (klimov-paul) 2.0.4 May 10, 2015 ------------------ diff --git a/Tabs.php b/Tabs.php index 6e244c6..c31c120 100644 --- a/Tabs.php +++ b/Tabs.php @@ -70,7 +70,9 @@ class Tabs extends Widget * - url: string, optional, an external URL. When this is specified, clicking on this tab will bring * the browser to this URL. This option is available since version 2.0.4. * - options: array, optional, the HTML attributes of the tab pane container. - * - active: boolean, optional, whether the item tab header and pane should be visible or not. + * - active: boolean, optional, whether this item tab header and pane should be active. If no item is marked as + * 'active' explicitly - the first one will be activated. + * - visible: boolean, optional, whether the item tab header and pane should be visible or not. Defaults to true. * - items: array, optional, can be used instead of `content` to specify a dropdown items * configuration array. Each item can hold three extra keys, besides the above ones: * * active: boolean, optional, whether the item tab header and pane should be visible or not. @@ -148,6 +150,9 @@ class Tabs extends Widget } foreach ($this->items as $n => $item) { + if (!ArrayHelper::remove($item, 'visible', true)) { + continue; + } if (!array_key_exists('label', $item)) { throw new InvalidConfigException("The 'label' option is required."); } @@ -228,6 +233,9 @@ class Tabs extends Widget if (is_string($item)) { continue; } + if (isset($item['visible']) && !$item['visible']) { + continue; + } if (!array_key_exists('content', $item)) { throw new InvalidConfigException("The 'content' option is required."); } diff --git a/tests/TabsTest.php b/tests/TabsTest.php index 4f61098..43b432c 100644 --- a/tests/TabsTest.php +++ b/tests/TabsTest.php @@ -76,4 +76,34 @@ class TabsTest extends TestCase $this->assertContains($string, $out); } } + + public function testVisible() + { + Tabs::$counter = 0; + $html = Tabs::widget([ + 'items' => [ + [ + 'label' => 'Page1', 'content' => 'Page1', + ], + [ + 'label' => 'InvisiblePage', + 'content' => 'Invisible Page Content', + 'visible' => false + ], + [ + 'label' => 'Dropdown1', + 'items' => [ + ['label' => 'Page2', 'content' => 'Page2'], + ['label' => 'InvisibleItem', 'content' => 'Invisible Item Content', 'visible' => false], + ['label' => 'Page3', 'content' => 'Page3'], + ] + ], + ] + ]); + + $this->assertNotContains('InvisiblePage', $html); + $this->assertNotContains('Invisible Page Content', $html); + $this->assertNotContains('InvisibleItem', $html); + $this->assertNotContains('Invisible Item Content', $html); + } }