Skip to content

Commit

Permalink
Supported basePath for to prop when passed as string. Resolving acces… (
Browse files Browse the repository at this point in the history
#227)

* Supported basePath for to prop when passed as string. Resolving accessibility issues for new tabs/window interactions.

* Updated href to reference staticBasePath
  • Loading branch information
tpuric authored Mar 4, 2024
1 parent 07f5362 commit 13a946f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/ui/link/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
)) ||
''
: to;
const staticBasePath =
href == null && typeof to === 'string' ? routeAttributes.basePath : '';

const triggerPrefetch = useCallback(() => {
// ignore if async route not ready yet
Expand Down Expand Up @@ -145,7 +147,7 @@ const Link = forwardRef<HTMLButtonElement | HTMLAnchorElement, LinkProps>(
validLinkType,
{
...rest,
href: linkDestination,
href: `${staticBasePath}${linkDestination}`,
target,
onClick: handleLinkPress,
onKeyDown: handleLinkPress,
Expand Down
24 changes: 24 additions & 0 deletions src/ui/link/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,30 @@ describe('<Link />', () => {
expect(linkElement).toHaveAttribute('href', newPath);
});

it('should support the `to` prop with basePath', () => {
renderInRouter('my link', { to: '/my-page/1?foo=bar' }, '/base');
const linkElement = screen.getByRole('link', { name: 'my link' });
expect(linkElement).toHaveAttribute('href', `/base/my-page/1?foo=bar`);
});

it('should push history with correct link given basePath', async () => {
const user = userEvent.setup();
renderInRouter(
'my link',
{
to: '/my-page/1?foo=bar',
},
'/base'
);

await user.click(screen.getByRole('link', { name: 'my link' }));

expect(HistoryMock.push).toHaveBeenCalledWith(
'/base/my-page/1?foo=bar',
undefined
);
});

it('should pass props to the child element', () => {
renderInRouter('my link', {
...defaultProps,
Expand Down

0 comments on commit 13a946f

Please sign in to comment.