-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
Copy pathSelectInput.js
386 lines (351 loc) · 9.56 KB
/
SelectInput.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
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
// @flow
import React from 'react';
import type { Element, Node } from 'react';
import classNames from 'classnames';
import keycode from 'keycode';
import warning from 'warning';
import Menu from '../Menu/Menu';
import { isDirty } from '../Input/Input';
import ArrowDropDownIcon from '../svg-icons/ArrowDropDown';
type ProvidedProps = {
classes: Object,
};
export type Props = {
/**
* If true, the width of the popover will automatically be set according to the items inside the
* menu, otherwise it will be at least the width of the select input.
*/
autoWidth: boolean,
/**
* The option elements to populate the select with.
* Can be some `MenuItem` when `native` is false and `option` when `native` is true.
*/
children: Node,
/**
* Useful to extend the style applied to components.
*/
classes?: Object,
/**
* The CSS class name of the select element.
*/
className?: string,
/**
* If `true`, the select will be disabled.
*/
disabled?: boolean,
/**
* If `true`, the selected item is displayed even if its value is empty.
* You can only use it when the `native` property is `false` (default).
*/
displayEmpty: boolean,
/**
* If `true`, the component will be using a native `select` element.
*/
native: boolean,
/**
* If true, `value` must be an array and the menu will support multiple selections.
* You can only use it when the `native` property is `false` (default).
*/
multiple: boolean,
/**
* Properties applied to the `Menu` element.
*/
MenuProps?: Object,
/**
* Name attribute of the `select` or hidden `input` element.
*/
name?: string,
/**
* @ignore
*/
onBlur?: Function,
/**
* Callback function fired when a menu item is selected.
*
* @param {object} event The event source of the callback
* @param {object} child The react element that was selected
*/
onChange?: (event: SyntheticUIEvent<*>, child: Element<any>) => void,
/**
* @ignore
*/
onFocus?: Function,
/**
* @ignore
*/
readOnly?: boolean,
/**
* Render the selected value.
* You can only use it when the `native` property is `false` (default).
*/
renderValue?: Function,
/**
* Use that property to pass a ref callback to the native select element.
*/
selectRef?: Function,
/**
* The value of the component, required for a controlled component.
*/
value?: string | number | $ReadOnlyArray<string | number>,
};
type State = {
open: boolean,
anchorEl: ?HTMLElement,
};
/**
* @ignore - internal component.
*/
class SelectInput extends React.Component<ProvidedProps & Props, State> {
static muiName = 'SelectInput';
state = {
anchorEl: null,
open: false,
};
ignoreNextBlur = false;
handleClick = (event: SyntheticMouseEvent<HTMLElement>) => {
// Opening the menu is going to blur the. It will be focused back when closed.
this.ignoreNextBlur = true;
this.setState({
open: true,
anchorEl: event.currentTarget,
});
};
handleRequestClose = () => {
this.setState({
open: false,
});
};
handleItemClick = (child: Element<any>) => (event: SyntheticMouseEvent<> & { target?: any }) => {
if (!this.props.multiple) {
this.setState({
open: false,
});
}
if (this.props.onChange) {
const { onChange } = this.props;
let value;
let target;
if (event.target) {
target = event.target;
}
if (this.props.multiple) {
value = Array.isArray(this.props.value) ? [...this.props.value] : [];
const itemIndex = value.indexOf(child.props.value);
if (itemIndex === -1) {
value.push(child.props.value);
} else {
value.splice(itemIndex, 1);
}
} else {
value = child.props.value;
}
event.persist();
event.target = { ...target, value };
onChange(event, child);
}
};
handleBlur = (event: SyntheticFocusEvent<>) => {
if (this.ignoreNextBlur === true) {
// The parent components are relying on the bubbling of the event.
event.stopPropagation();
this.ignoreNextBlur = false;
return;
}
if (this.props.onBlur) {
this.props.onBlur(event);
}
};
handleKeyDown = (event: SyntheticKeyboardEvent<HTMLElement>) => {
if (this.props.readOnly) {
return;
}
if (['space', 'up', 'down'].includes(keycode(event))) {
event.preventDefault();
// Opening the menu is going to blur the. It will be focused back when closed.
this.ignoreNextBlur = true;
this.setState({
open: true,
anchorEl: event.currentTarget,
});
}
};
handleSelectRef = (node: ?HTMLElement) => {
if (!this.props.selectRef) {
return;
}
this.props.selectRef({
node,
// By pass the native input as we expose a rich object (array).
value: this.props.value,
});
};
render() {
const {
autoWidth,
children,
className: classNameProp,
classes,
disabled,
displayEmpty,
name,
native,
multiple,
MenuProps = {},
onBlur,
onChange,
onFocus,
readOnly,
renderValue,
selectRef,
value,
...other
} = this.props;
if (native) {
warning(
multiple === false,
'Material-UI: you can not use the `native` and `multiple` properties ' +
'at the same time on a `Select` component.',
);
warning(
!renderValue,
'Material-UI: the `renderValue` property is not used by the native implementation.',
);
warning(
!displayEmpty,
'Material-UI: the `displayEmpty` property is not used by the native implementation.',
);
return (
<div className={classes.root}>
<select
className={classNames(
classes.select,
{
[classes.disabled]: disabled,
},
classNameProp,
)}
name={name}
disabled={disabled}
onBlur={onBlur}
onChange={onChange}
onFocus={onFocus}
value={value}
readOnly={readOnly}
{...other}
ref={selectRef}
>
{children}
</select>
<ArrowDropDownIcon className={classes.icon} />
</div>
);
}
if (value === undefined) {
throw new Error(
'Material-UI: the `value` property is required ' +
'when using the `Select` component with `native=false`.',
);
}
let display;
let displaySingle = '';
const displayMultiple = [];
let computeDisplay = false;
// No need to display any value if the field is empty.
if (isDirty(this.props) || displayEmpty) {
if (renderValue) {
display = renderValue(value);
} else {
computeDisplay = true;
}
}
const items = React.Children.map(children, child => {
if (!React.isValidElement(child)) {
return null;
}
let selected;
if (multiple) {
if (!Array.isArray(value)) {
throw new Error(
'Material-UI: the `value` property must be an array ' +
'when using the `Select` component with `multiple`.',
);
}
selected = value.indexOf(child.props.value) !== -1;
if (selected && computeDisplay) {
displayMultiple.push(child.props.children);
}
} else {
selected = value === child.props.value;
if (selected && computeDisplay) {
displaySingle = child.props.children;
}
}
return React.cloneElement(child, {
role: 'option',
selected,
onClick: this.handleItemClick(child),
});
});
if (computeDisplay) {
display = multiple ? displayMultiple.join(', ') : displaySingle;
}
const minimumMenuWidth =
this.state.anchorEl != null && !autoWidth ? this.state.anchorEl.clientWidth : undefined;
return (
<div className={classes.root}>
<div
className={classNames(
classes.select,
classes.selectMenu,
{
[classes.disabled]: disabled,
},
classNameProp,
)}
data-mui-test="SelectDisplay"
aria-pressed={this.state.open ? 'true' : 'false'}
tabIndex={disabled ? null : 0}
role="button"
aria-owns={this.state.open ? `menu-${name || ''}` : null}
aria-haspopup="true"
onKeyDown={this.handleKeyDown}
onBlur={this.handleBlur}
onClick={disabled || readOnly ? null : this.handleClick}
onFocus={onFocus}
>
{display}
</div>
<input
value={Array.isArray(value) ? value.join(',') : value}
name={name}
readOnly={readOnly}
{...other}
ref={this.handleSelectRef}
type="hidden"
/>
<ArrowDropDownIcon className={classes.icon} />
<Menu
id={`menu-${name || ''}`}
anchorEl={this.state.anchorEl}
open={this.state.open}
onRequestClose={this.handleRequestClose}
{...MenuProps}
MenuListProps={{
...MenuProps.MenuListProps,
role: 'listbox',
}}
PaperProps={{
...MenuProps.PaperProps,
style: {
minWidth: minimumMenuWidth,
...(MenuProps.PaperProps != null ? MenuProps.PaperProps.style : null),
},
}}
>
{items}
</Menu>
</div>
);
}
}
export default SelectInput;