From 82b4b7c2c8c021d5b040e448e4e48f82a23104ec Mon Sep 17 00:00:00 2001 From: Joe Heffernan Date: Thu, 31 Oct 2024 19:53:21 -0700 Subject: [PATCH] remove nested conditional in key handler --- src/components/CustomDropdown/index.tsx | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/components/CustomDropdown/index.tsx b/src/components/CustomDropdown/index.tsx index 5028942c..bfac623a 100644 --- a/src/components/CustomDropdown/index.tsx +++ b/src/components/CustomDropdown/index.tsx @@ -62,22 +62,21 @@ const CustomDropdown: React.FC = ({ * Manually handling keydown and hover behavior because * our focus management overrides the defaults of the antd components. */ + const openTriggers = new Set(["Enter", " ", "ArrowDown"]); + const handleKeyDown: KeyboardEventHandler = (event) => { - if (dropdownState === DropdownState.CLOSED) { - if ( - (event.key === "Enter" || - event.key === " " || - event.key === "ArrowDown") && - !event.defaultPrevented - ) { - event.preventDefault(); - setDropdownState(DropdownState.FORCED_OPEN); // Opened by keyboard - } - } else if (event.key === "Escape") { + if (event.key === "Escape") { event.preventDefault(); setDropdownState(DropdownState.CLOSED); triggerRef.current?.focus(); } + if ( + openTriggers.has(event.key) && + dropdownState === DropdownState.CLOSED + ) { + event.preventDefault(); + setDropdownState(DropdownState.FORCED_OPEN); // Opened by keyboard + } }; const handleMouseEnter = () => {