Skip to content

Commit 551f1a1

Browse files
committed
Fix ReferenceFieldBase considers zero-index reference as empty
1 parent 90f3a53 commit 551f1a1

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-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: 59 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,55 @@ export const WithRenderProp = ({ dataProvider = dataProviderWithAuthors }) => (
397395
</TestMemoryRouter>
398396
);
399397

398+
export const ZeroIndex = ({
399+
dataProvider = fakeRestDataProvider(
400+
{
401+
books: [
402+
{
403+
id: 1,
404+
title: 'War and Peace',
405+
author: 0,
406+
summary:
407+
"War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.",
408+
year: 1869,
409+
},
410+
],
411+
authors: [
412+
{
413+
id: 0,
414+
first_name: 'Leo',
415+
last_name: 'Tolstoy',
416+
language: 'Russian',
417+
},
418+
],
419+
},
420+
process.env.NODE_ENV === 'development'
421+
),
422+
}: {
423+
dataProvider?: DataProvider;
424+
}) => (
425+
<TestMemoryRouter initialEntries={['/books/1/show']}>
426+
<CoreAdmin dataProvider={dataProvider}>
427+
<Resource
428+
name="books"
429+
show={() => (
430+
<ShowBase>
431+
<TextField source="title" />
432+
<ReferenceFieldBase
433+
reference="authors"
434+
source="author"
435+
empty={<>Should not appear</>}
436+
>
437+
<TextField source="first_name" />
438+
<TextField source="last_name" />
439+
</ReferenceFieldBase>
440+
</ShowBase>
441+
)}
442+
/>
443+
</CoreAdmin>
444+
</TestMemoryRouter>
445+
);
446+
400447
export const Offline = () => {
401448
return (
402449
<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)