Skip to content

Commit 9c02c93

Browse files
committed
Fix Link doesn't manage search-only to param
1 parent a597df1 commit 9c02c93

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

packages/ra-router-tanstack/src/tanStackRouterProvider.spec.tsx

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,41 @@ describe('tanStackRouterProvider', () => {
703703
).toBeInTheDocument();
704704
});
705705
});
706+
707+
it('should support location object with pathname and search', async () => {
708+
render(<LinkComponent />);
709+
await waitFor(() => {
710+
expect(
711+
screen.getByText('Go to Post #4 (with search)')
712+
).toBeInTheDocument();
713+
});
714+
715+
fireEvent.click(screen.getByText('Go to Post #4 (with search)'));
716+
717+
await waitFor(() => {
718+
expect(screen.getByText('Post Details')).toBeInTheDocument();
719+
});
720+
// Check that search params are preserved in location
721+
expect(screen.getByText(/\"search\": \"\?foo=bar\"/)).toBeInTheDocument();
722+
});
723+
724+
it('should support location object with only search (no pathname)', async () => {
725+
render(<LinkComponent />);
726+
await waitFor(() => {
727+
expect(
728+
screen.getByText('Go to same page with search param')
729+
).toBeInTheDocument();
730+
});
731+
732+
fireEvent.click(screen.getByText('Go to same page with search param'));
733+
734+
await waitFor(() => {
735+
// Should stay on the same page (Link Tests page)
736+
expect(screen.getByText('Link Component Tests')).toBeInTheDocument();
737+
});
738+
// Check that search params are added
739+
expect(screen.getByText(/\"search\": \"\?foo=bar\"/)).toBeInTheDocument();
740+
});
706741
});
707742

708743
describe('Routes', () => {

packages/ra-router-tanstack/src/tanStackRouterProvider.stories.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const dataProvider = fakeDataProvider(
6161
{ id: 1, title: 'Post #1', body: 'Hello World' },
6262
{ id: 2, title: 'Post #2', body: 'Second post' },
6363
{ id: 3, title: 'Post #3', body: 'Third post' },
64+
{ id: 4, title: 'Post #4', body: 'Fourth post' },
6465
],
6566
comments: [
6667
{ id: 1, post_id: 1, body: 'Nice post!' },
@@ -426,8 +427,17 @@ export const LinkComponent = () => {
426427
Go to Post #3 (with state)
427428
</LinkBase>
428429

429-
<h3>Current Location State</h3>
430-
<pre>{JSON.stringify(location.state, null, 2)}</pre>
430+
<h3>Link with Location object</h3>
431+
<LinkBase
432+
to={{ pathname: '/posts/4/show', search: '?foo=bar' }}
433+
>
434+
Go to Post #4 (with search)
435+
</LinkBase>
436+
437+
<h3>Link with no pathname change</h3>
438+
<LinkBase to={{ search: '?foo=bar' }}>
439+
Go to same page with search param
440+
</LinkBase>
431441
</div>
432442
);
433443
};

packages/ra-router-tanstack/src/tanStackRouterProvider.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ const useBlocker = (
424424
const Link = forwardRef<HTMLAnchorElement, RouterLinkProps>(
425425
({ to, replace, state, children, ...rest }, ref) => {
426426
const basename = useBasename();
427+
const currentLocation = useTanStackLocation();
427428

428429
// Helper to prepend basename to absolute paths
429430
const resolvePath = (path: string) => {
@@ -434,12 +435,15 @@ const Link = forwardRef<HTMLAnchorElement, RouterLinkProps>(
434435
return `${basename}${path}`;
435436
};
436437

437-
// Handle object `to` with pathname (e.g., { pathname: '/path', search: '?foo=bar' })
438+
// Handle object `to` (e.g., { pathname: '/path', search: '?foo=bar' })
438439
let resolvedTo: string;
439440
let resolvedState = state;
440-
if (typeof to === 'object' && to !== null && 'pathname' in to) {
441+
if (typeof to === 'object' && to !== null) {
441442
const loc = to as Partial<RouterLocation>;
442-
let path = resolvePath(loc.pathname ?? '/');
443+
// If no pathname provided, use current pathname to stay on current page
444+
let path = loc.pathname
445+
? resolvePath(loc.pathname)
446+
: currentLocation.pathname;
443447
if (loc.search) {
444448
path += loc.search.startsWith('?')
445449
? loc.search

0 commit comments

Comments
 (0)