Skip to content

Commit 96993c6

Browse files
committed
Add support for record prop
1 parent 43e20fb commit 96993c6

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

docs/RecordField.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ The `source`, `field`, `children`, and `render` props are mutually exclusive.
8484
| `empty` | Optional | ReactNode | '' | Text to display when the field is empty. |
8585
| `field` | Optional | ReactElement | `TextField` | Field component used to render the field. Ignored if `children` or `render` are set. |
8686
| `label` | Optional | string | '' | Label to render. Can be a translation key. |
87+
| `record` | Optional | object | {} | Record to use. If not set, the record is taken from the context. |
8788
| `render` | Optional | record => JSX | | Function to render the field value. Ignored if `children` is set. |
8889
| `source` | Optional | string | '' | Name of the record field to render. |
8990
| `sx` | Optional | object | {} | Styles to apply to the field. |
@@ -228,6 +229,14 @@ Finally, you can pass `false` to the `label` prop to hide the label:
228229

229230
Note that using `label={false}` is equivalent to rendering a `<TextField>` directly.
230231

232+
## `record`
233+
234+
By default, `<RecordField>` uses the record from the current [`RecordContext`](./useRecordContext.md). You can override this behavior by passing a `record` prop:
235+
236+
```jsx
237+
<RecordField record={record} source="title" />
238+
```
239+
231240
## `render`
232241

233242
The `render` prop is used to pass a function that receives the current record and returns a React element. This is useful when you need to aggregate multiple fields, or when you need to use a component that doesn't accept the `source` prop.

packages/ra-ui-materialui/src/field/RecordField.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
useTranslate,
1717
type ExtractRecordPaths,
1818
type HintedString,
19-
type RaRecord,
2019
} from 'ra-core';
2120
import clsx from 'clsx';
2221

@@ -47,7 +46,7 @@ export const RecordField = <
4746
...rest
4847
} = props;
4948
const resource = useResourceContext();
50-
const record = useRecordContext<RecordType>();
49+
const record = useRecordContext<RecordType>(props);
5150
const translate = useTranslate();
5251
if (!source && !label) return null;
5352
return (
@@ -117,7 +116,7 @@ export interface RecordFieldProps<
117116
label?: ReactNode;
118117
render?: (record: RecordType) => React.ReactNode;
119118
source?: NoInfer<HintedString<ExtractRecordPaths<RecordType>>>;
120-
record?: RaRecord;
119+
record?: RecordType;
121120
sx?: SxProps;
122121
TypographyProps?: TypographyProps;
123122
variant?: 'default' | 'inline';

0 commit comments

Comments
 (0)