From 1c8504e9f1587804ad6c1f4f645a254f3e69b59b Mon Sep 17 00:00:00 2001 From: simialbi Date: Wed, 30 Nov 2022 09:17:05 +0100 Subject: [PATCH] Fixed #58 --- CHANGELOG.md | 1 + src/BootstrapWidgetTrait.php | 8 ++++---- src/Dropdown.php | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b08f4b2..7e3f80a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 bootstrap5 extension Change Log 2.0.4 under development ----------------------- +- Bug #58: Dropdown clientEvents did not fire because they need to be bound on parent (simialbi) - Bug #43: Accordion widget does not set "collapsed" class on toggle button (simialbi) - Enh #39: Add inline mode to `BaseHtml::checkboxList()` and `BaseHtml::radioList()` (WinterSilence) - Enh #40: Breadcrumbs refactoring (WinterSilence) diff --git a/src/BootstrapWidgetTrait.php b/src/BootstrapWidgetTrait.php index 5c3ce9e..09d1f69 100644 --- a/src/BootstrapWidgetTrait.php +++ b/src/BootstrapWidgetTrait.php @@ -94,7 +94,6 @@ trait BootstrapWidgetTrait // 'popover', 'toast' and 'tooltip' plugins not activates via data attributes if ( $this->clientOptions !== false - || !empty($this->clientOptions) || in_array($name, ['popover', 'toast', 'tooltip'], true) ) { $name = ucfirst($name); @@ -103,20 +102,21 @@ trait BootstrapWidgetTrait $view->registerJs("(new bootstrap.$name('#$id', $options));"); } - $this->registerClientEvents(); + $this->registerClientEvents($name); } } /** * Registers JS event handlers that are listed in [[clientEvents]]. */ - protected function registerClientEvents() + protected function registerClientEvents(string $name = null) { if (!empty($this->clientEvents)) { $id = $this->options['id']; $js = []; + $appendix = ($name === 'dropdown') ? '.parentElement' : ''; foreach ($this->clientEvents as $event => $handler) { - $js[] = "document.getElementById('$id').addEventListener('$event', $handler);"; + $js[] = "document.getElementById('$id')$appendix.addEventListener('$event', $handler);"; } $this->getView()->registerJs(implode("\n", $js)); } diff --git a/src/Dropdown.php b/src/Dropdown.php index 41d69bc..03afd37 100644 --- a/src/Dropdown.php +++ b/src/Dropdown.php @@ -85,7 +85,7 @@ class Dropdown extends Widget public function run(): string { BootstrapPluginAsset::register($this->getView()); - $this->registerClientEvents(); + $this->registerClientEvents('dropdown'); return $this->renderItems($this->items, $this->options); }