From 61fab45f82761dcbdfc4aa2ff297f97cb3b672fd Mon Sep 17 00:00:00 2001 From: Omri Lavi Date: Wed, 26 Jan 2022 17:25:02 +0200 Subject: [PATCH] Skipping disabled components while navigating (#488) * implemented * updated story --- .../GridKeyboardNavigationContext.js | 5 +- ...seGridKeyboardNavigationContext.stories.js | 11 +- ...eGridKeyboardNavigationContext.stories.mdx | 119 +++++++++++++----- ...GridKeyboardNavigationContext.stories.scss | 22 ++-- .../__tests__/helper.jest.js | 69 +++++++++- .../GridKeyboardNavigationContext/helper.js | 28 ++--- 6 files changed, 192 insertions(+), 62 deletions(-) diff --git a/src/components/GridKeyboardNavigationContext/GridKeyboardNavigationContext.js b/src/components/GridKeyboardNavigationContext/GridKeyboardNavigationContext.js index ad43be448d..009e19c680 100644 --- a/src/components/GridKeyboardNavigationContext/GridKeyboardNavigationContext.js +++ b/src/components/GridKeyboardNavigationContext/GridKeyboardNavigationContext.js @@ -3,6 +3,7 @@ import useEventListener from "../../hooks/useEventListener"; import { focusElementWithDirection, getDirectionMaps, + getNextElementToFocusInDirection, getOppositeDirection, getOutmostElementInDirection } from "./helper"; @@ -34,8 +35,8 @@ export const useGridKeyboardNavigationContext = (positions, wrapperRef) => { const onOutboundNavigation = useCallback( (elementRef, direction) => { - const maybeNextElement = directionMaps[direction].get(elementRef); - if (maybeNextElement?.current) { + const maybeNextElement = getNextElementToFocusInDirection(directionMaps[direction], elementRef); + if (maybeNextElement) { elementRef.current?.blur(); focusElementWithDirection(maybeNextElement, direction); return; diff --git a/src/components/GridKeyboardNavigationContext/__stories__/useGridKeyboardNavigationContext.stories.js b/src/components/GridKeyboardNavigationContext/__stories__/useGridKeyboardNavigationContext.stories.js index 4377f90acd..2bbaf9a5ee 100644 --- a/src/components/GridKeyboardNavigationContext/__stories__/useGridKeyboardNavigationContext.stories.js +++ b/src/components/GridKeyboardNavigationContext/__stories__/useGridKeyboardNavigationContext.stories.js @@ -12,7 +12,7 @@ const PADDING_PX = 24; const ON_CLICK = action("Selected"); -export const DummyNavigableGrid = forwardRef(({ itemsCount, numberOfItemsInLine, itemPrefix }, ref) => { +export const DummyNavigableGrid = forwardRef(({ itemsCount, numberOfItemsInLine, itemPrefix, disabled }, ref) => { const width = useMemo(() => numberOfItemsInLine * ELEMENT_WIDTH_PX + 2 * PADDING_PX, [numberOfItemsInLine]); const items = useMemo(() => range(itemsCount).map(num => `${itemPrefix} ${num}`), [itemPrefix, itemsCount]); const getItemByIndex = useCallback(index => items[index], [items]); @@ -25,12 +25,19 @@ export const DummyNavigableGrid = forwardRef(({ itemsCount, numberOfItemsInLine, }); const onClickByIndex = useCallback(index => () => onSelectionAction(index), [onSelectionAction]); return ( -
+
{items.map((item, index) => (