Skip to content

Commit 10a52cf

Browse files
committed
Misc doc fixes
1 parent ef70578 commit 10a52cf

File tree

2 files changed

+37
-76
lines changed

2 files changed

+37
-76
lines changed

docs/ReferenceArrayFieldBase.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,22 @@ A typical `post` record therefore looks like this:
4141
}
4242
```
4343

44-
In that case, use `<ReferenceArrayFieldBase>` to display the post tag names as Chips as follows:
44+
In that case, use `<ReferenceArrayFieldBase>` to display the post tag names as a list of chips, as follows:
4545

4646
```jsx
4747
import { ListBase, ListIterator, ReferenceArrayFieldBase } from 'react-admin';
4848

49-
const MyTagsView = (props: { children: React.ReactNode }) => {
49+
export const PostList = () => (
50+
<ListBase>
51+
<ListIterator>
52+
<ReferenceArrayFieldBase reference="tags" source="tag_ids">
53+
<TagList />
54+
</ReferenceArrayFieldBase>
55+
</ListIterator>
56+
</ListBase>
57+
);
58+
59+
const TagList = (props: { children: React.ReactNode }) => {
5060
const context = useListContext();
5161

5262
if (context.isPending) {
@@ -64,16 +74,6 @@ const MyTagsView = (props: { children: React.ReactNode }) => {
6474
</p>
6575
);
6676
};
67-
68-
export const PostList = () => (
69-
<ListBase>
70-
<ListIterator>
71-
<ReferenceArrayFieldBase reference="tags" source="tag_ids">
72-
<MyTagsView />
73-
</ReferenceArrayFieldBase>
74-
</ListIterator>
75-
</ListBase>
76-
);
7777
```
7878
7979
`<ReferenceArrayFieldBase>` expects a `reference` attribute, which specifies the resource to fetch for the related records. It also expects a `source` attribute, which defines the field containing the list of ids to look for in the referenced resource.
@@ -106,11 +106,10 @@ You can access the list context using the `useListContext` hook.
106106
```jsx
107107

108108
<ReferenceArrayFieldBase label="Tags" reference="tags" source="tag_ids">
109-
<MyTagList />
109+
<TagList />
110110
</ReferenceArrayFieldBase>
111111

112-
// With MyTagList like:
113-
const MyTagList = (props: { children: React.ReactNode }) => {
112+
const TagList = (props: { children: React.ReactNode }) => {
114113
const { isPending, error, data } = useListContext();
115114

116115
if (isPending) {

docs/ReferenceFieldBase.md

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,25 @@ For instance, let's consider a model where a `post` has one author from the `use
2424
└──────────────┘
2525
```
2626

27-
In that case, use `<ReferenceFieldBase>` to display the post author's as follows:
27+
In that case, use `<ReferenceFieldBase>` to display the post's author as follows:
2828

2929
```jsx
30-
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
31-
32-
export const UserList = () => (
33-
<ReferenceFieldBase source="user_id" reference="users" >
34-
<CustomUIRenderer />
35-
</ReferenceFieldBase>
30+
import { Show, SimpleShowLayout, ReferenceField, TextField, RecordRepresentation } from 'react-admin';
31+
32+
export const PostShow = () => (
33+
<Show>
34+
<SimpleShowLayout>
35+
<TextField source="title" />
36+
<ReferenceFieldBase source="user_id" reference="users" >
37+
<UserView />
38+
</ReferenceFieldBase>
39+
</SimpleShowLayout>
40+
</Show>
3641
);
37-
```
38-
39-
`<ReferenceFieldBase>` fetches the data, puts it in a [`RecordContext`](./useRecordContext.md), and its up to its children to handle the rendering by accessing the ReferencingContext using the useReferenceFieldContext hook.
40-
41-
This component fetches a referenced record (`users` in this example) using the `dataProvider.getMany()` method, and passes it to the `ReferenceFieldContext`.
42-
43-
```tsx
44-
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
4542

4643
export const UserView = () => {
4744
const context = useReferenceFieldContext();
4845

49-
const value = useFieldValue({ source });
5046
if (context.isPending) {
5147
return <p>Loading...</p>;
5248
}
@@ -55,16 +51,12 @@ export const UserView = () => {
5551
return <p className="error">{context.error.toString()}</p>;
5652
}
5753

58-
return <p>{value}</p>;
54+
return <RecordRepresentation />;
5955
};
60-
61-
export const MyReferenceField = () => (
62-
<ReferenceFieldBase source="user_id" reference="users">
63-
<UserView />
64-
</ReferenceFieldBase>
65-
);
6656
```
6757

58+
`<ReferenceFieldBase>` fetches the data, puts it in a [`RecordContext`](./useRecordContext.md), and its up to its children to handle the rendering by accessing the `ReferencingContext` using the `useReferenceFieldContext` hook.
59+
6860
It uses `dataProvider.getMany()` instead of `dataProvider.getOne()` [for performance reasons](#performance). When using several `<ReferenceFieldBase>` in the same page (e.g. in a `<DataTable>`), this allows to call the `dataProvider` once instead of once per row.
6961

7062
## Props
@@ -74,7 +66,7 @@ It uses `dataProvider.getMany()` instead of `dataProvider.getOne()` [for perform
7466
| `source` | Required | `string` | - | Name of the property to display |
7567
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'posts' |
7668
| `children` | Optional | `ReactNode` | - | React component to render the referenced record. |
77-
| `render` | Optional | `(context) => ReactNode` | - | Function that takes the referenceFieldContext and render the referenced record. |
69+
| `render` | Optional | `(context) => ReactNode` | - | Function that takes the referenceFieldContext and renders the referenced record. |
7870
| `empty` | Optional | `ReactNode` | - | What to render when the field has no value or when the reference is missing |
7971
| `queryOptions` | Optional | [`UseQuery Options`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
8072
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |
@@ -88,18 +80,17 @@ You can access the list context using the `useReferenceFieldContext` hook.
8880
import { ReferenceFieldBase } from 'react-admin';
8981

9082
export const UserView = () => {
91-
const { isPending, error } = useReferenceFieldContext();
83+
const { error, isPending, referenceRecord } = useReferenceFieldContext();
9284

93-
const value = useFieldValue({ source });
94-
if (context.isPending) {
85+
if (isPending) {
9586
return <p>Loading...</p>;
9687
}
9788

98-
if (context.error) {
99-
return <p className="error">{context.error.toString()}</p>;
89+
if (error) {
90+
return <p className="error">{error.toString()}</p>;
10091
}
10192

102-
return <p>{value}</p>;
93+
return <>{referenceRecord.name}</>;
10394
};
10495

10596
export const MyReferenceField = () => (
@@ -164,37 +155,6 @@ You can pass either a React element or a string to the `empty` prop:
164155
</ReferenceFieldBase>
165156
```
166157

167-
## `link`
168-
169-
To change the link from the `<Edit>` page to the `<Show>` page, set the `link` prop to "show".
170-
171-
```jsx
172-
<ReferenceFieldBase source="user_id" reference="users" link="show" >
173-
...
174-
</ReferenceFieldBase>
175-
```
176-
177-
You can also prevent `<ReferenceFieldBase>` from adding a link to children by setting `link` to `false`.
178-
179-
```jsx
180-
// No link
181-
<ReferenceFieldBase source="user_id" reference="users" link={false} >
182-
...
183-
</ReferenceFieldBase>
184-
```
185-
186-
You can also use a custom `link` function to get a custom path for the children. This function must accept `record` and `reference` as arguments.
187-
188-
```jsx
189-
// Custom path
190-
<ReferenceFieldBase
191-
source="user_id"
192-
reference="users"
193-
link={(record, reference) => `/my/path/to/${reference}/${record.id}`}
194-
>
195-
...
196-
</ReferenceFieldBase>
197-
```
198158
## `queryOptions`
199159

200160
Use the `queryOptions` prop to pass options to [the `dataProvider.getMany()` query](./useGetOne.md#aggregating-getone-calls) that fetches the referenced record.
@@ -225,6 +185,7 @@ For instance, if the `posts` resource has a `user_id` field, set the `reference`
225185
...
226186
</ReferenceFieldBase>
227187
```
188+
228189
## `sortBy`
229190

230191
By default, when used in a `<Datagrid>`, and when the user clicks on the column header of a `<ReferenceFieldBase>`, react-admin sorts the list by the field `source`. To specify another field name to sort by, set the `sortBy` prop.
@@ -234,6 +195,7 @@ By default, when used in a `<Datagrid>`, and when the user clicks on the column
234195
...
235196
</ReferenceFieldBase>
236197
```
198+
237199
## Performance
238200

239201
<iframe src="https://www.youtube-nocookie.com/embed/egBhWqF3sWc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen style="aspect-ratio: 16 / 9;width:100%;margin-bottom:1em;"></iframe>

0 commit comments

Comments
 (0)