Skip to content

Commit

Permalink
fix elements with click handlers must have at least one keyboard list…
Browse files Browse the repository at this point in the history
…ener
  • Loading branch information
luiqor committed Dec 17, 2024
1 parent 2518256 commit 45347c2
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/design-system/components/menu-item/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ interface MenuItemProps extends CommonMenuItemProps {
const MenuItem: FC<MenuItemProps> = ({
title,
additionalInfo,
graphics,
onRemove,
alignVariant = 'left',
colorVariant = MenuItemColorVariant.Default,
density = 1,
graphics,
isDropdown = false,
isToggled = false,
isBottomBorder = false,
isDisabled = false,
variant = MenuItemVariant.Default,
onClick = () => {}
onClick,
onRemove,
variant = MenuItemVariant.Default
}) => {
const graphicsRef = useRef<HTMLDivElement | null>(null)

Expand All @@ -46,12 +46,28 @@ const MenuItem: FC<MenuItemProps> = ({
}
}

onClick()
onClick?.()
}

const handleGraphicsClick = (
event: React.MouseEvent | React.KeyboardEvent
) => {
event.stopPropagation()
onClick?.()
}

const handleEnterOrSpaceKeyDown = (
event: React.KeyboardEvent,
handleAction: (event: React.KeyboardEvent) => void
) => {
if (event.key === 'Enter' || event.key === ' ') {
handleAction(event)
}
}

const handleGraphicsClick = (event: React.MouseEvent) => {
const handleRemoveItem = (event: React.MouseEvent | React.KeyboardEvent) => {
event.stopPropagation()
onClick()
onRemove?.()
}

return (
Expand All @@ -75,7 +91,10 @@ const MenuItem: FC<MenuItemProps> = ({
<div
className='s2s-item__graphics'
onClick={handleGraphicsClick}
onKeyDown={handleClick}
ref={graphicsRef}
role='button'
tabIndex={0}
>
{graphics}
</div>
Expand All @@ -93,10 +112,12 @@ const MenuItem: FC<MenuItemProps> = ({
{onRemove && !isDropdown && !isDisabled && (
<div
className='s2s-item__graphics'
onClick={(event) => {
event.stopPropagation()
onRemove()
onClick={handleRemoveItem}
onKeyDown={(event) => {
handleEnterOrSpaceKeyDown(event, handleRemoveItem)
}}
role='button'
tabIndex={0}
>
<CloseRounded className='s2s-item__close' />
</div>
Expand Down

0 comments on commit 45347c2

Please sign in to comment.