From 42a9979d914948244afbccbaf6140886585f3de4 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 6 Dec 2021 11:18:22 +0100 Subject: [PATCH] web/elements: close dropdown when refresh event is dispatched Signed-off-by: Jens Langhammer --- web/src/elements/buttons/Dropdown.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/web/src/elements/buttons/Dropdown.ts b/web/src/elements/buttons/Dropdown.ts index 7065e086c..995327500 100644 --- a/web/src/elements/buttons/Dropdown.ts +++ b/web/src/elements/buttons/Dropdown.ts @@ -1,17 +1,32 @@ import { LitElement, TemplateResult, html } from "lit"; import { customElement } from "lit/decorators.js"; +import { EVENT_REFRESH } from "../../constants"; + @customElement("ak-dropdown") export class DropdownButton extends LitElement { + menu: HTMLElement | null; + constructor() { super(); - const menu = this.querySelector(".pf-c-dropdown__menu"); + this.menu = this.querySelector(".pf-c-dropdown__menu"); this.querySelectorAll("button.pf-c-dropdown__toggle").forEach((btn) => { btn.addEventListener("click", () => { - if (!menu) return; - menu.hidden = !menu.hidden; + if (!this.menu) return; + this.menu.hidden = !this.menu.hidden; }); }); + window.addEventListener(EVENT_REFRESH, this.clickHandler); + } + + clickHandler = (): void => { + if (!this.menu) return; + this.menu.hidden = true; + }; + + disconnectedCallback(): void { + super.disconnectedCallback(); + window.removeEventListener(EVENT_REFRESH, this.clickHandler); } render(): TemplateResult {