@@ -77,19 +77,18 @@ The `source`, `field`, `children`, and `render` props are mutually exclusive.
7777
7878## Props
7979
80- | Prop | Required | Type | Default | Description |
81- | ----------- | -------- | ------------------ | ------- | -------------------------------------------------------------------------------- |
82- | ` children ` | Optional | ReactNode | '' | Elements rendering the actual field. |
83- | ` className ` | Optional | string | '' | CSS class name to apply to the field. |
84- | ` defaultValue ` | Optional | any | undefined | Default value to display when the field is empty. |
85- | ` emptyText ` | Optional | string | '-' | Text to display when the field is empty. |
86- | ` field ` | Optional | ReactElement | ` TextField ` | Field component used to render the field. Ignored if ` children ` or ` render ` are set. |
87- | ` label ` | Optional | string | '' | Label to render. Can be a translation key. |
88- | ` render ` | Optional | record => JSX | | Function to render the field value. Ignored if ` children ` is set. |
89- | ` source ` | Optional | string | '' | Name of the record field to render. |
90- | ` sx ` | Optional | object | {} | Styles to apply to the field. |
91- | ` TypographyProps ` | Optional | object | {} | Props to pass to label wrapper |
92- | ` variant ` | Optional | `'default' || 'inline'` | 'default' | When ` inline ` , the label is displayed inline with the field value. |
80+ | Prop | Required | Type | Default | Description |
81+ | ----------- | -------- | ----------------------- | ------- | -------------------------------------------------------------------------------- |
82+ | ` children ` | Optional | ReactNode | '' | Elements rendering the actual field. |
83+ | ` className ` | Optional | string | '' | CSS class name to apply to the field. |
84+ | ` empty ` | Optional | ReactNode | '' | Text to display when the field is empty. |
85+ | ` field ` | Optional | ReactElement | ` TextField ` | Field component used to render the field. Ignored if ` children ` or ` render ` are set. |
86+ | ` label ` | Optional | string | '' | Label to render. Can be a translation key. |
87+ | ` render ` | Optional | record => JSX | | Function to render the field value. Ignored if ` children ` is set. |
88+ | ` source ` | Optional | string | '' | Name of the record field to render. |
89+ | ` sx ` | Optional | object | {} | Styles to apply to the field. |
90+ | ` TypographyProps ` | Optional | object | {} | Props to pass to label wrapper |
91+ | ` variant ` | Optional | `'default' || 'inline'` | 'default' | When ` inline ` , the label is displayed inline with the field value. |
9392
9493## ` children `
9594
@@ -127,38 +126,38 @@ import { RecordField, NumberField } from 'react-admin';
127126< / RecordField>
128127```
129128
130- ## ` defaultValue `
129+ ## ` empty `
131130
132- When the record contains no value for the ` source ` prop, ` RecordField ` renders an empty string. You can use the ` defaultValue ` prop to specify a default value to display instead.
131+ When the record contains no value for the ` source ` prop, ` RecordField ` renders an empty string. If you need to render a custom string in this case, you can use the ` empty ` prop :
133132
134133``` jsx
135- < RecordField source= " rating " defaultValue = { 0 } / >
134+ < RecordField source= " title " empty = " Missing title " / >
136135```
137136
138- If you're using the ` render ` prop, the ` defaultValue ` will be passed to the ` render ` function as the second argument :
137+ ` empty ` also accepts a translation key, so you can have a localized string when the field is empty :
139138
140139``` jsx
141- < RecordField
142- label= " Rating"
143- defaultValue= {0 }
144- render= {(record , defaultValue ) =>
145- record .rating ? ` ${ record .rating } stars` : defaultValue
146- }
147- / >
140+ < RecordField source= " title" empty= " resources.books.fields.title.missing" / >
148141```
149142
150- ** Tip ** : If you need the default value to be translated, use the ` emptyText ` prop instead .
143+ If you use the ` render ` prop, you can even use a React element as ` empty ` value .
151144
152- ## ` emptyText `
145+ ``` jsx
146+ < RecordField
147+ source= " title"
148+ empty= {< span style= {{ color: ' red' }}> Missing title< / span> }
149+ render= {record => record .title }
150+ / >
151+ ```
153152
154- When the record contains no value for the ` source ` prop, ` RecordField ` renders an empty string. If you need to render a locale-specific string in this case, you can use the ` emptyText ` prop to specify a translation key:
153+ Note that ` empty ` is ignored when you pass a custom field component as child. In this case, it's the child's responsibility to handle the empty value.
155154
156155``` jsx
157- < RecordField source= " title" emptyText= " resources.books.fields.title.missing" / >
156+ < RecordField label= " title" >
157+ < TextField source= " title" emptyText= " Missing title" / >
158+ < / RecordField>
158159```
159160
160- ** Tip** : If you don't need the default value to be translated, use the ` defaultValue ` prop instead.
161-
162161## ` field `
163162
164163By default, ` <RecordField> ` uses the [ ` <TextField> ` ] ( ./TextField.md ) component to render the field value.
0 commit comments