-
Notifications
You must be signed in to change notification settings - Fork 326
/
api.ts
303 lines (286 loc) · 7.92 KB
/
api.ts
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { AutocompleteAccessibilityGetters } from './getters';
import { AutocompleteSetters } from './setters';
import { AutocompleteState } from './state';
export interface AutocompleteApi<
TItem,
TEvent = Event,
TMouseEvent = MouseEvent,
TKeyboardEvent = KeyboardEvent
> extends AutocompleteSetters<TItem>,
AutocompleteAccessibilityGetters<
TItem,
TEvent,
TMouseEvent,
TKeyboardEvent
> {
/**
* Triggers a search to refresh the state.
*/
refresh: AutocompleteRefresh;
}
export type AutocompleteRefresh = () => Promise<void>;
export interface AutocompleteSuggestion<TItem> {
source: InternalAutocompleteSource<TItem>;
items: TItem[];
}
export interface GetSourcesParams<TItem> extends AutocompleteSetters<TItem> {
query: string;
state: AutocompleteState<TItem>;
refresh: AutocompleteRefresh;
}
interface ItemParams<TItem> {
suggestion: TItem;
suggestionValue: ReturnType<
InternalAutocompleteSource<TItem>['getInputValue']
>;
suggestionUrl: ReturnType<
InternalAutocompleteSource<TItem>['getSuggestionUrl']
>;
source: InternalAutocompleteSource<TItem>;
}
interface OnSelectParams<TItem>
extends ItemParams<TItem>,
AutocompleteSetters<TItem> {
state: AutocompleteState<TItem>;
event: any;
}
type OnHighlightParams<TItem> = OnSelectParams<TItem>;
interface OnSubmitParams<TItem> extends AutocompleteSetters<TItem> {
state: AutocompleteState<TItem>;
event: any;
}
interface OnInputParams<TItem> extends AutocompleteSetters<TItem> {
query: string;
state: AutocompleteState<TItem>;
refresh: AutocompleteRefresh;
}
export interface AutocompleteSource<TItem> {
// This allows flavors to pass other keys to their source.
// Example: `templates` in the JavaScript API
[key: string]: unknown;
/**
* Get the string value of the suggestion. The value is used to fill the search box.
*/
getInputValue?({
suggestion,
state,
}: {
suggestion: TItem;
state: AutocompleteState<TItem>;
}): string;
/**
* Get the URL of a suggestion. The value is used to create default navigation features for
* `onClick` and `onKeyDown`.
*/
getSuggestionUrl?({
suggestion,
state,
}: {
suggestion: TItem;
state: AutocompleteState<TItem>;
}): string | undefined;
/**
* Function called when the input changes. You can use this function to filter/search the items based on the query.
*/
getSuggestions(params: GetSourcesParams<TItem>): TItem[] | Promise<TItem[]>;
/**
* Function called when an item is selected.
*/
onSelect?(params: OnSelectParams<TItem>): void;
/**
* Function called when an item is highlighted.
*
* An item is highlighted either via keyboard navigation or via mouse over.
* You can trigger different behaviors based on the event `type`.
*/
onHighlight?(params: OnHighlightParams<TItem>): void;
}
export type InternalAutocompleteSource<TItem> = {
[KParam in keyof AutocompleteSource<TItem>]-?: AutocompleteSource<
TItem
>[KParam];
};
export type GetSources<TItem> = (
params: GetSourcesParams<TItem>
) => Promise<Array<InternalAutocompleteSource<TItem>>>;
export type Environment =
| Window
| {
[prop: string]: unknown;
addEventListener: Window['addEventListener'];
removeEventListener: Window['removeEventListener'];
setTimeout: Window['setTimeout'];
document: Window['document'];
location: {
assign: Location['assign'];
};
open: Window['open'];
};
interface Navigator<TItem> {
/**
* Called when a URL should be open in the current page.
*/
navigate(params: {
suggestionUrl: string;
suggestion: TItem;
state: AutocompleteState<TItem>;
}): void;
/**
* Called when a URL should be open in a new tab.
*/
navigateNewTab(params: {
suggestionUrl: string;
suggestion: TItem;
state: AutocompleteState<TItem>;
}): void;
/**
* Called when a URL should be open in a new window.
*/
navigateNewWindow(params: {
suggestionUrl: string;
suggestion: TItem;
state: AutocompleteState<TItem>;
}): void;
}
export type AutocompletePlugin<TItem, TData> = {
/**
* The sources to get the suggestions from.
*/
getSources?(
params: GetSourcesParams<TItem>
):
| Array<AutocompleteSource<TItem>>
| Promise<Array<AutocompleteSource<TItem>>>;
/**
* The function called when the autocomplete form is submitted.
*/
onSubmit?(params: OnSubmitParams<TItem>): void;
/**
* Function called when an item is selected.
*/
onSelect?(params: OnSelectParams<TItem>): void;
/**
* An extra plugin specific object to store variables and functions
*/
data?: TData;
};
export interface AutocompleteOptions<TItem> {
/**
* Whether to consider the experience in debug mode.
*
* The debug mode is useful when developing because it doesn't close
* the dropdown when the blur event occurs.
*
* @default false
*/
debug?: boolean;
/**
* The Autocomplete ID to create accessible attributes.
*
* @default "autocomplete-0"
*/
id?: string;
/**
* Function called when the internal state changes.
*/
onStateChange?<TItem>(props: { state: AutocompleteState<TItem> }): void;
/**
* The text that appears in the search box input when there is no query.
*/
placeholder?: string;
/**
* Whether to focus the search box when the page is loaded.
*
* @default false
*/
autoFocus?: boolean;
/**
* The default item index to pre-select.
*
* @default null
*/
defaultHighlightedIndex?: number | null;
/**
* Whether to show the highlighted suggestion as completion in the input.
*
* @default false
*/
enableCompletion?: boolean;
/**
* Whether to open the dropdown on focus when there's no query.
*
* @default false
*/
openOnFocus?: boolean;
/**
* The number of milliseconds that must elapse before the autocomplete
* experience is stalled.
*
* @default 300
*/
stallThreshold?: number;
/**
* The initial state to apply when autocomplete is created.
*/
initialState?: Partial<AutocompleteState<TItem>>;
/**
* The sources to get the suggestions from.
*/
getSources(
params: GetSourcesParams<TItem>
):
| Array<AutocompleteSource<TItem>>
| Promise<Array<AutocompleteSource<TItem>>>;
/**
* The environment from where your JavaScript is running.
* Useful if you're using autocomplete in a different context than
* `window`.
*
* @default window
*/
environment?: Environment;
/**
* Navigator API to redirect the user when a link should be opened.
*/
navigator?: Partial<Navigator<TItem>>;
/**
* The function called to determine whether the dropdown should open.
*/
shouldDropdownShow?(params: { state: AutocompleteState<TItem> }): boolean;
/**
* The function called when the autocomplete form is submitted.
*/
onSubmit?(params: OnSubmitParams<TItem>): void;
/**
* The function called when the input changes.
*
* This turns the experience in controlled mode, leaving you in charge of
* updating the state.
*/
onInput?(params: OnInputParams<TItem>): void;
/**
* The array of plugins.
*/
plugins?: Array<AutocompletePlugin<TItem, unknown>>;
}
// Props manipulated internally with default values.
export interface InternalAutocompleteOptions<TItem>
extends AutocompleteOptions<TItem> {
debug: boolean;
id: string;
onStateChange<TItem>(props: { state: AutocompleteState<TItem> }): void;
placeholder: string;
autoFocus: boolean;
defaultHighlightedIndex: number | null;
enableCompletion: boolean;
openOnFocus: boolean;
stallThreshold: number;
initialState: AutocompleteState<TItem>;
getSources: GetSources<TItem>;
environment: Environment;
navigator: Navigator<TItem>;
plugins: Array<AutocompletePlugin<TItem, unknown>>;
shouldDropdownShow(params: { state: AutocompleteState<TItem> }): boolean;
onSubmit(params: OnSubmitParams<TItem>): void;
onInput?(params: OnInputParams<TItem>): void | Promise<any>;
}