-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathonKeyDown.ts
209 lines (192 loc) · 5.35 KB
/
onKeyDown.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
import { onInput } from './onInput';
import {
ActionType,
AutocompleteScopeApi,
AutocompleteStore,
BaseItem,
InternalAutocompleteOptions,
} from './types';
import { getActiveItem } from './utils';
interface OnKeyDownOptions<TItem extends BaseItem>
extends AutocompleteScopeApi<TItem> {
event: KeyboardEvent;
props: InternalAutocompleteOptions<TItem>;
store: AutocompleteStore<TItem>;
}
export function onKeyDown<TItem extends BaseItem>({
event,
props,
refresh,
store,
...setters
}: OnKeyDownOptions<TItem>): void {
if (event.key === 'ArrowUp' || event.key === 'ArrowDown') {
// eslint-disable-next-line no-inner-declarations
function triggerScrollIntoView() {
const nodeItem = props.environment.document.getElementById(
`${props.id}-item-${store.getState().activeItemId}`
);
if (nodeItem) {
if ((nodeItem as any).scrollIntoViewIfNeeded) {
(nodeItem as any).scrollIntoViewIfNeeded(false);
} else {
nodeItem.scrollIntoView(false);
}
}
}
// eslint-disable-next-line no-inner-declarations
function triggerOnActive() {
const highlightedItem = getActiveItem(store.getState());
if (store.getState().activeItemId !== null && highlightedItem) {
const { item, itemInputValue, itemUrl, source } = highlightedItem;
source.onActive({
event,
item,
itemInputValue,
itemUrl,
refresh,
source,
state: store.getState(),
...setters,
});
}
}
// Default browser behavior changes the caret placement on ArrowUp and
// ArrowDown.
event.preventDefault();
// When re-opening the panel, we need to split the logic to keep the actions
// synchronized as `onInput` returns a promise.
if (
store.getState().isOpen === false &&
(props.openOnFocus || Boolean(store.getState().query))
) {
onInput({
event,
props,
query: store.getState().query,
refresh,
store,
...setters,
}).then(() => {
store.dispatch(event.key as ActionType, {
nextActiveItemId: props.defaultActiveItemId,
});
triggerOnActive();
// Since we rely on the DOM, we need to wait for all the micro tasks to
// finish (which include re-opening the panel) to make sure all the
// elements are available.
setTimeout(triggerScrollIntoView, 0);
});
} else {
store.dispatch(event.key, {});
triggerOnActive();
triggerScrollIntoView();
}
} else if (event.key === 'Escape') {
// This prevents the default browser behavior on `input[type="search"]`
// from removing the query right away because we first want to close the
// panel.
event.preventDefault();
store.dispatch(event.key, null);
// Hitting the `Escape` key signals the end of a user interaction with the
// autocomplete. At this point, we should ignore any requests that are still
// pending and could reopen the panel once they resolve, because that would
// result in an unsolicited UI behavior.
store.pendingRequests.cancelAll();
} else if (event.key === 'Enter') {
// No active item, so we let the browser handle the native `onSubmit` form
// event.
if (
store.getState().activeItemId === null ||
store
.getState()
.collections.every((collection) => collection.items.length === 0)
) {
return;
}
// This prevents the `onSubmit` event to be sent because an item is
// highlighted.
event.preventDefault();
const { item, itemInputValue, itemUrl, source } = getActiveItem(
store.getState()
)!;
if (event.metaKey || event.ctrlKey) {
if (itemUrl !== undefined) {
source.onSelect({
event,
item,
itemInputValue,
itemUrl,
refresh,
source,
state: store.getState(),
...setters,
});
props.navigator.navigateNewTab({
itemUrl,
item,
state: store.getState(),
});
}
} else if (event.shiftKey) {
if (itemUrl !== undefined) {
source.onSelect({
event,
item,
itemInputValue,
itemUrl,
refresh,
source,
state: store.getState(),
...setters,
});
props.navigator.navigateNewWindow({
itemUrl,
item,
state: store.getState(),
});
}
} else if (event.altKey) {
// Keep native browser behavior
} else {
if (itemUrl !== undefined) {
source.onSelect({
event,
item,
itemInputValue,
itemUrl,
refresh,
source,
state: store.getState(),
...setters,
});
props.navigator.navigate({
itemUrl,
item,
state: store.getState(),
});
return;
}
onInput({
event,
nextState: { isOpen: false },
props,
query: itemInputValue,
refresh,
store,
...setters,
}).then(() => {
source.onSelect({
event,
item,
itemInputValue,
itemUrl,
refresh,
source,
state: store.getState(),
...setters,
});
});
}
}
}