Skip to content

Commit

Permalink
fix(Menu): Pass onClick through even when rendered as a link
Browse files Browse the repository at this point in the history
  • Loading branch information
dougmacknz committed Jul 18, 2024
1 parent 1167f2d commit 5546c7e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-crabs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kaizen/components": patch
---

Pass onClick through to MenuItem even when it is rendered as a link
35 changes: 28 additions & 7 deletions packages/components/src/Menu/_docs/MenuContentExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const MenuContentExample = ({
<MenuList heading={<MenuHeading>Buttons</MenuHeading>}>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
alert("Hello")
e.preventDefault()
Expand All @@ -36,7 +39,10 @@ export const MenuContentExample = ({
/>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
Expand All @@ -45,7 +51,10 @@ export const MenuContentExample = ({
/>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
Expand All @@ -55,7 +64,10 @@ export const MenuContentExample = ({
/>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
Expand All @@ -65,7 +77,10 @@ export const MenuContentExample = ({
/>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
Expand All @@ -78,15 +93,21 @@ export const MenuContentExample = ({
<MenuList heading={<MenuHeading>Buttons (no icons)</MenuHeading>}>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
label="Menu button no icons"
/>
<MenuItem
onClick={(
e: React.MouseEvent<HTMLButtonElement, MouseEvent>
e: React.MouseEvent<
HTMLButtonElement | HTMLAnchorElement,
MouseEvent
>
): void => {
e.preventDefault()
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type MenuItemProps = {
href?: string
// Only applicable if href is supplied above
target?: string
onClick?: (e: React.MouseEvent<HTMLButtonElement>) => void
onClick?: (e: React.MouseEvent<HTMLButtonElement | HTMLAnchorElement>) => void
icon?: JSX.Element
destructive?: boolean
disabled?: boolean
Expand Down Expand Up @@ -70,6 +70,7 @@ export const MenuItem = ({
<a
id={id}
href={href}
onClick={onClick}
className={className}
target={target}
// this tells screenreaders that this link represents the current page
Expand Down

0 comments on commit 5546c7e

Please sign in to comment.