Newer
Older
import React, { useState, useRef, useEffect } from 'react';
import lively from '@jlongster/lively';
import Downshift from 'downshift';
import { css } from 'glamor';
import { colors } from '../style';
import Remove from '../svg/v2/Remove';
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
import { View, Input, Tooltip, Button } from './common';
function findItem(strict, suggestions, value) {
if (strict) {
let idx = suggestions.findIndex(item => item.id === value);
return idx === -1 ? null : suggestions[idx];
}
return value;
}
function getItemName(item) {
if (item == null) {
return '';
} else if (typeof item === 'string') {
return item;
}
return item.name || '';
}
function getItemId(item) {
if (typeof item === 'string') {
return item;
}
return item ? item.id : null;
}
function getInitialState({
props: {
value,
suggestions,
embedded,
isOpen = false,
strict,
initialFilterSuggestions
}
}) {
let selectedItem = findItem(strict, suggestions, value);
let filteredSuggestions = initialFilterSuggestions
? initialFilterSuggestions(suggestions, value)
: null;
return {
selectedItem,
value: selectedItem ? getItemName(selectedItem) : '',
originalItem: selectedItem,
filteredSuggestions,
highlightedIndex: null,
isOpen: embedded || isOpen
};
}
function componentWillReceiveProps(bag, nextProps) {
let {
strict,
suggestions,
filterSuggestions = defaultFilterSuggestions,
initialFilterSuggestions,
value,
itemToString = defaultItemToString
} = nextProps;
let { value: currValue } = bag.state;
let updates = null;
function updateValue() {
let selectedItem = findItem(strict, suggestions, value);
if (selectedItem) {
updates = updates || {};
updates.value = itemToString(selectedItem);
updates.selectedItem = selectedItem;
}
}
if (bag.props.value !== value) {
updateValue();
}
// TODO: Something is causing a rerender immediately after first
// render, and this condition is true, causing items to be filtered
// twice. This shouldn't effect functionality (I think), but look
// into this later
if (bag.props.suggestions !== suggestions) {
let filteredSuggestions = null;
if (bag.state.highlightedIndex != null) {
filteredSuggestions = filterSuggestions(suggestions, currValue);
} else {
filteredSuggestions = initialFilterSuggestions
? initialFilterSuggestions(suggestions, currValue)
: null;
}
updates = updates || {};
updateValue();
updates.filteredSuggestions = filteredSuggestions;
}
return updates;
}
export function defaultFilterSuggestion(suggestion, value) {
return getItemName(suggestion).toLowerCase().includes(value.toLowerCase());
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
}
export function defaultFilterSuggestions(suggestions, value) {
return suggestions.filter(suggestion =>
defaultFilterSuggestion(suggestion, value)
);
}
function fireUpdate(onUpdate, strict, suggestions, index, value) {
// If the index is null, look up the id in the suggestions. If the
// value is empty it will select nothing (as expected). If it's not
// empty but nothing is selected, it still resolves to an id. It
// would very confusing otherwise: the menu could be in a state
// where nothing is highlighted but there is a valid value.
let selected = null;
if (!strict) {
selected = value;
} else {
if (index == null) {
// If passing in a value directly, validate the id
let sug = suggestions.find(sug => sug.id === value);
if (sug) {
selected = sug.id;
}
} else if (index < suggestions.length) {
selected = suggestions[index].id;
}
}
onUpdate && onUpdate(selected);
}
function onInputValueChange(
{
props: {
suggestions,
onUpdate,
multi,
highlightFirst,
strict,
filterSuggestions = defaultFilterSuggestions,
getHighlightedIndex
},
state: { isOpen }
},
value,
changes
) {
// OMG this is the dumbest thing ever. I need to remove Downshift
// and build my own component. For some reason this is fired on blur
// with an empty value which clears out the input when the app blurs
if (!document.hasFocus()) {
return;
}
// Do nothing if it's simply updating the selected item
if (
changes.type ===
Downshift.stateChangeTypes.controlledPropUpdatedSelectedItem
) {
return;
}
// Otherwise, filter the items and always the first item if
// desired
const filteredSuggestions = filterSuggestions(suggestions, value);
if (value === '') {
// A blank value shouldn't highlight any item so that the field
// can be left blank if desired
if (changes.type !== Downshift.stateChangeTypes.clickItem) {
fireUpdate(onUpdate, strict, filteredSuggestions, null, null);
}
return {
value,
filteredSuggestions,
highlightedIndex: null
};
} else {
let defaultGetHighlightedIndex = filteredSuggestions => {
return highlightFirst && filteredSuggestions.length ? 0 : null;
};
let highlightedIndex = (getHighlightedIndex || defaultGetHighlightedIndex)(
filteredSuggestions
);
if (changes.type !== Downshift.stateChangeTypes.clickItem) {
fireUpdate(
onUpdate,
strict,
filteredSuggestions,
highlightedIndex,
value
);
}
return {
value,
filteredSuggestions,
highlightedIndex
};
}
}
function onStateChange({ props, state, inst }, changes, stateAndHelpers) {
if (
props.tableBehavior &&
changes.type === Downshift.stateChangeTypes.mouseUp
) {
return;
}
const newState = {};
if ('highlightedIndex' in changes) {
newState.highlightedIndex = changes.highlightedIndex;
}
if ('isOpen' in changes) {
newState.isOpen = props.embedded ? true : changes.isOpen;
}
if ('selectedItem' in changes) {
newState.selectedItem = changes.selectedItem;
}
// We only ever want to update the value if the user explicitly
// highlighted an item via the keyboard. It shouldn't change with
// mouseover; otherwise the user could accidentally hover over an
// item without realizing it and change the value.
if (
state.isOpen &&
(changes.type === Downshift.stateChangeTypes.keyDownArrowUp ||
changes.type === Downshift.stateChangeTypes.keyDownArrowDown)
) {
fireUpdate(
props.onUpdate,
props.strict,
state.filteredSuggestions || props.suggestions,
newState.highlightedIndex != null
? newState.highlightedIndex
: state.highlightedIndex,
state.value
);
}
inst.lastChangeType = changes.type;
return newState;
}
function onSelect(
{ props: { onSelect, clearAfterSelect, suggestions }, inst },
item
) {
if (onSelect) {
// I AM NOT PROUD OF THIS OK??
// This WHOLE FILE is a mess anyway
// OK SIT DOWN AND I WILL EXPLAIN
// This component uses `componentWillReceiveProps` and in there
// it will re-filter suggestions if the suggestions change and
// a `highlightedIndex` exists. When we select something,
// we clear `highlightedIndex` so it should show all suggestions
// again. HOWEVER, in the case of a multi-autocomplete, it's
// changing the suggestions every time something is selected.
// In that case, cWRP is running *before* our state setting that
// cleared `highlightedIndex`. Forcing this to run later assures
// us that we will clear out local state before cWRP runs.
// YEAH THAT'S ALL OK I JUST WANT TO SHIP THIS
setTimeout(() => {
onSelect(getItemId(item));
}, 0);
}
return onSelectAfter(suggestions, clearAfterSelect, inst);
}
function onSelectAfter(suggestions, clearAfterSelect, inst) {
if (clearAfterSelect) {
return {
value: '',
selectedItem: null,
highlightedIndex: null,
filteredSuggestions: suggestions
};
} else if (inst.input) {
inst.input.setSelectionRange(0, 10000);
}
}
function onChange({ props: { inputProps } }, e) {
const { onChange } = inputProps || {};
onChange && onChange(e.target.value);
}
function onKeyDown(
{
props: {
suggestions,
clearAfterSelect,
initialFilterSuggestions,
embedded,
onUpdate,
onSelect,
inputProps,
shouldSaveFromKey = defaultShouldSaveFromKey,
strict
},
state: {
selectedItem,
filteredSuggestions,
highlightedIndex,
originalItem,
isNulled,
isOpen,
value
},
inst
},
e
) {
let ENTER = 13;
let ESC = 27;
let { onKeyDown } = inputProps || {};
// If the dropdown is open, an item is highlighted, and the user
// pressed enter, always capture that and handle it ourselves
if (isOpen) {
if (e.keyCode === ENTER) {
if (highlightedIndex != null) {
if (inst.lastChangeType === Downshift.stateChangeTypes.itemMouseEnter) {
// If the last thing the user did was hover an item, intentionally
// ignore the default behavior of selecting the item. It's too
// common to accidentally hover an item and then save it
e.preventDefault();
} else {
// Otherwise, stop propagation so that the table navigator
// doesn't handle it
e.stopPropagation();
}
} else if (!strict) {
// Handle it ourselves
e.stopPropagation();
onSelect(value);
return onSelectAfter(suggestions, clearAfterSelect, inst);
} else {
// No highlighted item, still allow the table to save the item
// as `null`, even though we're allowing the table to move
e.preventDefault();
onKeyDown && onKeyDown(e);
}
} else if (shouldSaveFromKey(e)) {
e.preventDefault();
onKeyDown && onKeyDown(e);
}
}
// Handle escape ourselves
if (e.keyCode === ESC) {
e.preventDefault();
if (!embedded) {
e.stopPropagation();
}
let filteredSuggestions = initialFilterSuggestions
? initialFilterSuggestions(suggestions, getItemName(originalItem))
: null;
fireUpdate(onUpdate, strict, suggestions, null, getItemId(originalItem));
return {
value: getItemName(originalItem),
selectedItem: findItem(strict, suggestions, originalItem),
filteredSuggestions,
highlightedIndex: null,
isOpen: embedded ? true : false
};
}
}
function defaultRenderInput(props) {
return <Input {...props} />;
}
function defaultRenderItems(items, getItemProps, highlightedIndex) {
return (
<div>
{items.map((item, index) => {
let name = getItemName(item);
return (
<div
{...getItemProps({ item })}
key={name}
{...css({
padding: 5,
cursor: 'default',
backgroundColor: highlightedIndex === index ? colors.n4 : null
})}
>
{name}
</div>
);
})}
</div>
);
}
function defaultShouldSaveFromKey(e) {
// Enter
return e.keyCode === 13;
}
function onFocus({ inst, props: { inputProps = {}, openOnFocus = true } }, e) {
inputProps.onFocus && inputProps.onFocus(e);
if (openOnFocus) {
return { isOpen: true };
}
}
function onBlur({ inst, props, state: { selectedItem } }, e) {
let { inputProps = {}, onSelect } = props;
e.preventDownshiftDefault = true;
inputProps.onBlur && inputProps.onBlur(e);
if (!props.tableBehavior) {
if (e.target.value === '') {
onSelect && onSelect(null);
return { selectedItem: null, originalValue: null, isOpen: false };
}
// If not using table behavior, reset the input on blur. Tables
// handle saving the value on blur.
let value = selectedItem ? getItemId(selectedItem) : null;
return getInitialState({
props: {
...props,
value,
originalValue: value
}
});
} else {
return { isOpen: false };
}
}
function defaultItemToString(item) {
return item ? getItemName(item) : '';
}
function _SingleAutocomplete({
props: {
focused,
embedded,
containerProps,
inputProps,
children,
suggestions,
tooltipStyle,
onItemClick,
strict,
tooltipProps,
renderInput = defaultRenderInput,
renderItems = defaultRenderItems,
itemToString = defaultItemToString
},
state: { value, selectedItem, filteredSuggestions, highlightedIndex, isOpen },
updater,
inst
}) {
const filtered = filteredSuggestions || suggestions;
return (
<Downshift
onSelect={updater(onSelect)}
highlightedIndex={highlightedIndex}
selectedItem={selectedItem || null}
itemToString={itemToString}
inputValue={value}
isOpen={isOpen}
onInputValueChange={updater(onInputValueChange)}
onStateChange={updater(onStateChange)}
>
{({
getInputProps,
getItemProps,
getRootProps,
isOpen,
inputValue,
selectedItem,
highlightedIndex
}) => (
// Super annoying but it works best to return a div so we
// can't use a View here, but we can fake it be using the
// className
<div
className={'view ' + css({ display: 'flex' }).toString()}
{...containerProps}
>
{renderInput(
getInputProps({
focused,
...inputProps,
onFocus: updater(onFocus),
onBlur: updater(onBlur),
onKeyDown: updater(onKeyDown),
onChange: updater(onChange)
})
)}
{isOpen &&
filtered.length > 0 &&
(embedded ? (
<View style={{ marginTop: 5 }} data-testid="autocomplete">
{renderItems(
filtered,
getItemProps,
highlightedIndex,
inputValue
)}
</View>
) : (
<Tooltip
position="bottom-stretch"
offset={2}
style={{
padding: 0,
backgroundColor: colors.n1,
color: 'white',
...tooltipStyle
}}
{...tooltipProps}
data-testid="autocomplete"
>
{renderItems(
filtered,
getItemProps,
highlightedIndex,
inputValue
)}
</Tooltip>
))}
</div>
)}
</Downshift>
);
}
const SingleAutocomplete = lively(_SingleAutocomplete, {
getInitialState,
componentWillReceiveProps
});
function MultiItem({ name, onRemove }) {
return (
<View
style={{
alignItems: 'center',
flexDirection: 'row',
backgroundColor: colors.b9,
padding: '2px 4px',
margin: '2px',
borderRadius: 4
}}
>
{name}
<Button type="button" bare style={{ marginLeft: 1 }} onClick={onRemove}>
<Remove style={{ width: 8, height: 8 }} />
</Button>
</View>
);
}
export function MultiAutocomplete({
value: selectedItems,
onSelect,
suggestions,
strict,
...props
}) {
let [focused, setFocused] = useState(false);
let lastSelectedItems = useRef();
useEffect(() => {
lastSelectedItems.current = selectedItems;
});
function onRemoveItem(id) {
let items = selectedItems.filter(i => i !== id);
onSelect(items);
}
function onAddItem(id) {
if (id) {
id = id.trim();
onSelect([...selectedItems, id], id);
}
}
function onKeyDown(e, prevOnKeyDown) {
if (e.key === 'Backspace' && e.target.value === '') {
onRemoveItem(selectedItems[selectedItems.length - 1]);
}
prevOnKeyDown && prevOnKeyDown(e);
}
return (
<Autocomplete
{...props}
value={null}
suggestions={suggestions.filter(
item => !selectedItems.includes(getItemId(item))
)}
onSelect={onAddItem}
clearAfterSelect
highlightFirst
strict={strict}
tooltipProps={{
forceLayout: lastSelectedItems.current !== selectedItems
}}
renderInput={props => (
<View
style={[
{
display: 'flex',
flexWrap: 'wrap',
flexDirection: 'row',
alignItems: 'center',
backgroundColor: 'white',
borderRadius: 4,
border: '1px solid #d0d0d0'
},
focused && {
border: '1px solid ' + colors.b5,
boxShadow: '0 1px 1px ' + colors.b7
}
]}
>
{selectedItems.map((item, idx) => {
item = findItem(strict, suggestions, item);
return (
item && (
<MultiItem
key={getItemId(item) || idx}
name={getItemName(item)}
onRemove={() => onRemoveItem(getItemId(item))}
/>
)
);
})}
<Input
{...props}
onKeyDown={e => onKeyDown(e, props.onKeyDown)}
onFocus={e => {
setFocused(true);
props.onFocus(e);
}}
onBlur={e => {
setFocused(false);
props.onBlur(e);
}}
style={[
{
flex: 1,
minWidth: 30,
border: 0,
':focus': { border: 0, boxShadow: 'none' }
},
props.style
]}
/>
</View>
)}
/>
);
}
export function AutocompleteFooterButton({
title,
style,
hoveredStyle,
onClick
}) {
return (
<Button
style={[
{
fontSize: 12,
color: colors.n10,
backgroundColor: 'transparent',
borderColor: colors.n5
},
style
]}
hoveredStyle={[
{ backgroundColor: 'rgba(200, 200, 200, .25)' },
hoveredStyle
]}
onClick={onClick}
>
{title}
</Button>
);
}
export function AutocompleteFooter({ show = true, embedded, children }) {
return (
show && (
<View
style={[
{ flexShrink: 0 },
embedded ? { paddingTop: 5 } : { padding: 5 }
]}
onMouseDown={e => e.preventDefault()}
>
{children}
</View>
)
);
}
export default function Autocomplete({ multi, ...props }) {
if (multi) {
return <MultiAutocomplete {...props} />;
} else {
return <SingleAutocomplete {...props} />;
}
}