Skip to content

Commit 85a130c

Browse files
committed
Add <ReferenceField empty> prop
1 parent 3fcc16d commit 85a130c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RaRecord } from '../../types';
66
import { useReferenceFieldController } from './useReferenceFieldController';
77
import { ResourceContextProvider } from '../../core';
88
import { RecordContextProvider } from '../record';
9+
import { useFieldValue } from '../../util';
910

1011
/**
1112
* Fetch reference record, and render its representation, or delegate rendering to child component.
@@ -43,11 +44,21 @@ export const ReferenceFieldBase = <
4344
>(
4445
props: ReferenceFieldBaseProps<ReferenceRecordType>
4546
) => {
46-
const { children } = props;
47-
47+
const { children, empty = null } = props;
48+
const id = useFieldValue(props);
4849
const controllerProps =
4950
useReferenceFieldController<ReferenceRecordType>(props);
5051

52+
if (
53+
// no foreign key value
54+
!id ||
55+
// no reference record
56+
(!controllerProps.error &&
57+
!controllerProps.isPending &&
58+
!controllerProps.referenceRecord)
59+
) {
60+
return empty;
61+
}
5162
return (
5263
<ResourceContextProvider value={props.reference}>
5364
<ReferenceFieldContextProvider value={controllerProps}>
@@ -64,6 +75,7 @@ export interface ReferenceFieldBaseProps<
6475
> {
6576
children?: ReactNode;
6677
className?: string;
78+
empty?: ReactNode;
6779
error?: ReactNode;
6880
queryOptions?: Partial<
6981
UseQueryOptions<ReferenceRecordType[], Error> & {

packages/ra-core/src/controller/field/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
export * from './ReferenceOneFieldBase';
12
export * from './ReferenceFieldBase';
23
export * from './ReferenceFieldContext';
34
export * from './ReferenceManyCountBase';
5+
export * from './ReferenceManyFieldBase';
46
export * from './useReferenceArrayFieldController';
57
export * from './useReferenceFieldController';
68
export * from './useReferenceManyFieldController';

packages/ra-core/src/controller/record/WithRecord.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import { useRecordContext } from './useRecordContext';
1616
*/
1717
export const WithRecord = <RecordType extends Record<string, any> = any>({
1818
render,
19+
empty = null,
1920
}: WithRecordProps<RecordType>) => {
2021
const record = useRecordContext<RecordType>();
21-
return record ? <>{render(record)}</> : null;
22+
return record ? <>{render(record)}</> : empty;
2223
};
2324

2425
export interface WithRecordProps<RecordType extends Record<string, any> = any> {
2526
render: (record: RecordType) => ReactNode;
27+
empty?: ReactNode;
2628
label?: string;
2729
}

0 commit comments

Comments
 (0)