Skip to content

Commit 1e16f15

Browse files
authored
Merge pull request #11042 from marmelab/fix-referencefieldbase
Fix ReferenceFieldBase considers zero-index reference as empty
2 parents 90f3a53 + d1851d3 commit 1e16f15

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

packages/ra-core/src/controller/field/ReferenceFieldBase.spec.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
Meta,
1313
Offline,
1414
WithRenderProp,
15+
ZeroIndex,
1516
} from './ReferenceFieldBase.stories';
1617
import { RecordContextProvider } from '../record';
1718
import { onlineManager } from '@tanstack/react-query';
@@ -184,4 +185,11 @@ describe('<ReferenceFieldBase />', () => {
184185
await screen.findByText('You are offline, the data may be outdated');
185186
await screen.findByText('Leo');
186187
});
188+
189+
it('should not render the empty component for zero-index ids', async () => {
190+
render(<ZeroIndex />);
191+
await waitFor(() => {
192+
expect(screen.queryByText('Leo')).not.toBeNull();
193+
});
194+
});
187195
});

packages/ra-core/src/controller/field/ReferenceFieldBase.stories.tsx

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,16 @@ export const QueryOptions = () => (
268268
const BookShowMeta = () => {
269269
return (
270270
<ShowBase>
271-
<>
272-
<TextField source="title" />
273-
<ReferenceFieldBase
274-
reference="authors"
275-
source="author"
276-
queryOptions={{ meta: { test: true } }}
277-
>
278-
<MyReferenceField>
279-
<TextField source="last_name" />
280-
</MyReferenceField>
281-
</ReferenceFieldBase>
282-
</>
271+
<TextField source="title" />
272+
<ReferenceFieldBase
273+
reference="authors"
274+
source="author"
275+
queryOptions={{ meta: { test: true } }}
276+
>
277+
<MyReferenceField>
278+
<TextField source="last_name" />
279+
</MyReferenceField>
280+
</ReferenceFieldBase>
283281
</ShowBase>
284282
);
285283
};
@@ -397,6 +395,39 @@ export const WithRenderProp = ({ dataProvider = dataProviderWithAuthors }) => (
397395
</TestMemoryRouter>
398396
);
399397

398+
export const ZeroIndex = ({
399+
dataProvider = fakeRestDataProvider(
400+
{
401+
books: [{ id: 1, title: 'War and Peace', author: 0 }],
402+
authors: [{ id: 0, first_name: 'Leo', last_name: 'Tolstoy' }],
403+
},
404+
process.env.NODE_ENV === 'development'
405+
),
406+
}: {
407+
dataProvider?: DataProvider;
408+
}) => (
409+
<TestMemoryRouter initialEntries={['/books/1/show']}>
410+
<CoreAdmin dataProvider={dataProvider}>
411+
<Resource
412+
name="books"
413+
show={() => (
414+
<ShowBase>
415+
<TextField source="title" />
416+
<ReferenceFieldBase
417+
reference="authors"
418+
source="author"
419+
empty={<>Should not appear</>}
420+
>
421+
<TextField source="first_name" />
422+
<TextField source="last_name" />
423+
</ReferenceFieldBase>
424+
</ShowBase>
425+
)}
426+
/>
427+
</CoreAdmin>
428+
</TestMemoryRouter>
429+
);
430+
400431
export const Offline = () => {
401432
return (
402433
<TestMemoryRouter initialEntries={['/books/1/show']}>

packages/ra-core/src/controller/field/ReferenceFieldBase.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export const ReferenceFieldBase = <
7777
!!controllerError && error !== false && error !== undefined;
7878
const shouldRenderEmpty =
7979
!isPaused &&
80-
(!id ||
80+
(id == null ||
8181
(!referenceRecord &&
8282
!controllerError &&
8383
!isPending &&

0 commit comments

Comments
 (0)