-
Notifications
You must be signed in to change notification settings - Fork 228
Expand file tree
/
Copy pathSuggestBox.tsx
More file actions
289 lines (259 loc) · 8.07 KB
/
SuggestBox.tsx
File metadata and controls
289 lines (259 loc) · 8.07 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
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
import type { FormEvent, ReactNode } from "react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import {
autoUpdate,
flip,
FloatingFocusManager,
FloatingPortal,
size,
useDismiss,
useFloating,
useFocus,
useInteractions,
useListNavigation,
useRole,
} from "@floating-ui/react";
import { css, styled } from "styled-components";
import type { Option } from "./options";
import { findMatchingOptions } from "./options";
import { SuggestBoxItem } from "./SuggestBoxItem";
import { LabelText } from "./LabelText";
import type { Diagnostic } from "./diagnostics";
import { useOpenKey } from "./useOpenKey";
import { VscodeTextfield } from "@vscode-elements/react-elements";
interface InputProps {
$error: boolean;
}
const Input = styled(VscodeTextfield)<InputProps>`
width: 100%;
font-family: var(--vscode-editor-font-family);
${(props: InputProps) =>
props.$error &&
css`
--dropdown-border: var(--vscode-inputValidation-errorBorder);
--focus-border: var(--vscode-inputValidation-errorBorder);
`}
`;
const Container = styled.div`
display: flex;
flex-direction: column;
border-radius: 3px;
background-color: var(--vscode-editorSuggestWidget-background);
border: 1px solid var(--vscode-editorSuggestWidget-border);
user-select: none;
`;
const ListContainer = styled(Container)`
font-size: 95%;
`;
const NoSuggestionsContainer = styled(Container)`
padding-top: 2px;
padding-bottom: 2px;
`;
const NoSuggestionsText = styled.div`
padding-left: 22px;
`;
export type SuggestBoxProps<
T extends Option<T>,
D extends Diagnostic = Diagnostic,
> = {
value?: string;
onChange: (value: string) => void;
options: T[];
/**
* Parse the value into tokens that can be used to match against the options. The
* tokens will be passed to {@link findMatchingOptions}.
* @param value The user-entered value to parse.
*/
parseValueToTokens: (value: string) => string[];
/**
* Validate the value. This is used to show syntax errors in the input.
* @param value The user-entered value to validate.
*/
validateValue?: (value: string) => D[];
/**
* Get the icon to display for an option.
* @param option The option to get the icon for.
*/
getIcon?: (option: T) => ReactNode | undefined;
/**
* Get the details text to display for an option.
* @param option The option to get the details for.
*/
getDetails?: (option: T) => ReactNode | undefined;
disabled?: boolean;
"aria-label"?: string;
/**
* Can be used to render a different component for the input. This is used
* in testing to use default HTML components rather than the VscodeTextfield
* for easier testing.
* @param props The props returned by `getReferenceProps` of {@link useInteractions}
*/
renderInputComponent?: (
props: Record<string, unknown>,
hasError: boolean,
) => ReactNode;
};
const stopClickPropagation = (e: React.MouseEvent) => {
e.stopPropagation();
};
export const SuggestBox = <
T extends Option<T>,
D extends Diagnostic = Diagnostic,
>({
value = "",
onChange,
options,
parseValueToTokens,
validateValue,
getIcon,
getDetails,
disabled,
"aria-label": ariaLabel,
renderInputComponent = (props, hasError) => (
<Input {...props} $error={hasError} />
),
}: SuggestBoxProps<T, D>) => {
const [isOpen, setIsOpen] = useState(false);
const [activeIndex, setActiveIndex] = useState<number | null>(null);
const listRef = useRef<Array<HTMLElement | null>>([]);
const { refs, floatingStyles, context } = useFloating<HTMLInputElement>({
whileElementsMounted: autoUpdate,
open: isOpen,
onOpenChange: setIsOpen,
placement: "bottom-start",
middleware: [
// Flip when the popover is too close to the bottom of the screen
flip({ padding: 10 }),
// Resize the popover to be fill the available height
size({
apply({ availableHeight, elements }) {
Object.assign(elements.floating.style, {
maxHeight: `${availableHeight}px`,
});
},
padding: 10,
}),
],
});
const focus = useFocus(context);
const role = useRole(context, { role: "listbox" });
const dismiss = useDismiss(context);
const openKey = useOpenKey(context);
const listNav = useListNavigation(context, {
listRef,
activeIndex,
onNavigate: setActiveIndex,
virtual: true,
loop: true,
});
const { getReferenceProps, getFloatingProps, getItemProps } = useInteractions(
[focus, role, dismiss, openKey, listNav],
);
const handleInput = useCallback(
(event: FormEvent<HTMLInputElement>) => {
const value = event.currentTarget.value;
onChange(value);
setIsOpen(true);
setActiveIndex(0);
},
[onChange],
);
const tokens = useMemo(() => {
return parseValueToTokens(value);
}, [value, parseValueToTokens]);
const suggestionItems = useMemo(() => {
return findMatchingOptions(options, tokens);
}, [options, tokens]);
const diagnostics = useMemo(
() => validateValue?.(value) ?? [],
[validateValue, value],
);
const hasSyntaxError = diagnostics.length > 0;
useEffect(() => {
if (disabled) {
setIsOpen(false);
}
}, [disabled]);
return (
<div onClick={stopClickPropagation}>
{renderInputComponent(
getReferenceProps({
ref: refs.setReference,
value,
onInput: handleInput,
"aria-autocomplete": "list",
"aria-label": ariaLabel,
onKeyDown: (event) => {
// When the user presses the enter or tab key, select the active item
if (
(event.key === "Enter" || event.key === "Tab") &&
activeIndex !== null &&
suggestionItems[activeIndex]
) {
onChange(suggestionItems[activeIndex].value);
setActiveIndex(null);
setIsOpen(false);
}
},
disabled,
}),
hasSyntaxError,
)}
{isOpen && (
<FloatingPortal>
{value && suggestionItems.length === 0 && (
<NoSuggestionsContainer
{...getFloatingProps({
ref: refs.setFloating,
style: floatingStyles,
})}
>
<NoSuggestionsText>No suggestions.</NoSuggestionsText>
</NoSuggestionsContainer>
)}
{suggestionItems.length > 0 && (
<FloatingFocusManager
context={context}
initialFocus={-1}
visuallyHiddenDismiss
// The default for returnFocus is true, but this doesn't work when opening
// the command palette in a VS Code webview. The focus is returned to the
// input element, but this closes the command palette immediately after opening
// it. By setting returnFocus to false, the focus is not immediately given to
// the input element, so the command palette stays open.
returnFocus={false}
>
<ListContainer
{...getFloatingProps({
ref: refs.setFloating,
style: floatingStyles,
})}
>
{suggestionItems.map((item, index) => (
<SuggestBoxItem
key={item.label}
{...getItemProps({
key: item.label,
ref(node) {
listRef.current[index] = node;
},
onClick() {
onChange(item.value);
setIsOpen(false);
refs.domReference.current?.focus();
},
})}
active={activeIndex === index}
icon={getIcon?.(item)}
labelText={<LabelText tokens={tokens} item={item} />}
details={getDetails?.(item)}
/>
))}
</ListContainer>
</FloatingFocusManager>
)}
</FloatingPortal>
)}
</div>
);
};