From aa9be414b4990cd07f013de3cd597ed38aa93f5f Mon Sep 17 00:00:00 2001 From: "n.vanderburg" Date: Fri, 24 Mar 2017 11:28:31 +0100 Subject: [PATCH] Added activateFirstVisibleTab() method to Tab widget close #187 --- CHANGELOG.md | 1 + Tabs.php | 23 +++++++++++++++++++-- tests/TabsTest.php | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03b019d..9979bad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Yii Framework 2 bootstrap extension Change Log - 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) - Bug #126: `yii\bootstrap\ToggleButtonGroup` was unable to work without model (makroxyz) - Bug #130: Fixed `yii\bootstrap\Collapse` to use pure numerical value on `content` property (meysampg) - Bug #137: Remove `role="navbar"` from `yii\bootstrap\NavBar` according to new aria specification (tino415) diff --git a/Tabs.php b/Tabs.php index 41a7d43..1cb7522 100644 --- a/Tabs.php +++ b/Tabs.php @@ -160,8 +160,8 @@ class Tabs extends Widget $headers = []; $panes = []; - if (!$this->hasActiveTab() && !empty($this->items)) { - $this->items[0]['active'] = true; + if (!$this->hasActiveTab()) { + $this->activateFirstVisibleTab(); } foreach ($this->items as $n => $item) { @@ -238,6 +238,25 @@ class Tabs extends Widget } /** + * Sets the first visible tab as active. + * + * This method activates the first tab that is visible and + * not explicitly set to inactive (`'active' => false`). + * @since 2.0.7 + */ + protected function activateFirstVisibleTab() + { + foreach ($this->items as $i => $item) { + $active = ArrayHelper::getValue($item, 'active', null); + $visible = ArrayHelper::getValue($item, 'visible', true); + if ($visible && $active !== false) { + $this->items[$i]['active'] = true; + return; + } + } + } + + /** * Normalizes dropdown item options by removing tab specific keys `content` and `contentOptions`, and also * configure `panes` accordingly. * @param string $itemNumber number of the item diff --git a/tests/TabsTest.php b/tests/TabsTest.php index e45ccf5..867d6d9 100644 --- a/tests/TabsTest.php +++ b/tests/TabsTest.php @@ -147,7 +147,7 @@ class TabsTest extends TestCase $out = Tabs::widget([ 'items' => [ [ - 'label' => 'Page1', 'content'=>'Page1' + 'label' => 'Page1', 'content' => 'Page1' ] ], 'tabContentOptions' => [ @@ -155,7 +155,63 @@ class TabsTest extends TestCase ] ]); - $this->assertContains($checkAttribute.'=', $out); + $this->assertContains($checkAttribute . '=', $out); $this->assertContains($checkValue, $out); } + + public function testActivateFirstVisibleTab() + { + $html = Tabs::widget([ + 'id'=>'mytab', + 'items' => [ + [ + 'label' => 'Tab 1', + 'content' => 'some content', + 'visible' => false + ], + [ + 'label' => 'Tab 2', + 'content' => 'some content' + ], + [ + 'label' => 'Tab 3', + 'content' => 'some content' + ], + [ + 'label' => 'Tab 4', + 'content' => 'some content' + ] + ] + ]); + $this->assertNotContains('
  • Tab 1
  • ', $html); + $this->assertContains('
  • Tab 2
  • ', $html); + } + + public function testActivateTab() + { + $html = Tabs::widget([ + 'id'=>'mytab', + 'items' => [ + [ + 'label' => 'Tab 1', + 'content' => 'some content', + 'visible'=>false + ], + [ + 'label' => 'Tab 2', + 'content' => 'some content' + ], + [ + 'label' => 'Tab 3', + 'content' => 'some content', + 'active' => true + ], + [ + 'label' => 'Tab 4', + 'content' => 'some content' + ] + ] + ]); + $this->assertContains('
  • Tab 3
  • ', $html); + } }