Skip to content

Commit 59bc05e

Browse files
committed
stop referring mui component in ReferenceFieldBase doc, move section about render under props
1 parent 58b7a55 commit 59bc05e

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

docs/ReferenceFieldBase.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ storybook_path: ra-core-controller-field-referencefieldbase--basic
1111

1212
## Usage
1313

14-
### With children
15-
1614
For instance, let's consider a model where a `post` has one author from the `users` resource, referenced by a `user_id` field.
1715

1816
```
@@ -32,16 +30,9 @@ In that case, use `<ReferenceFieldBase>` to display the post author's as follows
3230
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
3331

3432
export const PostShow = () => (
35-
<Show>
36-
<SimpleShowLayout>
37-
<TextField source="id" />
38-
<TextField source="title" />
39-
<DateField source="published_at" />
40-
<ReferenceFieldBase source="user_id" reference="users" >
41-
<CustomUIRenderer />
42-
</ReferenceFieldBase>
43-
</SimpleShowLayout>
44-
</Show>
33+
<ReferenceFieldBase source="user_id" reference="users" >
34+
<CustomUIRenderer />
35+
</ReferenceFieldBase>
4536
);
4637
```
4738

@@ -76,7 +67,50 @@ export const MyReferenceField = () => (
7667

7768
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.
7869

79-
### With render prop
70+
71+
## Props
72+
73+
| Prop | Required | Type | Default | Description |
74+
| ----------- | -------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
75+
| `source` | Required | `string` | - | Name of the property to display |
76+
| `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. |
79+
| `empty` | Optional | `ReactNode` | - | What to render when the field has no value or when the reference is missing |
80+
| `queryOptions` | Optional | [`UseQuery Options`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
81+
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |
82+
83+
## `children`
84+
85+
You can pass any component of your own as child, to render the related records as you wish.
86+
You can access the list context using the `useReferenceFieldContext` hook.
87+
88+
```tsx
89+
import { Show, SimpleShowLayout, ReferenceField, TextField, DateField } from 'react-admin';
90+
91+
export const MyReferenceFieldView = () => {
92+
const context = useReferenceFieldContext();
93+
94+
const value = useFieldValue({ source });
95+
if (context.isPending) {
96+
return <p>Loading...</p>;
97+
}
98+
99+
if (context.error) {
100+
return <p className="error">{context.error.toString()}</p>;
101+
}
102+
103+
return <p>{value}</p>;
104+
};
105+
106+
export const MyReferenceField = () => (
107+
<ReferenceFieldBase source="user_id" reference="users">
108+
<MyReferenceFieldView />
109+
</ReferenceFieldBase>
110+
);
111+
```
112+
113+
## `render`
80114

81115
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.
82116

@@ -99,18 +133,6 @@ export const MyReferenceField = () => (
99133
);
100134
```
101135

102-
## Props
103-
104-
| Prop | Required | Type | Default | Description |
105-
| ----------- | -------- | ------------------- | -------- | ------------------------------------------------------------------------------------------------------------------- |
106-
| `source` | Required | `string` | - | Name of the property to display |
107-
| `reference` | Required | `string` | - | The name of the resource for the referenced records, e.g. 'posts' |
108-
| `children` | Optional | `ReactNode` | - | React component to render the referenced record, the component need to use useReferenceFieldContext to access the context. |
109-
| `render` | Optional | `(context) => ReactNode` | - | Function that takes the referenceFieldContext and render the referenced record. Will take priority on children props if both are set. |
110-
| `empty` | Optional | `ReactNode` | - | What to render when the field has no value or when the reference is missing |
111-
| `queryOptions` | Optional | [`UseQuery Options`](https://tanstack.com/query/v5/docs/react/reference/useQuery) | `{}` | `react-query` client options |
112-
| `sortBy` | Optional | `string | Function` | `source` | Name of the field to use for sorting when used in a Datagrid |
113-
114136

115137
## `empty`
116138

packages/ra-core/src/controller/field/ReferenceManyFieldBase.stories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Resource } from '../../core/Resource';
55
import { ShowBase } from '../../controller/show/ShowBase';
66
import { TestMemoryRouter } from '../../routing';
77
import { ReferenceManyFieldBase } from './ReferenceManyFieldBase';
8-
import { useListContext, useListContextWithProps } from '../list';
8+
import { useListContext } from '../list';
99

1010
export default {
1111
title: 'ra-core/controller/field/ReferenceManyFieldBase',

packages/ra-core/src/controller/field/ReferenceOneFieldBase.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
ReferenceOneFieldBase,
77
ResourceContextProvider,
88
TestMemoryRouter,
9-
useRecordContext,
109
useReferenceFieldContext,
1110
} from '../..';
1211

0 commit comments

Comments
 (0)