/
TokenizingField.tsx
385 lines (339 loc) · 11.3 KB
/
TokenizingField.tsx
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import {Menu, MenuItem, Popover, Spinner, TagInput} from '@blueprintjs/core';
import {isEqual} from 'lodash';
import React from 'react';
import styled from 'styled-components/macro';
export interface SuggestionProvider {
token: string;
values: () => string[];
}
interface Suggestion {
text: string;
final: boolean;
}
interface ActiveSuggestionInfo {
text: string;
idx: number;
}
export interface TokenizingFieldValue {
token?: string;
value: string;
}
interface TokenizingFieldProps {
values: TokenizingFieldValue[];
maxValues?: number;
onChange: (values: TokenizingFieldValue[]) => void;
onChangeBeforeCommit?: boolean;
placeholder?: string;
loading?: boolean;
className?: string;
small?: boolean;
suggestionProviders: SuggestionProvider[];
suggestionProvidersFilter?: (
suggestionProvider: SuggestionProvider[],
values: TokenizingFieldValue[],
) => SuggestionProvider[];
}
function findProviderByToken(token: string, providers: SuggestionProvider[]) {
return providers.find((p) => p.token.toLowerCase() === token.toLowerCase());
}
export function tokenizedValuesFromString(str: string, providers: SuggestionProvider[]) {
if (str === '') {
return [];
}
const tokens = str.split(',');
return tokens.map((token) => tokenizedValueFromString(token, providers));
}
export function tokenizedValueFromString(
str: string,
providers: SuggestionProvider[],
): TokenizingFieldValue {
const parts = str.split(':');
if (parts.length === 2 && findProviderByToken(parts[0], providers)) {
return {token: parts[0], value: parts[1]};
}
return {value: str};
}
export function stringFromValue(value: TokenizingFieldValue[]) {
return value
.filter((v) => v.value !== '')
.map((v) => (v.token ? `${v.token}:${v.value}` : v.value))
.join(',');
}
/** Provides a text field with typeahead autocompletion for key value pairs,
where the key is one of a known set of "suggestion provider tokens". Provide
one or more SuggestionProviders to build the tree of autocompletions. The
input also allows for freeform typing (`value` items with no token value) */
export const TokenizingField: React.FunctionComponent<TokenizingFieldProps> = ({
suggestionProviders,
suggestionProvidersFilter,
values: externalValues,
maxValues,
onChange,
onChangeBeforeCommit,
placeholder,
loading,
small,
className,
}) => {
const [open, setOpen] = React.useState<boolean>(false);
const [active, setActive] = React.useState<ActiveSuggestionInfo | null>(null);
const [typed, setTyped] = React.useState<string>('');
const values = React.useMemo(() => [...externalValues], [externalValues]);
const typedValue = tokenizedValueFromString(typed, suggestionProviders);
if (isEqual(typedValue, values[values.length - 1])) {
values.pop();
}
const atMaxValues = maxValues !== undefined && values.length >= maxValues;
const filteredSuggestionProviders = suggestionProvidersFilter
? suggestionProvidersFilter(suggestionProviders, values)
: suggestionProviders;
// Build the set of suggestions that should be displayed for the current input value.
// Note: "typed" is the text that has not yet been submitted, separate from values[].
const parts = typed.split(':');
const lastPart = parts[parts.length - 1].toLowerCase();
const suggestions = React.useMemo(() => {
if (atMaxValues) {
return [];
}
let suggestionsArr: Suggestion[] = [];
const suggestionMatchesTypedText = (s: Suggestion) =>
!lastPart ||
s.text
.toLowerCase()
.split(':')
.some((c) => c.includes(lastPart));
const availableSuggestionsForProvider = (provider: SuggestionProvider) => {
const suggestionNotUsed = (v: string) =>
!values.some((e) => e.token === provider.token && e.value === v);
return provider
.values()
.filter(suggestionNotUsed)
.map((v) => ({text: `${provider.token}:${v}`, final: true}))
.filter(suggestionMatchesTypedText)
.slice(0, 6); // never show too many suggestions for one provider
};
if (parts.length === 1) {
// Suggest providers (eg: `pipeline:`) so users can discover the search space
suggestionsArr = filteredSuggestionProviders
.map((s) => ({text: `${s.token}:`, final: false}))
.filter(suggestionMatchesTypedText);
// Suggest value completions so users can type "airline_" without the "pipeline"
// prefix and get the correct suggestion.
if (typed.length > 0) {
for (const p of filteredSuggestionProviders) {
suggestionsArr.push(...availableSuggestionsForProvider(p));
}
}
}
if (parts.length === 2) {
// Suggest values from the chosen provider (eg: `pipeline:abc`)
const provider = findProviderByToken(parts[0], filteredSuggestionProviders);
suggestionsArr = provider ? availableSuggestionsForProvider(provider) : [];
}
// Truncate suggestions to the ones currently matching the typed text,
// and always sort them in alphabetical order.
suggestionsArr.sort((a, b) => a.text.localeCompare(b.text));
return suggestionsArr;
}, [atMaxValues, filteredSuggestionProviders, lastPart, parts, typed.length, values]);
// We need to manage selection in the dropdown by ourselves. To ensure the
// best behavior we store the active item's index and text (the text allows
// us to relocate it if it's moved and the index allows us to keep selection
// at the same location if the previous item is gone.)
// This hook keeps the active row state in sync with the suggestions, which
// are derived from the current input value.
React.useEffect(() => {
// If suggestions are present, autoselect the first one so the user can press
// enter to complete their search. (Esc + enter is how you enter your raw text.)
if (!active && suggestions.length) {
setActive({text: suggestions[0].text, idx: 0});
return;
}
if (!active) {
return;
}
if (suggestions.length === 0) {
setActive(null);
return;
}
// Relocate the currently active item in the latest suggestions list
const pos = suggestions.findIndex((a) => a.text === active.text);
// The new index is the index of the active item, or whatever item
// is now at it's location if it's gone, bounded to the array.
let nextIdx = pos !== -1 ? pos : active.idx;
nextIdx = Math.max(0, Math.min(suggestions.length - 1, nextIdx));
const nextText = suggestions[nextIdx] && suggestions[nextIdx].text;
if (nextIdx !== active.idx || nextText !== active.text) {
setActive({text: nextText, idx: nextIdx});
}
}, [active, suggestions]);
const onConfirmSuggestion = (suggestion: Suggestion) => {
if (atMaxValues) {
return;
}
if (suggestion.final) {
// The user has finished a key-value pair
onConfirmText(suggestion.text);
setTyped('');
setActive(null);
setOpen(false);
} else {
// The user has finished a key
setTyped(suggestion.text);
}
};
const onConfirmText = (str: string) => {
if (atMaxValues) {
return;
}
if (str.endsWith(':')) {
return;
}
if (str === '') {
return;
}
setTyped('');
onChange([...values, tokenizedValueFromString(str, filteredSuggestionProviders)]);
};
const onKeyDown = (e: React.KeyboardEvent<any>) => {
if (atMaxValues && e.key !== 'Delete' && e.key !== 'Backspace') {
e.preventDefault();
e.stopPropagation();
return;
}
// Enter and Return confirm the currently selected suggestion or
// confirm the freeform text you've typed if no suggestions are shown.
if (e.key === 'Enter' || e.key === 'Return' || e.key === 'Tab') {
if (active) {
const picked = suggestions.find((s) => s.text === active.text);
if (!picked) {
throw new Error('Selection out of sync with suggestions');
}
onConfirmSuggestion(picked);
e.preventDefault();
e.stopPropagation();
} else if (typed.length) {
onConfirmText(typed);
e.preventDefault();
e.stopPropagation();
}
return;
}
// Typing space confirms your freeform text
if (e.key === ' ') {
e.preventDefault();
onConfirmText(typed);
return;
}
// Escape closes the options. The options re-open if you type another char or click.
if (e.key === 'Escape') {
setActive(null);
setOpen(false);
return;
}
if (!open && e.key !== 'Delete' && e.key !== 'Backspace') {
setOpen(true);
}
// The up/down arrow keys shift selection in the dropdown.
// Note: The first down arrow press activates the first item.
const shift = {ArrowDown: 1, ArrowUp: -1}[e.key];
if (shift && suggestions.length > 0) {
e.preventDefault();
let idx = (active ? active.idx : -1) + shift;
idx = Math.max(0, Math.min(idx, suggestions.length - 1));
setActive({text: suggestions[idx].text, idx});
}
};
return (
<Popover
minimal={true}
isOpen={open && suggestions.length > 0 && !atMaxValues}
position={'bottom-left'}
content={
suggestions.length > 0 ? (
<StyledMenu>
{suggestions.slice(0, 20).map((suggestion, idx) => (
<StyledMenuItem
key={suggestion.text}
text={suggestion.text}
shouldDismissPopover={false}
active={active ? active.idx === idx : false}
onMouseDown={(e: React.MouseEvent<any>) => {
e.preventDefault();
e.stopPropagation();
onConfirmSuggestion(suggestion);
setActive(null);
}}
/>
))}
</StyledMenu>
) : (
<div />
)
}
>
<StyledTagInput
small={small}
className={className}
values={values.map((v) => (v.token ? `${v.token}:${v.value}` : v.value))}
inputValue={typed}
onRemove={(_, idx) => {
const next = [...values];
next.splice(idx, 1);
onChange(next);
}}
onInputChange={(e) => {
setTyped(e.currentTarget.value);
if (onChangeBeforeCommit) {
const tokenized = tokenizedValueFromString(
e.currentTarget.value,
filteredSuggestionProviders,
);
onChange([...values, tokenized]);
}
}}
inputProps={{
onFocus: () => setOpen(true),
onBlur: () => setOpen(false),
}}
onAdd={() => false}
onKeyDown={onKeyDown}
tagProps={{minimal: true}}
placeholder={placeholder || 'Filter...'}
rightElement={
loading ? (
<div style={{padding: 3}}>
<Spinner size={17} />
</div>
) : undefined
}
/>
</Popover>
);
};
const StyledTagInput = styled(TagInput)<{small?: boolean}>`
min-width: 400px;
max-width: 600px;
input {
font-size: 12px;
}
${({small}) =>
small
? `height: 20px;
min-width: 200px;
max-width: 800px;
&.bp3-tag-input {
min-height: 26px;
}
&.bp3-tag-input .bp3-tag-input-values {
height: 23px;
margin-top: 3px;
}`
: ``}
`;
const StyledMenu = styled(Menu)`
width: 400px;
`;
const StyledMenuItem = styled(MenuItem)`
font-size: 13px;
line-height: 15px;
`;