This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathDropdown.react.js
111 lines (104 loc) · 3.46 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
import {isNil, pluck, omit, type} from 'ramda';
import React, {Component} 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';
// 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 DELIMETER = ',';
export default class Dropdown extends Component {
constructor(props) {
super(props);
this.state = {
filterOptions: createFilterOptions({
options: props.options,
tokenizer: TOKENIZER,
}),
};
}
UNSAFE_componentWillReceiveProps(newProps) {
if (newProps.options !== this.props.options) {
this.setState({
filterOptions: createFilterOptions({
options: newProps.options,
tokenizer: TOKENIZER,
}),
});
}
}
render() {
const {
id,
clearable,
multi,
close_on_select,
options,
setProps,
style,
loading_state,
value,
} = this.props;
const {filterOptions} = this.state;
let selectedValue;
if (type(value) === 'array') {
selectedValue = value.join(DELIMETER);
} else {
selectedValue = 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={options}
value={selectedValue}
onChange={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});
}
}}
closeOnSelect={close_on_select}
onInputChange={search_value => setProps({search_value})}
backspaceRemoves={clearable}
deleteRemoves={clearable}
inputProps={{autoComplete: 'off'}}
{...omit(['setProps', 'value'], this.props)}
/>
</div>
);
}
}
Dropdown.propTypes = propTypes;
Dropdown.defaultProps = defaultProps;