Skip to content

Commit 5add7d5

Browse files
committed
add render prop to ReferenceOneField
1 parent 90b5ca6 commit 5add7d5

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

docs/ReferenceOneField.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const BookShow = () => (
6060
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'book_details' |
6161
| `target` | Required | string | - | Target field carrying the relationship on the referenced resource, e.g. 'book_id' |
6262
| `children` | Optional | `Element` | - | The Field element used to render the referenced record |
63+
| `render` | Optional | `(ReferenceFieldContext) => Element` | - | A function that takes the `ReferenceFieldContext` and returns a React element |
6364
| `empty` | Optional | `ReactNode` | - | The text or element to display when the referenced record is empty |
6465
| `filter` | Optional | `Object` | `{}` | Used to filter referenced records |
6566
| `link` | Optional | `string | Function` | `edit` | Target of the link wrapping the rendered child. Set to `false` to disable the link. |
@@ -80,6 +81,34 @@ For instance, if you want to render both the genre and the ISBN for a book:
8081
</ReferenceOneField>
8182
```
8283

84+
85+
## `render`
86+
87+
Alternatively to children you can pass a `render` function prop to `<ReferenceOneFieldBase>`. The `render` function prop will receive the `ReferenceFieldContext` as its argument, allowing to inline the render logic.
88+
When receiving a `render` function prop the `<ReferenceOneFieldBase>` component will ignore the children property.
89+
90+
```jsx
91+
<ReferenceOneField
92+
reference="book_details"
93+
target="book_id"
94+
render={({ isPending, error, referenceRecord }) => {
95+
if (isPending) {
96+
return <Typography>Loading...</Typography>;
97+
}
98+
99+
if (error) {
100+
return <Typography className="error" >{error.toString()}</Typography>;
101+
}
102+
return (
103+
<div>
104+
<Typography>{referenceRecord ? referenceRecord.genre : ''}</Typography>
105+
<Typography>{referenceRecord ? referenceRecord.ISBN : ''}</Typography>
106+
</div>
107+
);
108+
}}
109+
/>
110+
```
111+
83112
## `empty`
84113

85114
Use `empty` to customize the text displayed when the related record is empty.

packages/ra-ui-materialui/src/field/ReferenceOneField.spec.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
EmptyText,
1010
Empty,
1111
Themed,
12+
WithRenderProp,
1213
} from './ReferenceOneField.stories';
1314

1415
describe('ReferenceOneField', () => {
@@ -54,6 +55,11 @@ describe('ReferenceOneField', () => {
5455
});
5556
});
5657

58+
it('should allow to render the referenceRecord using a render prop', async () => {
59+
render(<WithRenderProp />);
60+
await screen.findByText('9780393966473');
61+
});
62+
5763
describe('emptyText', () => {
5864
it('should render the emptyText prop when the record is not found', async () => {
5965
render(<EmptyText />);

packages/ra-ui-materialui/src/field/ReferenceOneField.stories.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,25 @@ export const Themed = () => (
568568
</ReferenceOneField>
569569
</Wrapper>
570570
);
571+
572+
export const WithRenderProp = () => (
573+
<Wrapper>
574+
<ReferenceOneField
575+
reference="book_details"
576+
target="book_id"
577+
render={({ isPending, error, referenceRecord }) => {
578+
if (isPending) {
579+
return <p>Loading...</p>;
580+
}
581+
582+
if (error) {
583+
return <p style={{ color: 'red' }}>{error.toString()}</p>;
584+
}
585+
586+
return (
587+
<span>{referenceRecord ? referenceRecord.ISBN : ''}</span>
588+
);
589+
}}
590+
/>
591+
</Wrapper>
592+
);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SortPayload,
88
RaRecord,
99
ReferenceOneFieldBase,
10+
UseReferenceResult,
1011
} from 'ra-core';
1112

1213
import { FieldProps } from './types';
@@ -37,6 +38,7 @@ export const ReferenceOneField = <
3738

3839
const {
3940
children,
41+
render,
4042
reference,
4143
source = 'id',
4244
target,
@@ -88,6 +90,7 @@ export interface ReferenceOneFieldProps<
8890
ReferenceRecordType extends RaRecord = RaRecord,
8991
> extends Omit<FieldProps<RecordType>, 'source' | 'emptyText'> {
9092
children?: ReactNode;
93+
render?: (record: UseReferenceResult<ReferenceRecordType>) => ReactElement;
9194
reference: string;
9295
target: string;
9396
sort?: SortPayload;

0 commit comments

Comments
 (0)