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(action-menu): clicking an action menu item should call click event. #8627

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -402,13 +402,13 @@ describe("calcite-action-menu", () => {
expect(await trigger.getProperty("active")).toBe(false);
});

it.skip("should close on blur", async () => {
it.skip("should handle TAB navigation", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action> </calcite-action-menu
><button>test</button>`,
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `,
});

await page.waitForChanges();
Expand All @@ -429,14 +429,14 @@ describe("calcite-action-menu", () => {
expect(actions[1].getAttribute(activeAttr)).toBe(null);
expect(actions[2].getAttribute(activeAttr)).toBe(null);

const button = await page.find("button");
await button.focus();
await page.keyboard.press("Tab");

await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(false);
});

it("should click the active action and close the menu", async () => {
it("should click the active action on Enter key and close the menu", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
Expand Down Expand Up @@ -466,9 +466,43 @@ describe("calcite-action-menu", () => {
expect(actions[2].getAttribute(activeAttr)).toBe(null);

await page.keyboard.press("Enter");
await page.waitForChanges();

expect(await actionMenu.getProperty("open")).toBe(false);
expect(clickSpy).toHaveReceivedEventTimes(1);
});

it("should click the active action when clicked and close the menu", async () => {
const page = await newE2EPage({
html: html`<calcite-action-menu>
<calcite-action id="first" text="Add" icon="plus" text-enabled></calcite-action>
<calcite-action id="second" text="Add" icon="minus" text-enabled></calcite-action>
<calcite-action id="third" text="Add" icon="banana" text-enabled></calcite-action>
</calcite-action-menu> `,
});

await page.waitForChanges();

const actionMenu = await page.find("calcite-action-menu");
const actions = await page.findAll("calcite-action");

expect(await actionMenu.getProperty("open")).toBe(false);

await actionMenu.callMethod("setFocus");
await page.waitForChanges();

await page.keyboard.press("ArrowDown");
await page.waitForChanges();

const clickSpy = await actions[0].spyOnEvent("click");

expect(await actionMenu.getProperty("open")).toBe(true);
expect(actions[0].getAttribute(activeAttr)).toBe("");
expect(actions[1].getAttribute(activeAttr)).toBe(null);
expect(actions[2].getAttribute(activeAttr)).toBe(null);

await page.$eval("calcite-action", (el: HTMLCalciteActionElement) => el.click());
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why this is calling click directly vs clicking via the E2E APIs? If so, can you add an explainer comment, otherwise, I'd suggest await page.click("calcite-action"); to simplify.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah it wasn't triggering the closing of the menu when await page.click("calcite-action") was used.


expect(await actionMenu.getProperty("open")).toBe(false);
expect(clickSpy).toHaveReceivedEventTimes(1);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ export class ActionMenu implements LoadableComponent {

menuButtonEl.addEventListener("pointerdown", this.menuButtonClick);
menuButtonEl.addEventListener("keydown", this.menuButtonKeyDown);
menuButtonEl.addEventListener("blur", this.menuButtonBlur);
};

disconnectMenuButtonEl = (): void => {
Expand All @@ -236,7 +235,6 @@ export class ActionMenu implements LoadableComponent {

menuButtonEl.removeEventListener("pointerdown", this.menuButtonClick);
menuButtonEl.removeEventListener("keydown", this.menuButtonKeyDown);
menuButtonEl.removeEventListener("blur", this.menuButtonBlur);
};

setMenuButtonEl = (event: Event): void => {
Expand Down Expand Up @@ -419,10 +417,6 @@ export class ActionMenu implements LoadableComponent {
return !!supportedKeys.find((k) => k === key);
}

private menuButtonBlur = (): void => {
this.open = false;
};

menuButtonKeyDown = (event: KeyboardEvent): void => {
const { key } = event;
const { actionElements, activeMenuItemIndex, open } = this;
Expand All @@ -443,6 +437,11 @@ export class ActionMenu implements LoadableComponent {
action ? action.click() : this.toggleOpen(false);
}

if (key === "Tab") {
this.open = false;
return;
}

if (key === "Escape") {
this.toggleOpen(false);
event.preventDefault();
Expand Down
Loading