Skip to content

Commit

Permalink
fix(core/dropdown): handle multiple dropdowns at once (#393)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Leroux <[email protected]>
  • Loading branch information
nuke-ellington and danielleroux authored Feb 22, 2023
1 parent 2a0591f commit 4ab0246
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
17 changes: 8 additions & 9 deletions packages/core/src/components/dropdown/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -258,49 +258,48 @@ export class Dropdown {
if (
this.show === false ||
this.closeBehavior === false ||
this.anchorElement === target ||
this.triggerElement === target
this.anchorElement?.contains(target) ||
this.triggerElement?.contains(target)
) {
return;
}

switch (this.closeBehavior) {
case 'outside':
if (!this.dropdownRef.contains(target)) {
this.close(event);
this.close();
}
break;

case 'inside':
if (this.dropdownRef.contains(target)) {
this.close(event);
this.close();
}
break;

default:
this.close(event);
this.close();
}
}

private toggle(event?: Event) {
event?.preventDefault();
event?.stopPropagation();

this.show = !this.show;
this.showChanged.emit(this.show);
}

private open(event?: Event) {
event?.preventDefault();
event?.stopPropagation();

this.show = true;
this.showChanged.emit(true);
}

private close(event?: Event) {
event?.preventDefault();
event?.stopPropagation();
if (event?.defaultPrevented) {
return;
}

this.show = false;
this.showChanged.emit(false);
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/components/dropdown/test/dropdown.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ regressionTest.describe('dropdown', () => {
maxDiffPixelRatio: 0.05,
});
});

regressionTest('handle multiple', async ({ page }) => {
await page.goto('dropdown/test/multiple');

await page.locator('#trigger-a').click();
await page.waitForSelector('.dropdown-menu.show');

await page.locator('#trigger-b').click();
await page.waitForSelector('.dropdown-menu.show');

expect(await page.screenshot({ fullPage: true })).toMatchSnapshot();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions packages/core/src/components/dropdown/test/multiple/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!--
SPDX-FileCopyrightText: 2022 Siemens AG
SPDX-License-Identifier: MIT
-->

<html>
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0"
/>
<title>Stencil Component Starter</title>
</head>
<body style="width: 100vw; height: 100vh">
<ix-button id="trigger-a">Open A</ix-button>
<ix-button id="trigger-b">Open B</ix-button>
<ix-dropdown trigger="trigger-a">
<ix-dropdown-item label="Item 1"></ix-dropdown-item>
<ix-dropdown-item label="Item 2"></ix-dropdown-item>
</ix-dropdown>
<ix-dropdown trigger="trigger-b">
<ix-dropdown-item label="Item A"></ix-dropdown-item>
<ix-dropdown-item label="Item B"></ix-dropdown-item>
</ix-dropdown>
<script src="http://127.0.0.1:8080/scripts/e2e/load-e2e-runtime.js"></script>
</body>
</html>

0 comments on commit 4ab0246

Please sign in to comment.