You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Once it receives the deduplicated reference resources, this component delegates rendering to a subcomponent, to which it passes the possible choices as the `choices` attribute.
1409
+
Once it receives the deduplicated reference resources, this component delegates rendering to a subcomponent, by providing the possible choices through the `ReferenceArrayInputContext`. This context value can be accessed with the [`useReferenceArrayInputContext`](#usereferencearrayinputcontext) hook.
1410
1410
1411
1411
This means you can use `<ReferenceArrayInput>` with [`<SelectArrayInput>`](#selectarrayinput), or with the component of your choice, provided it supports the `choices` attribute.
1412
1412
@@ -1477,8 +1477,30 @@ You can tweak how this component fetches the possible values using the `perPage`
1477
1477
```
1478
1478
{% endraw %}
1479
1479
1480
+
In addition to the `ReferenceArrayInputContext`, `<ReferenceArrayInput>` also sets up a `ListContext` providing access to the records from the reference resource in a similar fashion to that of the `<List>` component. This `ListContext` value is accessible with the [`useListContext`](/List.md#uselistcontext) hook.
1481
+
1480
1482
`<ReferenceArrayInput>` also accepts the [common input props](./Inputs.md#common-input-props).
1481
1483
1484
+
### `useReferenceArrayInputContext`
1485
+
1486
+
The [`<ReferenceArrayInput>`](#referencearrayinput) component take care of fetching the data, and put that data in a context called `ReferenceArrayInputContext` so that it’s available for its descendants. This context also stores filters, pagination, sort state, and provides callbacks to update them.
1487
+
1488
+
Any component decendent of `<ReferenceArryInput>` can grab information from the `ReferenceArrayInputContext` using the `useReferenceArrayInputContext` hook. Here is what it returns:
1489
+
1490
+
```js
1491
+
const {
1492
+
choices, // An array of records matching both the current input value and the filters
1493
+
error, // A potential error that may have occured while fetching the data
1494
+
warning, // A potential warning regarding missing references
1495
+
loaded, // boolean that is false until the data is available
1496
+
loading, // boolean that is true on mount, and false once the data was fetched
1497
+
setFilter, // a callback to update the filters, e.g. setFilters({ q: 'query' })
1498
+
setPagination, // a callback to change the pagination, e.g. setPagination({ page: 2, perPage: 50 })
1499
+
setSort, // a callback to change the sort, e.g. setSort({ field: 'name', order: 'DESC' })
1500
+
setSortForList, // a callback to set the sort with the same signature as the one from the ListContext. This is required to avoid breaking backward compatibility and will be removed in v4
1501
+
} =useReferenceArrayInputContext();
1502
+
```
1503
+
1482
1504
### `<ReferenceInput>`
1483
1505
1484
1506
Use `<ReferenceInput>` for foreign-key values, for instance, to edit the `post_id` of a `comment` resource. This component fetches the related record (using `dataProvider.getMany()`) as well as possible choices (using `dataProvider.getList()` in the reference resource), then delegates rendering to a subcomponent, to which it passes the possible choices as the `choices` attribute.
0 commit comments