Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core/dropdown): clean up disconnected dropdowns #1483

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-hornets-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@siemens/ix": patch
---

fix(core/dropdown): clean up disconnected dropdowns
18 changes: 17 additions & 1 deletion packages/core/src/components/dropdown/dropdown-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,23 @@ class DropdownController {
}

disconnected(dropdown: DropdownInterface) {
this.dropdowns.delete(dropdown.getId());
const id = dropdown.getId();
this.removeFromSubmenuIds(id);
this.dropdowns.delete(id);
}

removeFromSubmenuIds(id: string) {
this.dropdowns.forEach((dropdown) => {
const submenuIds = this.submenuIds[dropdown.getId()];
if (submenuIds) {
const index = submenuIds.indexOf(id);
if (index > -1) {
submenuIds.splice(index, 1);
}
}
});

delete this.submenuIds[id];
}

discoverSubmenus() {
Expand Down
32 changes: 23 additions & 9 deletions packages/core/src/components/dropdown/test/dropdown.ct.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
* SPDX-License-Identifier: MIT
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/*
* SPDX-FileCopyrightText: 2023 Siemens AG
*
Expand Down Expand Up @@ -634,6 +625,29 @@ test.describe('resolve during element connect', () => {
});
});

test('Child dropdown disconnects', async ({ mount, page }) => {
await mount(`<ix-button id="trigger">Open</ix-icon-button>
<ix-dropdown closeBehavior="outside" trigger="trigger">
<ix-dropdown-item id="item-1">Item level 1</ix-dropdown-item>
<ix-dropdown-button label="Nested">
<ix-dropdown-item id="item-1">Item level 2</ix-dropdown-item>
</ix-dropdown-button>
</ix-dropdown>`);
const trigger = page.locator('ix-button').first();
await trigger.click();
const dropdown = page.locator('ix-dropdown').first();

await expect(dropdown).toBeVisible();

await dropdown.evaluate((dd) => {
dd.removeChild(dd.querySelector('ix-dropdown-button'));
});

await trigger.click();
await trigger.click();
await expect(dropdown).toBeVisible();
});

test.describe('A11y', () => {
test.describe('Keyboard navigation', () => {
test.beforeEach(async ({ page, mount }) => {
Expand Down
Loading