2024-01-10 06:51:24 +00:00
|
|
|
import { useCallback } from "react";
|
|
|
|
|
|
|
|
type TUseDropdownKeyDown = {
|
2024-01-31 10:06:55 +00:00
|
|
|
(onEnterKeyDown: () => void, onEscKeyDown: () => void): (event: React.KeyboardEvent<HTMLElement>) => void;
|
2024-01-10 06:51:24 +00:00
|
|
|
};
|
|
|
|
|
2024-01-31 10:06:55 +00:00
|
|
|
export const useDropdownKeyDown: TUseDropdownKeyDown = (onEnterKeyDown, onEscKeyDown) => {
|
2024-01-10 06:51:24 +00:00
|
|
|
const handleKeyDown = useCallback(
|
|
|
|
(event: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
event.stopPropagation();
|
2024-01-31 10:06:55 +00:00
|
|
|
event.preventDefault();
|
|
|
|
onEnterKeyDown();
|
|
|
|
} else if (event.key === "Escape") {
|
2024-01-10 06:51:24 +00:00
|
|
|
event.stopPropagation();
|
2024-01-31 10:06:55 +00:00
|
|
|
event.preventDefault();
|
|
|
|
onEscKeyDown();
|
2024-01-10 06:51:24 +00:00
|
|
|
}
|
|
|
|
},
|
2024-01-31 10:06:55 +00:00
|
|
|
[onEnterKeyDown, onEscKeyDown]
|
2024-01-10 06:51:24 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return handleKeyDown;
|
|
|
|
};
|