Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fruity-bags-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-resource-router': patch
---

pass down onKeyDown to Link
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,4 @@
"engines": {
"node": ">=16.0"
}
}
}
2 changes: 2 additions & 0 deletions src/common/utils/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { KeyboardEvent } from 'react';

export const isModifiedEvent = (event: { [key: string]: any }) =>
!!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);

Expand Down
2 changes: 2 additions & 0 deletions src/ui/link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
href = undefined,
to = undefined,
onClick = undefined,
onKeyDown = undefined,
onMouseEnter = undefined,
onMouseLeave = undefined,
onPointerDown = undefined,
Expand Down Expand Up @@ -103,6 +104,7 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
const handleLinkPress = (e: MouseEvent | KeyboardEvent) =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Given this is immediately split into keyboard vs non-keyboard inside handleNavigation, might as well just fully split this into two different things

handleNavigation(e, {
onClick,
onKeyDown,
target,
replace,
routerActions,
Expand Down
18 changes: 17 additions & 1 deletion src/ui/link/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,17 @@ describe('<Link />', () => {
describe('when the link has focus, and a keypress is fired', () => {
it('should navigate if the key was an `enter`', async () => {
const user = userEvent.setup();
renderInRouter('my link', { href: newPath });
const mockKeyHandler = jest.fn();

renderInRouter('my link', { href: newPath, onKeyDown: mockKeyHandler });

const linkElement = screen.getByRole('link', { name: 'my link' });
linkElement.focus();
await user.keyboard('{Enter}');

expect(HistoryMock.push).toHaveBeenCalledTimes(1);
expect(HistoryMock.push).toHaveBeenCalledWith(newPath, undefined);
expect(mockKeyHandler).not.toHaveBeenCalled();
});

it('should not navigate for any other key', async () => {
Expand All @@ -382,6 +385,19 @@ describe('<Link />', () => {

expect(HistoryMock.push).not.toHaveBeenCalled();
});

it('should respect onKeyDown as long as it is not {Enter}', async () => {
const user = userEvent.setup();
const mockKeyHandler = jest.fn();
renderInRouter('my link', { href: newPath, onKeyDown: mockKeyHandler });

const linkElement = screen.getByRole('link', { name: 'my link' });
linkElement.focus();
await user.keyboard('{a}');

expect(HistoryMock.push).not.toHaveBeenCalled();
expect(mockKeyHandler).toHaveBeenCalled();
});
});

describe('when styles are passed into Link, element should be rendered with styles', () => {
Expand Down
18 changes: 15 additions & 3 deletions src/ui/link/utils/handle-navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { KeyboardEvent, MouseEvent } from 'react';
import type { KeyboardEvent, MouseEvent } from 'react';

import { Route } from '../../../common/types';
import type { Route } from '../../../common/types';
import { isKeyboardEvent, isModifiedEvent } from '../../../common/utils/event';

type LinkNavigationEvent = MouseEvent | KeyboardEvent;
Expand All @@ -16,15 +16,27 @@ type LinkPressArgs = {
replace: boolean;
href: string;
onClick?: (e: LinkNavigationEvent) => void;
onKeyDown?: (e: KeyboardEvent<HTMLAnchorElement>) => void;
to: [Route, any] | void;
state?: unknown;
};

export const handleNavigation = (
event: any,
{ onClick, target, replace, routerActions, href, to, state }: LinkPressArgs
{
onClick,
onKeyDown,
target,
replace,
routerActions,
href,
to,
state,
}: LinkPressArgs
): void => {
if (isKeyboardEvent(event) && event.key !== 'Enter') {
onKeyDown?.(event as KeyboardEvent<HTMLAnchorElement>);

return;
}

Expand Down