Closed
Description
openedon Dec 12, 2023
What you were expecting:
ReferenceManyField has same filter debounce behavoir as normal list context.
<ReferenceManyField> creates a ListContext with the related records, so you can use any component relying on this context (<Datagrid>, <SimpleList>, etc.).
What happened instead:
No debounce effect when using ReferenceManyField with filters.
Steps to reproduce:
- Preferably, a sandbox: https://stackblitz.com/edit/github-8jmtgu?file=src%2Fposts%2FPostShow.tsx (not sure if that link works...)
To reproduce in the sandbox I basically added FilterLiveSearch to PostShow.tsx:
<ReferenceManyField
reference="comments"
target="post_id"
sort={{ field: 'created_at', order: 'DESC' }}
>
+ <FilterLiveSearch source="body" />
<Datagrid>
<DateField source="created_at" />
<TextField source="author.name" />
<TextField source="body" />
<EditButton />
</Datagrid>
</ReferenceManyField>
Related code:
To me it looks like the implementation of useReferenceManyFieldController
ignores the debounce parameter of setFilters
:
// Example generic code to set filters:
const { setFilters } = useListContext();
// ListControllerResult defines:
setFilters: (
filters: any,
displayedFilters: any,
debounce?: boolean
) => void;
// Inside <List> this correctly returns:
const setFilters = useCallback(
(filter, displayedFilters, debounce = true) =>
debounce
? debouncedSetFilters(filter, displayedFilters)
: changeParams({
type: SET_FILTER,
payload: {
filter: removeEmpty(filter),
displayedFilters,
},
}),
[changeParams] // eslint-disable-line react-hooks/exhaustive-deps
);
// Inside <ReferenceManyField> it instead returns:
const setFilters = useCallback(
(filters, displayedFilters) => {
setFilterValues(removeEmpty(filters));
setDisplayedFilters(displayedFilters);
setPage(1);
},
[setDisplayedFilters, setFilterValues, setPage]
);
// NOTE: inside useReferenceManyFieldController the third parameter `debounce` is ignored
Other information:
- https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/controller/list/useListController.ts#L419
- https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/controller/list/useListParams.ts#L226
- https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/controller/field/useReferenceManyFieldController.ts#L131
Environment
- React-admin version: 4.16.2
- Last version that did not exhibit the issue (if applicable): -
- React version: 18.2.0
- Browser: Chrome
- Stack trace (in case of a JS error): -
Activity