Skip to content

Commit

Permalink
fix: dropdowns should close on tab key
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryan610 committed May 3, 2024
1 parent a678844 commit 41adf4e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions web/hooks/use-dropdown-key-down.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@ type TUseDropdownKeyDown = {
};

export const useDropdownKeyDown: TUseDropdownKeyDown = (onEnterKeyDown, onEscKeyDown, stopPropagation = true) => {
const stopEventPropagation = (event: React.KeyboardEvent<HTMLElement>) => {
if (stopPropagation) {
event.stopPropagation();
event.preventDefault();
}
};
const stopEventPropagation = useCallback(
(event: React.KeyboardEvent<HTMLElement>) => {
if (stopPropagation) {
event.stopPropagation();
event.preventDefault();
}
},
[stopPropagation]
);

const handleKeyDown = useCallback(
(event: React.KeyboardEvent<HTMLElement>) => {
if (event.key === "Enter") {
stopEventPropagation(event);

onEnterKeyDown();
} else if (event.key === "Escape") {
stopEventPropagation(event);
onEscKeyDown();
}
} else if (event.key === "Tab") onEscKeyDown();
},
[onEnterKeyDown, onEscKeyDown, stopEventPropagation]
);
Expand Down

0 comments on commit 41adf4e

Please sign in to comment.