Skip to content

Commit 0a07b0b

Browse files
committed
review
1 parent 18462e3 commit 0a07b0b

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

docs_headless/astro.config.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ export default defineConfig({
176176
label: 'Inputs',
177177
items: [
178178
'inputs',
179+
'referenceinputbase',
180+
'referencearrayinputbase',
179181
'referencemanyinputbase',
180182
'referencemanytomanyinputbase',
181183
'referenceoneinputbase',

docs_headless/src/content/docs/ReferenceArrayInputBase.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ dataProvider.getList('tags', {
9797
});
9898
```
9999

100-
`<ReferenceArrayInputBase>` handles the data fetching and provides the choices through a [`ChoicesContext`](./usechoicescontext). It's up to the child components to render the selection interface.
100+
`<ReferenceArrayInputBase>` handles the data fetching and provides the choices through a [`ChoicesContext`](./useChoicesContext.md). It's up to the child components to render the selection interface.
101101

102102
You can tweak how `<ReferenceArrayInputBase>` fetches the possible values using the `page`, `perPage`, `sort`, and `filter` props.
103103

@@ -108,6 +108,7 @@ You can tweak how `<ReferenceArrayInputBase>` fetches the possible values using
108108
| `source` | Required | `string` | - | Name of the entity property to use for the input value |
109109
| `reference` | Required | `string` | '' | Name of the reference resource, e.g. 'tags'. |
110110
| `children` | Required | `ReactNode` | - | The actual selection component |
111+
| `render` | Optional | `(context) => ReactNode` | - | Function that takes the choices context and renders the selection interface |
111112
| `enableGetChoices` | Optional | `({q: string}) => boolean` | `() => true` | Function taking the `filterValues` and returning a boolean to enable the `getList` call. |
112113
| `filter` | Optional | `Object` | `{}` | Permanent filters to use for getting the suggestion list |
113114
| `offline` | Optional | `ReactNode` | - | What to render when there is no network connectivity when loading the record |
@@ -170,6 +171,44 @@ export const MyReferenceArrayInput = () => (
170171
);
171172
```
172173

174+
## `render`
175+
176+
Alternatively, you can pass a `render` function prop instead of children. This function will receive the `ChoicesContext` as argument.
177+
178+
```jsx
179+
export const MyReferenceArrayInput = () => (
180+
<ReferenceArrayInputBase
181+
source="tag_ids"
182+
reference="tags"
183+
render={({ choices, isLoading, error }) => {
184+
if (isLoading) {
185+
return <div>Loading...</div>;
186+
}
187+
188+
if (error) {
189+
return (
190+
<div className="error">
191+
{error.message}
192+
</div>
193+
);
194+
}
195+
196+
return (
197+
<select multiple>
198+
{choices.map(choice => (
199+
<option key={choice.id} value={choice.id}>
200+
{choice.name}
201+
</option>
202+
))}
203+
</select>
204+
);
205+
}}
206+
/>
207+
);
208+
```
209+
210+
The `render` function prop will take priority on `children` props if both are set.
211+
173212
## `enableGetChoices`
174213

175214
You can make the `getList()` call lazy by using the `enableGetChoices` prop. This prop should be a function that receives the `filterValues` as parameter and return a boolean. This can be useful when using a search input on a resource with a lot of data. The following example only starts fetching the options when the query has at least 2 characters:

docs_headless/src/content/docs/ReferenceInputBase.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ dataProvider.getList('companies', {
8787
});
8888
```
8989

90-
`<ReferenceInputBase>` handles the data fetching and provides the choices through a [`ChoicesContext`](./usechoicescontext). It's up to the child components to render the selection interface.
90+
`<ReferenceInputBase>` handles the data fetching and provides the choices through a [`ChoicesContext`](./useChoicesContext.md). It's up to the child components to render the selection interface.
9191

9292
You can tweak how `<ReferenceInputBase>` fetches the possible values using the `page`, `perPage`, `sort`, and `filter` props.
9393

@@ -115,7 +115,7 @@ You can access the choices context using the `useChoicesContext` hook.
115115
import { ReferenceInputBase, useChoicesContext, useInput } from 'ra-core';
116116

117117
export const CustomSelector = () => {
118-
const { allChoices, isLoading, error, source } = useChoicesContext();
118+
const { allChoices, isPending, error, source } = useChoicesContext();
119119
const { field, id } = useInput({ source });
120120

121121
if (error) {

docs_headless/src/content/docs/useChoicesContext.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "useChoicesContext"
44

55
The [`<ReferenceInputBase>`](./ReferenceInputBase.md) and [`<ReferenceArrayInputBase>`](./ReferenceArrayInputBase.md) components create a `ChoicesContext` to store the choices, as well as filters, pagination, sort state, and callbacks to update them.
66

7-
The `ChoicesContext` is very similar to the [`ListContext`](./uselistcontext) with the exception that it does not return a `data` property but 3 choices related properties:
7+
The `ChoicesContext` is very similar to the [`ListContext`](./useListContext.md) with the exception that it does not return a `data` property but 3 choices related properties:
88

99
- `availableChoices`: The choices that are not selected but match the parameters (sorting, pagination and filters)
1010
- `selectedChoices`: The selected choices.

0 commit comments

Comments
 (0)