Skip to content

Commit ff20e36

Browse files
committed
[Doc] Fix ReferenceOneField doc
1 parent c7e11af commit ff20e36

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

docs/ReferenceOneField.md

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,16 @@ const BookShow = () => (
5959
| -------------- | -------- | ------------------------------------------- | -------------------------------- | ----------------------------------------------------------------------------------- |
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' |
62-
| `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 |
62+
| `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 |
6464
| `empty` | Optional | `ReactNode` | - | The text or element to display when the referenced record is empty |
6565
| `filter` | Optional | `Object` | `{}` | Used to filter referenced records |
6666
| `link` | Optional | `string | Function` | `edit` | Target of the link wrapping the rendered child. Set to `false` to disable the link. |
6767
| `queryOptions` | Optional | [`UseQueryOptions`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
6868
| `sort` | Optional | `{ field: String, order: 'ASC' or 'DESC' }` | `{ field: 'id', order: 'ASC' }` | Used to order referenced records |
6969

70+
`*` You must provide either `children` or `render`.
71+
7072
`<ReferenceOneField>` also accepts the [common field props](./Fields.md#common-field-props).
7173

7274
## `children`
@@ -81,34 +83,6 @@ For instance, if you want to render both the genre and the ISBN for a book:
8183
</ReferenceOneField>
8284
```
8385

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-
11286
## `empty`
11387

11488
Use `empty` to customize the text displayed when the related record is empty.
@@ -213,6 +187,38 @@ For instance, if you want to display the details of a given book, the `reference
213187
</ReferenceOneField>
214188
```
215189

190+
## `render`
191+
192+
Alternatively to `children`, you can pass a `render` prop to `<ReferenceOneField>`. It will receive the `ReferenceFieldContext` as its argument, and should return a React node.
193+
194+
This allows to inline the render logic for the related record.
195+
196+
```tsx
197+
<ReferenceOneField
198+
reference="book_details"
199+
target="book_id"
200+
render={({ isPending, error, referenceRecord }) => {
201+
if (isPending) {
202+
return <p>Loading...</p>;
203+
}
204+
if (error) {
205+
return <p className="error" >{error.toString()}</p>;
206+
}
207+
if (!referenceRecord) {
208+
return <p className="error">No details found</p>;
209+
}
210+
return (
211+
<dl>
212+
<dt>Genre</dt>
213+
<dd>{referenceRecord.genre}</dd>
214+
<dt>ISBN</dt>
215+
<dd>{referenceRecord.ISBN}</dd>
216+
</dl>
217+
);
218+
}}
219+
/>
220+
```
221+
216222
## `sort`
217223

218224
You can also use `<ReferenceOneField>` in a one-to-many relationship. In that case, the first record will be displayed. This is where the `sort` prop comes in handy. It allows you to select the appropriate record to display.

0 commit comments

Comments
 (0)