From cf300a615cd7514fbead9dbdcae6c80fa9ea8023 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Mon, 11 Apr 2022 13:47:06 -0700 Subject: [PATCH] Keyboard navigation for the bottom buttons --- .../templates/article/EditLabelsControl.tsx | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/web/components/templates/article/EditLabelsControl.tsx b/packages/web/components/templates/article/EditLabelsControl.tsx index d0bfd9f31..edb5ea129 100644 --- a/packages/web/components/templates/article/EditLabelsControl.tsx +++ b/packages/web/components/templates/article/EditLabelsControl.tsx @@ -227,22 +227,36 @@ export function EditLabelsControl(props: EditLabelsControlProps): JSX.Element { // Move focus through the labels list on tab or arrow up/down keys const [focusedIndex, setFocusedIndex] = useState(undefined) const handleKeyDown = useCallback((event: React.KeyboardEvent) => { - const maxIndex = filteredLabels.length + 2 + const maxIndex = filteredLabels.length + 1 if (event.key === 'ArrowUp') { event.preventDefault() + let newIndex = focusedIndex if (focusedIndex) { - setFocusedIndex(Math.max(0, focusedIndex - 1)) + newIndex = Math.max(0, focusedIndex - 1) } else { - setFocusedIndex(undefined) + newIndex = undefined } + // If the `Create New label` button isn't visible we skip it + // when navigating with the arrow keys + if (focusedIndex === maxIndex && !filterText) { + newIndex = maxIndex - 2 + } + setFocusedIndex(newIndex) } if (event.key === 'ArrowDown' || event.key === 'Tab') { event.preventDefault() + let newIndex = focusedIndex if (focusedIndex === undefined) { - setFocusedIndex(0) + newIndex = 0 } else { - setFocusedIndex(Math.min(maxIndex, focusedIndex + 1)) + newIndex = Math.min(maxIndex, focusedIndex + 1) } + // If the `Create New label` button isn't visible we skip it + // when navigating with the arrow keys + if (focusedIndex === maxIndex - 2 && !filterText) { + newIndex = maxIndex + } + setFocusedIndex(newIndex) } if (event.key === 'Enter') { event.preventDefault() @@ -310,7 +324,7 @@ export function EditLabelsControl(props: EditLabelsControlProps): JSX.Element { {`Create new label "${filterText}"`} - + )}