-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDropdown.react.js
131 lines (121 loc) · 3.65 KB
/
Dropdown.react.js
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
import {isNil, pluck, without, pick} from 'ramda';
import React, {useState, useCallback, useEffect, useMemo} from 'react';
import ReactDropdown from 'react-virtualized-select';
import createFilterOptions from 'react-select-fast-filter-options';
import '../components/css/[email protected]';
import '../components/css/[email protected]';
import '../components/css/Dropdown.css';
import {propTypes, defaultProps} from '../components/Dropdown.react';
import {sanitizeOptions} from '../utils/optionTypes';
// Custom tokenizer, see https://github.com/bvaughn/js-search/issues/43
// Split on spaces
const REGEX = /\s+/;
const TOKENIZER = {
tokenize(text) {
return text.split(REGEX).filter(
// Filter empty tokens
text => text
);
},
};
const RDProps = [
'multi',
'clearable',
'searchable',
'search_value',
'placeholder',
'disabled',
'optionHeight',
'style',
'className',
];
const Dropdown = props => {
const {
id,
clearable,
multi,
options,
setProps,
style,
loading_state,
value,
} = props;
const [optionsCheck, setOptionsCheck] = useState(null);
const [sanitizedOptions, filterOptions] = useMemo(() => {
const sanitized = sanitizeOptions(options);
return [
sanitized,
createFilterOptions({
options: sanitized,
tokenizer: TOKENIZER,
}),
];
}, [options]);
const onChange = useCallback(
selectedOption => {
if (multi) {
let value;
if (isNil(selectedOption)) {
value = [];
} else {
value = pluck('value', selectedOption);
}
setProps({value});
} else {
let value;
if (isNil(selectedOption)) {
value = null;
} else {
value = selectedOption.value;
}
setProps({value});
}
},
[multi]
);
const onInputChange = useCallback(
search_value => setProps({search_value}),
[]
);
useEffect(() => {
if (optionsCheck !== sanitizedOptions && !isNil(value)) {
const values = sanitizedOptions.map(option => option.value);
if (multi && Array.isArray(value)) {
const invalids = value.filter(v => !values.includes(v));
if (invalids.length) {
setProps({value: without(invalids, value)});
}
} else {
if (!values.includes(value)) {
setProps({value: null});
}
}
setOptionsCheck(sanitizedOptions);
}
}, [sanitizedOptions, optionsCheck, multi, value]);
return (
<div
id={id}
className="dash-dropdown"
style={style}
data-dash-is-loading={
(loading_state && loading_state.is_loading) || undefined
}
>
<ReactDropdown
filterOptions={filterOptions}
options={sanitizeOptions(options)}
value={value}
onChange={onChange}
onInputChange={onInputChange}
backspaceRemoves={clearable}
deleteRemoves={clearable}
inputProps={{autoComplete: 'off'}}
{...pick(RDProps, props)}
/>
</div>
);
};
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;
export default Dropdown;