Skip to content

Commit 8c08dec

Browse files
committed
code review
1 parent 60eaf37 commit 8c08dec

File tree

11 files changed

+133
-133
lines changed

11 files changed

+133
-133
lines changed

docs/CreateBase.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const BookCreate = () => (
4646
You can customize the `<CreateBase>` component using the following props, documented in the `<Create>` component:
4747

4848
* `children`: the components that renders the form
49-
* `render`: alternative to children, a function that takes the CreateController context and renders the form
49+
* `render`: alternative to children, a function that takes the `CreateController` context and renders the form
5050
* [`disableAuthentication`](./Create.md#disableauthentication): disable the authentication check
5151
* [`mutationMode`](./Create.md#mutationmode): Switch to optimistic or undoable mutations (pessimistic by default)
5252
* [`mutationOptions`](./Create.md#mutationoptions): options for the `dataProvider.create()` call

docs/EditBase.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const BookEdit = () => (
4747
You can customize the `<EditBase>` component using the following props, documented in the `<Edit>` component:
4848

4949
* `children`: the components that renders the form
50-
* `render`: alternative to children, a function that takes the EditController context and renders the form
50+
* `render`: alternative to children, a function that takes the `EditController` context and renders the form
5151
* [`disableAuthentication`](./Edit.md#disableauthentication): disable the authentication check
5252
* [`id`](./Edit.md#id): the id of the record to edit
5353
* [`mutationMode`](./Edit.md#mutationmode): switch to optimistic or pessimistic mutations (undoable by default)

docs/ListBase.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ The `<ListBase>` component accepts the same props as [`useListController`](./use
6666
* [`resource`](./List.md#resource)
6767
* [`sort`](./List.md#sort)
6868

69-
These are a subset of the props accepted by `<List>` - only the props that change data fetching, and not the props related to the user interface.
70-
7169
In addition, `<ListBase>` renders its children components inside a `ListContext`. Check [the `<List children>` documentation](./List.md#children) for usage examples.
72-
Alternatively you can pass a render function to the props, in place of children. This function will receive the listContext as argument.
70+
Alternatively, you can pass a `render` function prop instead of `children`. This function will receive the `ListContext` as argument.
7371

7472
## Security
7573

docs/ReferenceArrayFieldBase.md

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: default
33
title: "The ReferenceArrayFieldBase Component"
4-
storybook_path: ra-ui-materialui-fields-referencearrayfieldbase--basic
4+
storybook_path: ra-core-fields-referencearrayfieldbase--basic
55
---
66

77
# `<ReferenceArrayFieldBase>`
@@ -46,7 +46,7 @@ In that case, use `<ReferenceArrayFieldBase>` to display the post tag names as C
4646
```jsx
4747
import { List, DataTable, ReferenceArrayFieldBase } from 'react-admin';
4848

49-
const MyPostView = (props: { children: React.ReactNode }) => {
49+
const MyTagsView = (props: { children: React.ReactNode }) => {
5050
const context = useListContext();
5151

5252
if (context.isPending) {
@@ -72,7 +72,7 @@ export const PostList = () => (
7272
<DataTable.Col source="title" />
7373
<DataTable.Col source="tag_ids" label="Tags">
7474
<ReferenceArrayFieldBase reference="tags" source="tag_ids">
75-
<MyPostView />
75+
<MyTagsView />
7676
</ReferenceArrayFieldBase>
7777
</DataTable.Col>
7878
<DataTable.Col>
@@ -87,49 +87,50 @@ export const PostList = () => (
8787
8888
`<ReferenceArrayFieldBase>` fetches the `tag` resources related to each `post` resource by matching `post.tag_ids` to `tag.id`.
8989
90-
You can change how the list of related records is rendered by passing a custom child reading the `ListContext` (e.g. a [`<DataTable>`](./DataTable.md)). See the [`children`](#children) section for details.
90+
You can change how the list of related records is rendered by passing a custom child reading the `ListContext` (e.g. a [`<DataTable>`](./DataTable.md)) or a render function prop. See the [`children`](#children) and the [`render`](#render) sections for details.
9191
9292
## Props
9393
9494
| Prop | Required | Type | Default | Description |
9595
| -------------- | -------- | --------------------------------------------------------------------------------- | -------------------------------- | ------------------------------------------------------------------------------------------------------ |
9696
| `source` | Required | `string` | - | Name of the property to display |
9797
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'tags' |
98-
| `children` | Required if no render | `Element` | | One or several elements that render a list of records based on a `ListContext` |
99-
| `render` | Required if no children | `(listContext) => Element` | | A function that takes a list context and render a list of records |
98+
| `children` | Optional\* | `Element` | | One or several elements that render a list of records based on a `ListContext` |
99+
| `render` | Optional\* | `(ListContext) => Element` | | A function that takes a list context and renders a list of records |
100100
| `filter` | Optional | `Object` | - | Filters to use when fetching the related records (the filtering is done client-side) |
101101
| `pagination` | Optional | `Element` | - | Pagination element to display pagination controls. empty by default (no pagination) |
102102
| `perPage` | Optional | `number` | 1000 | Maximum number of results to display |
103103
| `queryOptions` | Optional | [`UseQuery Options`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` options for the `getMany` query |
104104
| `sort` | Optional | `{ field, order }` | `{ field: 'id', order: 'DESC' }` | Sort order to use when displaying the related records (the sort is done client-side) |
105105
| `sortBy` | Optional | `string | Function` | `source` | When used in a `List`, name of the field to use for sorting when the user clicks on the column header. |
106106
107+
\* Either one of children or render is required.
108+
107109
## `children`
108110
109111
You can pass any component of your own as child, to render the list of related records as you wish.
110112
You can access the list context using the `useListContext` hook.
111113
112-
113114
```jsx
114115

115116
<ReferenceArrayFieldBase label="Tags" reference="tags" source="tag_ids">
116-
<MyPostView />
117+
<MyTagList />
117118
</ReferenceArrayFieldBase>
118119

119-
// With MyPostView like:
120-
const MyPostView = (props: { children: React.ReactNode }) => {
121-
const context = useListContext();
120+
// With MyTagList like:
121+
const MyTagList = (props: { children: React.ReactNode }) => {
122+
const { isPending, error, data } = useListContext();
122123

123-
if (context.isPending) {
124+
if (isPending) {
124125
return <p>Loading...</p>;
125126
}
126127

127-
if (context.error) {
128-
return <p className="error">{context.error.toString()}</p>;
128+
if (error) {
129+
return <p className="error">{error.toString()}</p>;
129130
}
130131
return (
131132
<p>
132-
{listContext.data?.map((tag, index) => (
133+
{data?.map((tag, index) => (
133134
<li key={index}>{tag.name}</li>
134135
))}
135136
</p>
@@ -139,27 +140,24 @@ const MyPostView = (props: { children: React.ReactNode }) => {
139140
140141
## `render`
141142
142-
Alternatively to children you can pass a render prop to `<ReferenceArrayFieldBase>`. The render prop will receive the list context as its argument, allowing to inline the render logic for both the list and the pagination.
143-
When receiving a render prop the `<ReferenceArrayFieldBase>` component will ignore the children and the pagination property.
144-
143+
Alternatively to children you can pass a `render` function prop to `<ReferenceArrayFieldBase>`. The `render` prop will receive the `ListContext` as its argument, allowing to inline the `render` logic for both the list and the pagination.
144+
When receiving a `render` prop the `<ReferenceArrayFieldBase>` component will ignore the children property.
145145
146146
```jsx
147-
<ReferenceArrayFieldBase
147+
<ReferenceArrayFieldBase
148148
label="Tags"
149149
reference="tags"
150150
source="tag_ids"
151-
render={(context) => {
152-
153-
if (context.isPending) {
151+
render={({ isPending, error, data }) => {
152+
if (isPending) {
154153
return <p>Loading...</p>;
155154
}
156-
157-
if (context.error) {
158-
return <p className="error">{context.error.toString()}</p>;
155+
if (error) {
156+
return <p className="error">{error.toString()}</p>;
159157
}
160158
return (
161159
<p>
162-
{listContext.data?.map((tag, index) => (
160+
{data.map((tag, index) => (
163161
<li key={index}>{tag.name}</li>
164162
))}
165163
</p>
@@ -176,7 +174,7 @@ For instance, to render only tags that are 'published', you can use the followin
176174
177175
{% raw %}
178176
```jsx
179-
<ReferenceArrayFieldBase
177+
<ReferenceArrayFieldBase
180178
label="Tags"
181179
source="tag_ids"
182180
reference="tags"
@@ -229,7 +227,6 @@ For instance, to pass [a custom `meta`](./Actions.md#meta-parameter):
229227
```
230228
{% endraw %}
231229
232-
233230
## `reference`
234231
235232
The resource to fetch for the relateds record.
@@ -250,7 +247,7 @@ For instance, to sort tags by title in ascending order, you can use the followin
250247
251248
{% raw %}
252249
```jsx
253-
<ReferenceArrayFieldBase
250+
<ReferenceArrayFieldBase
254251
label="Tags"
255252
source="tag_ids"
256253
reference="tags"

docs/ReferenceFieldBase.md

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ storybook_path: ra-core-controller-field-referencefieldbase--basic
77
# `<ReferenceFieldBase>`
88

99
`<ReferenceFieldBase>` is useful for displaying many-to-one and one-to-one relationships, e.g. the details of a user when rendering a post authored by that user.
10-
`<ReferenceFieldBase>` is a headless component, handling only the logic. This Allows to use any UI library for the render. For a version incorporating UI see [`<ReferenceField>`](/ReferenceField.html)
10+
`<ReferenceFieldBase>` is a headless component, handling only the logic. This allows to use any UI library for the render. For a version based on MUI see [`<ReferenceField>`](/ReferenceField.html)
1111

1212
## Usage
1313

@@ -29,7 +29,7 @@ In that case, use `<ReferenceFieldBase>` to display the post author's as follows
2929
```jsx
3030
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
3131

32-
export const PostShow = () => (
32+
export const UserList = () => (
3333
<ReferenceFieldBase source="user_id" reference="users" >
3434
<CustomUIRenderer />
3535
</ReferenceFieldBase>
@@ -38,12 +38,12 @@ export const PostShow = () => (
3838

3939
`<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.
4040

41-
This component fetches a referenced record (`users` in this example) using the `dataProvider.getMany()` method, and passes it to the ReferenceFieldContext.
41+
This component fetches a referenced record (`users` in this example) using the `dataProvider.getMany()` method, and passes it to the `ReferenceFieldContext`.
4242

4343
```tsx
4444
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
4545

46-
export const MyReferenceFieldView = () => {
46+
export const UserView = () => {
4747
const context = useReferenceFieldContext();
4848

4949
const value = useFieldValue({ source });
@@ -60,22 +60,21 @@ export const MyReferenceFieldView = () => {
6060

6161
export const MyReferenceField = () => (
6262
<ReferenceFieldBase source="user_id" reference="users">
63-
<MyReferenceFieldView />
63+
<UserView />
6464
</ReferenceFieldBase>
6565
);
6666
```
6767

6868
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.
6969

70-
7170
## Props
7271

7372
| Prop | Required | Type | Default | Description |
7473
| ----------- | -------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
7574
| `source` | Required | `string` | - | Name of the property to display |
7675
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'posts' |
77-
| `children` | Optional | `ReactNode` | - | React component to render the referenced record, the component need to use useReferenceFieldContext to access the context. |
78-
| `render` | Optional | `(context) => ReactNode` | - | Function that takes the referenceFieldContext and render the referenced record. Will take priority on children props if both are set. |
76+
| `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. |
7978
| `empty` | Optional | `ReactNode` | - | What to render when the field has no value or when the reference is missing |
8079
| `queryOptions` | Optional | [`UseQuery Options`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
8180
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |
@@ -88,8 +87,8 @@ You can access the list context using the `useReferenceFieldContext` hook.
8887
```tsx
8988
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
9089

91-
export const MyReferenceFieldView = () => {
92-
const context = useReferenceFieldContext();
90+
export const UserView = () => {
91+
const { isPending, error } = useReferenceFieldContext();
9392

9493
const value = useFieldValue({ source });
9594
if (context.isPending) {
@@ -105,34 +104,39 @@ export const MyReferenceFieldView = () => {
105104

106105
export const MyReferenceField = () => (
107106
<ReferenceFieldBase source="user_id" reference="users">
108-
<MyReferenceFieldView />
107+
<UserView />
109108
</ReferenceFieldBase>
110109
);
111110
```
112111

113112
## `render`
114113

115-
Alternatively you can pass a render prop instead of children to be able to inline the rendering. The render function will then receive the reference field context directly.
114+
Alternatively, you can pass a `render` function prop instead of children. This function will receive the `ReferenceFieldContext` as argument.
116115

117116
```jsx
118117
export const MyReferenceField = () => (
119-
<ReferenceFieldBase source="user_id" reference="users" render={({ error, isPending }) => {
120-
if (isPending) {
121-
return <p>Loading...</p>;
122-
}
123-
124-
if (error) {
125-
return (
126-
<p className="error">
127-
{error.message}
128-
</p>
129-
);
130-
}
131-
return <p>{value}</p>;
132-
}} />
118+
<ReferenceFieldBase
119+
source="user_id"
120+
reference="users"
121+
render={({ error, isPending }) => {
122+
if (isPending) {
123+
return <p>Loading...</p>;
124+
}
125+
126+
if (error) {
127+
return (
128+
<p className="error">
129+
{error.message}
130+
</p>
131+
);
132+
}
133+
return <p>{value}</p>;
134+
}}
135+
/>
133136
);
134137
```
135138

139+
The `render` function prop will take priority on `children` props if both are set.
136140

137141
## `empty`
138142

@@ -154,7 +158,6 @@ You can pass either a React element or a string to the `empty` prop:
154158
<ReferenceField source="user_id" reference="users" empty="Missing user" />
155159
```
156160

157-
158161
## `link`
159162

160163
To change the link from the `<Edit>` page to the `<Show>` page, set the `link` prop to "show".
@@ -180,7 +183,6 @@ You can also use a custom `link` function to get a custom path for the children.
180183
link={(record, reference) => `/my/path/to/${reference}/${record.id}`}
181184
/>
182185
```
183-
184186
## `queryOptions`
185187

186188
Use the `queryOptions` prop to pass options to [the `dataProvider.getMany()` query](./useGetOne.md#aggregating-getone-calls) that fetches the referenced record.
@@ -208,15 +210,13 @@ For instance, if the `posts` resource has a `user_id` field, set the `reference`
208210
```jsx
209211
<ReferenceFieldBase source="user_id" reference="users" />
210212
```
211-
212213
## `sortBy`
213214

214215
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.
215216

216217
```jsx
217218
<ReferenceFieldBase source="user_id" reference="users" sortBy="user.name" />
218219
```
219-
220220
## Performance
221221

222222
<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)