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(dropdown): support dispatching enter key event on dropdown trigger #7752

Merged
merged 3 commits into from
Sep 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,37 @@ describe("calcite-dropdown", () => {
expect(calciteDropdownOpen).toHaveReceivedEventTimes(2);
expect(calciteDropdownClose).toHaveReceivedEventTimes(1);
});

it("toggles when Enter keydown is dispatched", async () => {
const page = await newE2EPage();
await page.setContent(html`
<calcite-dropdown>
<calcite-button slot="trigger">Open dropdown</calcite-button>
<calcite-dropdown-group selection-mode="single">
<calcite-dropdown-item id="item-1"> Dropdown Item Content </calcite-dropdown-item>
<calcite-dropdown-item id="item-2" selected> Dropdown Item Content </calcite-dropdown-item>
</calcite-dropdown-group>
</calcite-dropdown>
`);
const element = await page.find("calcite-dropdown");
const dropdownWrapper = await page.find(`calcite-dropdown >>> .calcite-dropdown-wrapper`);
const calciteDropdownOpen = await element.spyOnEvent("calciteDropdownOpen");
const calciteDropdownClose = await element.spyOnEvent("calciteDropdownClose");
const waitForCalciteDropdownOpen = page.waitForEvent("calciteDropdownOpen");

expect(await dropdownWrapper.isVisible()).toBe(false);

await page.$eval("calcite-button[slot='trigger']", (triggerEl: HTMLCalciteButtonElement) => {
// intentionally not pressing to avoid emitting `click`
triggerEl.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment here about only emitting keydown to avoid emitting click on the button?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to stick to puppeteer APIs, I think you could also do the following here:

const trigger = await page.find("calcite-button[slot='trigger']");
await trigger.callMethod("setFocus");
await page.keyboard.down("Enter"); // intentionally not pressing to avoid emitting `click`

Totally up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like the dispatch event better since it targets the issue more accurately and doesn't require any focusing.

});

await page.waitForChanges();
expect(await dropdownWrapper.isVisible()).toBe(true);
await waitForCalciteDropdownOpen;
expect(calciteDropdownOpen).toHaveReceivedEventTimes(1);
expect(calciteDropdownClose).toHaveReceivedEventTimes(0);
});
});

describe("Focus order with Tab key", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,10 @@ export class Dropdown
};

private keyDownHandler = (event: KeyboardEvent): void => {
const target = event.target as HTMLElement;

if (target !== this.referenceEl) {
if (!event.composedPath().includes(this.referenceEl)) {
return;
}

const { defaultPrevented, key } = event;

if (defaultPrevented) {
Expand Down
Loading