Skip to content

Commit

Permalink
Fix disabled menu items triggering click events (#4409)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding authored Nov 15, 2024
1 parent d2ffaa4 commit 1a29b4e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-bugs-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@salt-ds/core": patch
---

Fixed disabled MenuItems triggering onClick.
4 changes: 4 additions & 0 deletions packages/core/src/__tests__/__e2e__/menu/Menu.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ describe("Given a Menu", () => {
});

it("should support disabled items", () => {
const alertStub = cy.stub().as("alertStub");
cy.on("window:alert", alertStub);

cy.mount(<IconWithGroups />);
cy.findByRole("button", { name: "Open Menu" }).realClick();
cy.findByRole("menuitem", { name: "Paste" }).should(
Expand All @@ -175,6 +178,7 @@ describe("Given a Menu", () => {
cy.findByRole("menuitem", { name: "Paste" }).realClick();
cy.findByRole("menu").should("exist");
cy.findByRole("menuitem", { name: "Paste" }).should("not.be.focused");
cy.get("@alertStub").should("not.have.been.called");
});

it("should focus items on hover", () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/menu/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ export const MenuItem = forwardRef<HTMLDivElement, MenuItemProps>(
}
},
onClick(event: MouseEvent<HTMLDivElement>) {
onClick?.(event);
if (!triggersSubmenu && !disabled) {
tree?.events.emit("click");
if (!disabled) {
onClick?.(event);
if (!triggersSubmenu) {
tree?.events.emit("click");
}
}
},
onFocus(event: FocusEvent<HTMLDivElement>) {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/stories/menu/menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ export const IconWithGroups: StoryFn<typeof Menu> = (args) => {
<CopyIcon aria-hidden />
Copy
</MenuItem>
<MenuItem disabled>
<MenuItem
disabled
onClick={() => {
alert("Paste");
}}
>
<PasteIcon aria-hidden />
Paste
</MenuItem>
Expand Down

0 comments on commit 1a29b4e

Please sign in to comment.