From 309544b84cd5157bd8bf23c0a2603ee327d0bb1b Mon Sep 17 00:00:00 2001 From: Alex Powell Date: Thu, 22 Dec 2022 19:06:27 +1100 Subject: [PATCH] Allow Navbar to accept collapseOptions = false (#63) * Allow collapseOptions to be false in Navbar If `collapseOptions` is `false`, while `offcanvasOptions` is _also_ `false`, `renderToggleButton` throws an array access error (`$this->collapseOptions['id']];`) * Update CHANGELOG.md * Typo * Add unit test * Update NavBar.php Code style Co-authored-by: Alex Powell Co-authored-by: simialbi --- CHANGELOG.md | 2 ++ src/NavBar.php | 7 +++++-- tests/NavBarTest.php | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58d345b..556a3c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.5 under development ----------------------- +- Bug #62: Navbar can now accept `collapseOptions` to be `false` + 2.0.4 November 30, 2022 ----------------------- diff --git a/src/NavBar.php b/src/NavBar.php index ced797d..3b55c63 100644 --- a/src/NavBar.php +++ b/src/NavBar.php @@ -210,17 +210,20 @@ class NavBar extends Widget */ protected function renderToggleButton(): string { + if ($this->collapseOptions === false && $this->offcanvasOptions === false) { + return ''; + } + $options = $this->togglerOptions; Html::addCssClass($options, ['widget' => 'navbar-toggler']); if ($this->offcanvasOptions !== false) { $bsData = ['bs-toggle' => 'offcanvas', 'bs-target' => '#' . $this->offcanvasOptions['id']]; $aria = $this->offcanvasOptions['id']; - } else { + } elseif ($this->collapseOptions !== false) { $bsData = ['bs-toggle' => 'collapse', 'bs-target' => '#' . $this->collapseOptions['id']]; $aria = $this->collapseOptions['id']; } - return Html::button( $this->togglerContent, ArrayHelper::merge($options, [ diff --git a/tests/NavBarTest.php b/tests/NavBarTest.php index 580a423..e9caaf9 100644 --- a/tests/NavBarTest.php +++ b/tests/NavBarTest.php @@ -192,4 +192,26 @@ HTML; $this->assertEqualsWithoutLE($expected, $out); } + + public function testNoCollapse() + { + NavBar::$counter = 0; + + $out = NavBar::widget([ + 'brandLabel' => 'My Company', + 'brandUrl' => '/', + 'collapseOptions' => false, + ]); + + $expected = << + + +EXPECTED; + + $this->assertEqualsWithoutLE($expected, $out); + } }