/
IntegrationReactSelect.js
295 lines (276 loc) · 7.26 KB
/
IntegrationReactSelect.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
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
/* eslint-disable react/prop-types */
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Typography from 'material-ui/Typography';
import Input from 'material-ui/Input';
import { MenuItem } from 'material-ui/Menu';
import ArrowDropDownIcon from 'material-ui-icons/ArrowDropDown';
import CancelIcon from 'material-ui-icons/Cancel';
import ArrowDropUpIcon from 'material-ui-icons/ArrowDropUp';
import ClearIcon from 'material-ui-icons/Clear';
import Chip from 'material-ui/Chip';
import Select from 'react-select';
import 'react-select/dist/react-select.css';
const suggestions = [
{ label: 'Afghanistan' },
{ label: 'Aland Islands' },
{ label: 'Albania' },
{ label: 'Algeria' },
{ label: 'American Samoa' },
{ label: 'Andorra' },
{ label: 'Angola' },
{ label: 'Anguilla' },
{ label: 'Antarctica' },
{ label: 'Antigua and Barbuda' },
{ label: 'Argentina' },
{ label: 'Armenia' },
{ label: 'Aruba' },
{ label: 'Australia' },
{ label: 'Austria' },
{ label: 'Azerbaijan' },
{ label: 'Bahamas' },
{ label: 'Bahrain' },
{ label: 'Bangladesh' },
{ label: 'Barbados' },
{ label: 'Belarus' },
{ label: 'Belgium' },
{ label: 'Belize' },
{ label: 'Benin' },
{ label: 'Bermuda' },
{ label: 'Bhutan' },
{ label: 'Bolivia, Plurinational State of' },
{ label: 'Bonaire, Sint Eustatius and Saba' },
{ label: 'Bosnia and Herzegovina' },
{ label: 'Botswana' },
{ label: 'Bouvet Island' },
{ label: 'Brazil' },
{ label: 'British Indian Ocean Territory' },
{ label: 'Brunei Darussalam' },
].map(suggestion => ({
value: suggestion.label,
label: suggestion.label,
}));
class Option extends React.Component {
handleClick = event => {
this.props.onSelect(this.props.option, event);
};
render() {
const { children, isFocused, isSelected, onFocus } = this.props;
return (
<MenuItem
onFocus={onFocus}
selected={isFocused}
onClick={this.handleClick}
component="div"
style={{
fontWeight: isSelected ? 500 : 400,
}}
>
{children}
</MenuItem>
);
}
}
function SelectWrapped(props) {
const { classes, ...other } = props;
return (
<Select
optionComponent={Option}
noResultsText={<Typography>{'No results found'}</Typography>}
arrowRenderer={arrowProps => {
return arrowProps.isOpen ? <ArrowDropUpIcon /> : <ArrowDropDownIcon />;
}}
clearRenderer={() => <ClearIcon />}
valueComponent={valueProps => {
const { value, children, onRemove } = valueProps;
const onDelete = event => {
event.preventDefault();
event.stopPropagation();
onRemove(value);
};
if (onRemove) {
return (
<Chip
tabIndex={-1}
label={children}
className={classes.chip}
deleteIcon={<CancelIcon onTouchEnd={onDelete} />}
onDelete={onDelete}
/>
);
}
return <div className="Select-value">{children}</div>;
}}
{...other}
/>
);
}
const ITEM_HEIGHT = 48;
const styles = theme => ({
root: {
flexGrow: 1,
height: 200,
width: 200,
},
chip: {
margin: theme.spacing.unit / 4,
},
// We had to use a lot of global selectors in order to style react-select.
// We are waiting on https://github.com/JedWatson/react-select/issues/1679
// to provide a better implementation.
// Also, we had to reset the default style injected by the library.
'@global': {
'.Select-control': {
display: 'flex',
alignItems: 'center',
border: 0,
height: 'auto',
background: 'transparent',
'&:hover': {
boxShadow: 'none',
},
},
'.Select-multi-value-wrapper': {
flexGrow: 1,
display: 'flex',
flexWrap: 'wrap',
},
'.Select--multi .Select-input': {
margin: 0,
},
'.Select.has-value.is-clearable.Select--single > .Select-control .Select-value': {
padding: 0,
},
'.Select-noresults': {
padding: theme.spacing.unit * 2,
},
'.Select-input': {
display: 'inline-flex !important',
padding: 0,
height: 'auto',
},
'.Select-input input': {
background: 'transparent',
border: 0,
padding: 0,
cursor: 'default',
display: 'inline-block',
fontFamily: 'inherit',
fontSize: 'inherit',
margin: 0,
outline: 0,
},
'.Select-placeholder, .Select--single .Select-value': {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
display: 'flex',
alignItems: 'center',
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.pxToRem(16),
padding: 0,
},
'.Select-placeholder': {
opacity: 0.42,
color: theme.palette.common.black,
},
'.Select-menu-outer': {
backgroundColor: theme.palette.background.paper,
boxShadow: theme.shadows[2],
position: 'absolute',
left: 0,
top: `calc(100% + ${theme.spacing.unit}px)`,
width: '100%',
zIndex: 2,
maxHeight: ITEM_HEIGHT * 4.5,
},
'.Select.is-focused:not(.is-open) > .Select-control': {
boxShadow: 'none',
},
'.Select-menu': {
maxHeight: ITEM_HEIGHT * 4.5,
overflowY: 'auto',
},
'.Select-menu div': {
boxSizing: 'content-box',
},
'.Select-arrow-zone, .Select-clear-zone': {
color: theme.palette.action.active,
cursor: 'pointer',
height: 21,
width: 21,
zIndex: 1,
},
// Only for screen readers. We can't use display none.
'.Select-aria-only': {
position: 'absolute',
overflow: 'hidden',
clip: 'rect(0 0 0 0)',
height: 1,
width: 1,
margin: -1,
},
},
});
class IntegrationReactSelect extends React.Component {
state = {
single: null,
multi: null,
};
handleChangeSingle = single => {
this.setState({
single,
});
};
handleChangeMulti = multi => {
this.setState({
multi,
});
};
render() {
const { classes } = this.props;
const { single, multi } = this.state;
return (
<div className={classes.root}>
<Input
fullWidth
inputComponent={SelectWrapped}
inputProps={{
classes,
value: single,
onChange: this.handleChangeSingle,
placeholder: 'Select single-value…',
instanceId: 'react-select-single',
id: 'react-select-single',
name: 'react-select-single',
simpleValue: true,
options: suggestions,
}}
/>
<Input
fullWidth
inputComponent={SelectWrapped}
inputProps={{
classes,
value: multi,
multi: true,
onChange: this.handleChangeMulti,
placeholder: 'Select multi-value…',
instanceId: 'react-select-chip',
id: 'react-select-chip',
name: 'react-select-chip',
simpleValue: true,
options: suggestions,
}}
/>
</div>
);
}
}
IntegrationReactSelect.propTypes = {
classes: PropTypes.object.isRequired,
};
export default withStyles(styles)(IntegrationReactSelect);