From 7b95f9518cb479a97211f89d39845e67e724d05c Mon Sep 17 00:00:00 2001 From: Braden MacDonald Date: Mon, 16 Dec 2024 14:00:58 -0800 Subject: [PATCH] refactor: cleanups suggested by Adam --- .../tests/useIndexOfLastVisibleChild.test.tsx | 8 ++-- src/hooks/useArrowKeyNavigation.tsx | 38 ++++++++++++------- src/hooks/useIndexOfLastVisibleChild.tsx | 4 +- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/hooks/tests/useIndexOfLastVisibleChild.test.tsx b/src/hooks/tests/useIndexOfLastVisibleChild.test.tsx index d47377072d..67d75c91e4 100644 --- a/src/hooks/tests/useIndexOfLastVisibleChild.test.tsx +++ b/src/hooks/tests/useIndexOfLastVisibleChild.test.tsx @@ -12,12 +12,12 @@ window.ResizeObserver = window.ResizeObserver })); function TestComponent() { - const [containerElementRef, setContainerElementRef] = React.useState(null); - const overflowElementRef = React.useRef(null); - const indexOfLastVisibleChild = useIndexOfLastVisibleChild(containerElementRef, overflowElementRef.current); + const containerElementRef = React.useRef(null); + const overflowElementRef = React.useRef(null); + const indexOfLastVisibleChild = useIndexOfLastVisibleChild(containerElementRef.current, overflowElementRef.current); return ( -
+
Element 1
Element 2
Element 3
diff --git a/src/hooks/useArrowKeyNavigation.tsx b/src/hooks/useArrowKeyNavigation.tsx index 862d3f8d14..1700bbafbc 100644 --- a/src/hooks/useArrowKeyNavigation.tsx +++ b/src/hooks/useArrowKeyNavigation.tsx @@ -1,24 +1,24 @@ import { useRef, useEffect } from 'react'; -/** - * A React hook to enable arrow key navigation on a component. - */ +interface HandleEnterArgs { + event: KeyboardEvent; + currentIndex: number; + activeElement: HTMLElement; +} -function handleEnter( - { event, currentIndex, activeElement }: { event: KeyboardEvent, currentIndex: number, activeElement: HTMLElement }, -) { +function handleEnter({ event, currentIndex, activeElement }: HandleEnterArgs) { if (currentIndex === -1) { return; } activeElement.click(); event.preventDefault(); } -function handleArrowKey( - { event, currentIndex, availableElements }: { - event: KeyboardEvent, - currentIndex: number, - availableElements: NodeListOf, - }, -) { +interface HandleArrowKeyArgs { + event: KeyboardEvent; + currentIndex: number; + availableElements: NodeListOf; +} + +function handleArrowKey({ event, currentIndex, availableElements }: HandleArrowKeyArgs) { // If the focus isn't in the container, focus on the first thing if (currentIndex === -1) { availableElements[0].focus(); } @@ -44,6 +44,13 @@ function handleArrowKey( event.preventDefault(); } +interface HandleEventsArgs { + event: KeyboardEvent; + ignoredKeys?: string[]; + parentNode: HTMLElement | undefined; + selectors?: string; +} + /** * Implement arrow key navigation for the given parentNode */ @@ -52,7 +59,7 @@ function handleEvents({ ignoredKeys = [], parentNode, selectors = 'a,button,input', -}: { event: KeyboardEvent, ignoredKeys?: string[], parentNode: HTMLElement | undefined, selectors?: string }) { +}: HandleEventsArgs) { if (!parentNode) { return; } const { key } = event; @@ -90,6 +97,9 @@ export interface ArrowKeyNavProps { ignoredKeys?: string[]; } +/** + * A React hook to enable arrow key navigation on a component. + */ export default function useArrowKeyNavigation(props: ArrowKeyNavProps = {}) { const { selectors, ignoredKeys } = props; const parentNode = useRef(); diff --git a/src/hooks/useIndexOfLastVisibleChild.tsx b/src/hooks/useIndexOfLastVisibleChild.tsx index 0428ef97b5..2111152981 100644 --- a/src/hooks/useIndexOfLastVisibleChild.tsx +++ b/src/hooks/useIndexOfLastVisibleChild.tsx @@ -11,8 +11,8 @@ import { useLayoutEffect, useState } from 'react'; * @param overflowElementRef - overflow element */ const useIndexOfLastVisibleChild = ( - containerElementRef: Element | null, - overflowElementRef: Element | null, + containerElementRef: HTMLElement | null, + overflowElementRef: HTMLElement | null, ): number => { const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);