-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncAutocomplete.tsx
More file actions
149 lines (135 loc) · 5.21 KB
/
Copy pathAsyncAutocomplete.tsx
File metadata and controls
149 lines (135 loc) · 5.21 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { useState, useRef, useEffect, useCallback } from 'react';
import type { ChipTypeMap } from '@mui/material/Chip';
import { useInfiniteQuery, UseInfiniteQueryOptions } from '@tanstack/react-query';
import { AutocompleteInputChangeReason } from '@mui/material/Autocomplete';
import { Autocomplete, AutocompleteProps } from './Autocomplete';
import { useDebounce } from './util';
export interface AsyncAutocompleteProps<
Option,
Multiple extends boolean | undefined,
DisableClearable extends boolean | undefined,
FreeSolo extends boolean | undefined,
ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
> extends Omit<
AutocompleteProps<Option, Multiple, DisableClearable, FreeSolo, ChipComponent>,
'options' | 'disableListWrap' | 'loading'
> {
/** Function that is called to fetch the options for the list. Returns a promise with options, hasMore, and offset */
loadOptions: (
offset: number,
limit: number,
inputValue?: string
) => Promise<{ options: Option[]; hasMore: boolean; offset: number }>;
/** The key used by @tanstack/react-query to cache the response */
queryKey: string;
/** The number of options to request from the api
* @default 50 */
limit?: number;
/** Config options for the useInfiniteQuery hook */
queryOptions?: UseInfiniteQueryOptions<{ options: Option[]; hasMore: boolean; offset: number }>;
/** Object of parameters used for the cacheKey. Options are re-refetched when a value in the object changes */
watchParams?: Record<string, unknown>;
/** Time to wait before searching with the input value typed into the component */
debounceTimeout?: number;
/** Options to prepend to the list (e.g., frequently used items). These will be filtered from loadOptions results to avoid duplicates. */
prependOptions?: Option[];
}
export const AsyncAutocomplete = <
Option,
Multiple extends boolean | undefined = false,
DisableClearable extends boolean | undefined = false,
FreeSolo extends boolean | undefined = false,
ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
>({
loadOptions,
limit = 50,
queryKey,
ListboxProps,
queryOptions,
watchParams,
debounceTimeout = 350,
FieldProps,
onInputChange,
prependOptions = [],
...rest
}: AsyncAutocompleteProps<Option, Multiple, DisableClearable, FreeSolo, ChipComponent>) => {
const [inputValue, setInputValue] = useState('');
const listboxRef = useRef<Element>(null);
const setListboxRef = useCallback((node: Element) => {
listboxRef.current = node;
}, [])
const handleInputPropsOnChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setInputValue(event.target.value);
// Call passed in onChange if present
if (FieldProps?.InputProps?.onChange) FieldProps.InputProps.onChange(event);
};
const debouncedInput = useDebounce(inputValue, debounceTimeout);
const { isLoading, isFetching, data, hasNextPage, fetchNextPage } = useInfiniteQuery({
queryKey: [queryKey, limit, debouncedInput, watchParams],
queryFn: async ({ pageParam = 0 }) => loadOptions(pageParam, limit, debouncedInput),
staleTime: 10000,
getNextPageParam: (lastPage) => (lastPage.hasMore ? lastPage.offset + limit : undefined),
...queryOptions,
});
const options = data?.pages ? data.pages.map((page) => page.options).flat() : [];
const finalOptions =
prependOptions.length > 0
? [
...prependOptions,
...options.filter(
(option) =>
!prependOptions.some((prepended) =>
rest.isOptionEqualToValue ? rest.isOptionEqualToValue(option, prepended) : option === prepended
)
),
]
: options;
const handleOnInputChange = (
event: React.ChangeEvent<HTMLInputElement>,
value: string,
reason: AutocompleteInputChangeReason
) => {
if (reason === 'clear') {
setInputValue(event.target.value);
} else if (reason === 'blur') {
setInputValue(value);
}
if (onInputChange) onInputChange(event, value, reason);
};
const handleAddingOptions = async (event: React.SyntheticEvent) => {
const listboxNode = event.currentTarget;
const difference = listboxNode.scrollHeight - (listboxNode.scrollTop + listboxNode.clientHeight);
// Only fetch if we are near the bottom, not already fetching, and there are more results
if (difference <= 10 && !isLoading && !isFetching && hasNextPage) {
fetchNextPage();
}
}
// trigger scroll event every time options added in case too many options filtered out to allow for scroll
// allow onScroll to determine if next page should be fetched
useEffect(() => {
if (hasNextPage) {
listboxRef.current?.dispatchEvent(new UIEvent('scroll'));
}
}, [data?.pages.length, hasNextPage]);
return (
<Autocomplete
{...rest}
onInputChange={handleOnInputChange}
FieldProps={{
...FieldProps,
InputProps: {
...FieldProps?.InputProps,
onChange: handleInputPropsOnChange,
},
}}
loading={isFetching}
options={finalOptions}
ListboxProps={{
...ListboxProps,
ref: setListboxRef,
onScroll: handleAddingOptions,
onPointerEnter: handleAddingOptions
}}
/>
);
};