-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathuseChoicesContext.ts
More file actions
57 lines (55 loc) · 2.55 KB
/
useChoicesContext.ts
File metadata and controls
57 lines (55 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useContext, useMemo } from 'react';
import { RaRecord } from '../../types';
import { useList } from '../../controller';
import { ChoicesContext, ChoicesContextValue } from './ChoicesContext';
export const useChoicesContext = <ChoicesType extends RaRecord = RaRecord>(
options: Partial<ChoicesContextValue> & {
choices?: readonly ChoicesType[];
} = {}
): ChoicesContextValue => {
const context = useContext(ChoicesContext) as ChoicesContextValue<
ChoicesType
>;
const { data, ...list } = useList<ChoicesType>({
data: options.choices,
isLoading: options.isLoading,
// When not in a ChoicesContext, paginating does not make sense (e.g. AutocompleteInput).
perPage: Infinity,
});
const result = useMemo(() => {
// Props take precedence over context.
if (options.choices || !context) {
return {
allChoices: data,
availableChoices: options.availableChoices ?? data,
selectedChoices: options.selectedChoices ?? data,
displayedFilters:
options.selectedChoices ?? list.displayedFilters,
error: options.error,
filter: options.filter ?? list.filter,
filterValues: options.filterValues ?? list.filterValues,
hasNextPage: options.hasNextPage ?? list.hasNextPage,
hasPreviousPage:
options.hasPreviousPage ?? list.hasPreviousPage,
hideFilter: options.hideFilter ?? list.hideFilter,
isLoading: list.isLoading, // we must take the one for useList, otherwise the loading state isn't synchronized with the data
isFetching: list.isFetching, // same
page: options.page ?? list.page,
perPage: options.perPage ?? list.perPage,
refetch: options.refetch ?? list.refetch,
resource: options.resource ?? list.resource,
setFilters: options.setFilters ?? list.setFilters,
setPage: options.setPage ?? list.setPage,
setPerPage: options.setPerPage ?? list.setPerPage,
setSort: options.setSort ?? list.setSort,
showFilter: options.showFilter ?? list.showFilter,
sort: options.sort ?? list.sort,
source: options.source,
total: options.total ?? list.total,
isFromReference: false,
};
}
return context;
}, [context, data, list, options]);
return result;
};