File tree Expand file tree Collapse file tree 3 files changed +19
-3
lines changed
packages/ra-core/src/controller Expand file tree Collapse file tree 3 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { RaRecord } from '../../types';
66import { useReferenceFieldController } from './useReferenceFieldController' ;
77import { ResourceContextProvider } from '../../core' ;
88import { 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 > & {
Original file line number Diff line number Diff line change 1+ export * from './ReferenceOneFieldBase' ;
12export * from './ReferenceFieldBase' ;
23export * from './ReferenceFieldContext' ;
34export * from './ReferenceManyCountBase' ;
5+ export * from './ReferenceManyFieldBase' ;
46export * from './useReferenceArrayFieldController' ;
57export * from './useReferenceFieldController' ;
68export * from './useReferenceManyFieldController' ;
Original file line number Diff line number Diff line change @@ -16,12 +16,14 @@ import { useRecordContext } from './useRecordContext';
1616 */
1717export 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
2425export interface WithRecordProps < RecordType extends Record < string , any > = any > {
2526 render : ( record : RecordType ) => ReactNode ;
27+ empty ?: ReactNode ;
2628 label ?: string ;
2729}
You can’t perform that action at this time.
0 commit comments