diff --git a/packages/ra-core/src/routing/useGetPathForRecord.spec.tsx b/packages/ra-core/src/routing/useGetPathForRecord.spec.tsx index 83b5f70e63..0e8af8495f 100644 --- a/packages/ra-core/src/routing/useGetPathForRecord.spec.tsx +++ b/packages/ra-core/src/routing/useGetPathForRecord.spec.tsx @@ -7,6 +7,7 @@ import { InferredShowLink, InferredShowLinkWithAccessControl, NoAuthProvider, + SlowLoading, } from './useGetPathForRecord.stories'; import { AuthProvider } from '..'; @@ -83,4 +84,11 @@ describe('useGetPathForRecord', () => { ) ).toEqual('/posts/123/show'); }); + it('should recompute the path when the record changes', async () => { + render(); + await screen.findByText('Show no link'); + screen.getByText('Load record').click(); + const link = await screen.findByText('Show', { selector: 'a' }); + expect(link.getAttribute('href')).toEqual('/posts/123/show'); + }); }); diff --git a/packages/ra-core/src/routing/useGetPathForRecord.stories.tsx b/packages/ra-core/src/routing/useGetPathForRecord.stories.tsx index bc368aa4e4..06bbb1289c 100644 --- a/packages/ra-core/src/routing/useGetPathForRecord.stories.tsx +++ b/packages/ra-core/src/routing/useGetPathForRecord.stories.tsx @@ -85,6 +85,37 @@ export const InferredShowLink = () => ( ); +export const SlowLoading = () => { + const [record, setRecord] = React.useState(undefined); + const handleClick = () => { + setRecord({ id: 123 }); + }; + return ( + + + + + +
+ +
+ +
+
+
+
+
+ ); +}; + export const AccessControlWithLinkTypeProvided = ({ authProvider = { login: () => Promise.resolve(), diff --git a/packages/ra-core/src/routing/useGetPathForRecord.ts b/packages/ra-core/src/routing/useGetPathForRecord.ts index f2dd4954fa..7d1fa19d77 100644 --- a/packages/ra-core/src/routing/useGetPathForRecord.ts +++ b/packages/ra-core/src/routing/useGetPathForRecord.ts @@ -125,6 +125,18 @@ export const useGetPathForRecord = ( }) : false ); + return; + } + + // handle string case + if (link) { + setPath( + createPath({ + resource, + id: record.id, + type: link, + }) + ); } }, [ createPath, diff --git a/packages/ra-ui-materialui/src/field/ReferenceField.stories.tsx b/packages/ra-ui-materialui/src/field/ReferenceField.stories.tsx index 97dec6966a..acb3f0653f 100644 --- a/packages/ra-ui-materialui/src/field/ReferenceField.stories.tsx +++ b/packages/ra-ui-materialui/src/field/ReferenceField.stories.tsx @@ -837,3 +837,60 @@ const AccessControlUI = ({ ); }; + +export const Nested = () => ( + + { + if (resource === 'posts') { + await new Promise(resolve => + setTimeout(resolve, 1000) + ); + return { data: [{ id: 2, author_id: 3 }] }; + } + if (resource === 'authors') { + await new Promise(resolve => + setTimeout(resolve, 1000) + ); + return { data: [{ id: 3, name: 'John Doe' }] }; + } + throw new Error(`Unknown resource ${resource}`); + }, + } as any + } + > + + + + + + + + + + + +);