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 <alexanderp@agilitycis.com>
Co-authored-by: simialbi <simi.albi@outlook.com>
This commit is contained in:
Alex Powell 2022-12-22 19:06:27 +11:00 committed by GitHub
parent 0ce35f80ca
commit 309544b84c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 2 deletions

View File

@ -4,6 +4,8 @@ Yii Framework 2 bootstrap5 extension Change Log
2.0.5 under development 2.0.5 under development
----------------------- -----------------------
- Bug #62: Navbar can now accept `collapseOptions` to be `false`
2.0.4 November 30, 2022 2.0.4 November 30, 2022
----------------------- -----------------------

View File

@ -210,17 +210,20 @@ class NavBar extends Widget
*/ */
protected function renderToggleButton(): string protected function renderToggleButton(): string
{ {
if ($this->collapseOptions === false && $this->offcanvasOptions === false) {
return '';
}
$options = $this->togglerOptions; $options = $this->togglerOptions;
Html::addCssClass($options, ['widget' => 'navbar-toggler']); Html::addCssClass($options, ['widget' => 'navbar-toggler']);
if ($this->offcanvasOptions !== false) { if ($this->offcanvasOptions !== false) {
$bsData = ['bs-toggle' => 'offcanvas', 'bs-target' => '#' . $this->offcanvasOptions['id']]; $bsData = ['bs-toggle' => 'offcanvas', 'bs-target' => '#' . $this->offcanvasOptions['id']];
$aria = $this->offcanvasOptions['id']; $aria = $this->offcanvasOptions['id'];
} else { } elseif ($this->collapseOptions !== false) {
$bsData = ['bs-toggle' => 'collapse', 'bs-target' => '#' . $this->collapseOptions['id']]; $bsData = ['bs-toggle' => 'collapse', 'bs-target' => '#' . $this->collapseOptions['id']];
$aria = $this->collapseOptions['id']; $aria = $this->collapseOptions['id'];
} }
return Html::button( return Html::button(
$this->togglerContent, $this->togglerContent,
ArrayHelper::merge($options, [ ArrayHelper::merge($options, [

View File

@ -192,4 +192,26 @@ HTML;
$this->assertEqualsWithoutLE($expected, $out); $this->assertEqualsWithoutLE($expected, $out);
} }
public function testNoCollapse()
{
NavBar::$counter = 0;
$out = NavBar::widget([
'brandLabel' => 'My Company',
'brandUrl' => '/',
'collapseOptions' => false,
]);
$expected = <<<EXPECTED
<nav id="w0" class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="/">My Company</a>
</div>
</nav>
EXPECTED;
$this->assertEqualsWithoutLE($expected, $out);
}
} }