-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Table grid navigation register (#1856)
- Loading branch information
Showing
13 changed files
with
269 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,70 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React, { createRef, forwardRef, useImperativeHandle, useState } from 'react'; | ||
import React, { createRef, forwardRef, useCallback, useImperativeHandle, useRef } from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { SingleTabStopNavigationContext } from '../../../../lib/components/internal/context/single-tab-stop-navigation-context'; | ||
import { | ||
FocusableChangeHandler, | ||
FocusableDefinition, | ||
SingleTabStopNavigationContext, | ||
} from '../../../../lib/components/internal/context/single-tab-stop-navigation-context'; | ||
|
||
interface ProviderRef { | ||
setCurrentTarget(element: null | Element): void; | ||
setCurrentTarget(focusTarget: null | Element, suppressed?: (null | Element)[]): void; | ||
} | ||
|
||
const FakeSingleTabStopNavigationProvider = forwardRef( | ||
({ children }: { children: React.ReactNode }, ref: React.Ref<ProviderRef>) => { | ||
const [focusTarget, setFocusTarget] = useState<null | Element>(null); | ||
( | ||
{ children, navigationActive }: { children: React.ReactNode; navigationActive: boolean }, | ||
ref: React.Ref<ProviderRef> | ||
) => { | ||
const focusablesRef = useRef(new Set<FocusableDefinition>()); | ||
const focusHandlersRef = useRef(new Map<FocusableDefinition, FocusableChangeHandler>()); | ||
const registerFocusable = useCallback((focusable: FocusableDefinition, changeHandler: FocusableChangeHandler) => { | ||
focusablesRef.current.add(focusable); | ||
focusHandlersRef.current.set(focusable, changeHandler); | ||
return () => { | ||
focusablesRef.current.delete(focusable); | ||
focusHandlersRef.current.delete(focusable); | ||
}; | ||
}, []); | ||
|
||
useImperativeHandle(ref, () => ({ setCurrentTarget: setFocusTarget })); | ||
useImperativeHandle(ref, () => ({ | ||
setCurrentTarget: (focusTarget: null | Element, suppressed: (null | Element)[] = []) => { | ||
focusablesRef.current.forEach(focusable => { | ||
const element = focusable.current; | ||
const handler = focusHandlersRef.current.get(focusable)!; | ||
handler(focusTarget, element ? suppressed.includes(element) : false); | ||
}); | ||
}, | ||
})); | ||
|
||
return ( | ||
<SingleTabStopNavigationContext.Provider value={{ focusTarget, navigationActive: !!focusTarget }}> | ||
<SingleTabStopNavigationContext.Provider value={{ registerFocusable, navigationActive }}> | ||
{children} | ||
</SingleTabStopNavigationContext.Provider> | ||
); | ||
} | ||
); | ||
|
||
export function renderWithSingleTabStopNavigation(ui: React.ReactNode) { | ||
export function renderWithSingleTabStopNavigation( | ||
ui: React.ReactNode, | ||
{ navigationActive = true }: { navigationActive?: boolean } = {} | ||
) { | ||
const providerRef = createRef<ProviderRef>(); | ||
const { container, rerender } = render( | ||
<FakeSingleTabStopNavigationProvider ref={providerRef}>{ui}</FakeSingleTabStopNavigationProvider> | ||
<FakeSingleTabStopNavigationProvider ref={providerRef} navigationActive={navigationActive}> | ||
{ui} | ||
</FakeSingleTabStopNavigationProvider> | ||
); | ||
return { | ||
container, | ||
rerender, | ||
setCurrentTarget: (element: null | Element) => { | ||
setCurrentTarget: (focusTarget: null | Element, suppressed: (null | Element)[] = []) => { | ||
if (!providerRef.current) { | ||
throw new Error('Provider is not ready'); | ||
} | ||
providerRef.current.setCurrentTarget(element); | ||
providerRef.current.setCurrentTarget(focusTarget, suppressed); | ||
}, | ||
}; | ||
} |
43 changes: 32 additions & 11 deletions
43
src/internal/context/single-tab-stop-navigation-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.