Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("doesn't intercept tab key if not the first or last tabbable item", () => {
const focusableChildren = (
<>
<button>Focusable button</button>
<input> setTimeout(() => ref && ref.focus())} />
<button>Another button</button>
);
const { getByTestId } = render(
);
fireEvent.keyDown(getByTestId('container'), { keyCode: KEY_CODES.TAB });
expect(focusSpy).toHaveBeenCalledTimes(3);
});
it('focuses container if no tabbable elements found', () => {
const { getByTestId } = render();
const container = getByTestId('container');
fireEvent.keyDown(container, { keyCode: KEY_CODES.TAB });
expect(focusSpy).toHaveBeenCalledTimes(3);
expect(focusSpy).toHaveBeenLastCalledWith(container);
});
onKeyDown: composeEventHandlers(onKeyDown, (event: KeyboardEvent) => {
if (event.keyCode !== KEY_CODES.TAB) {
return;
}
validateContainerRef();
const tabbableNodes = getTabbableNodes();
if (
event.shiftKey &&
(event.target === tabbableNodes.firstItem || event.target === currentRef)
) {
focusElement(tabbableNodes.lastItem);
event.preventDefault();
}
if (!event.shiftKey && event.target === tabbableNodes.lastItem) {
focusElement(tabbableNodes.firstItem);