-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
/
DropDownMenu.js
494 lines (464 loc) · 13.5 KB
/
DropDownMenu.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
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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import transitions from '../styles/transitions';
import DropDownArrow from '../svg-icons/navigation/arrow-drop-down';
import Menu from '../Menu/Menu';
import ClearFix from '../internal/ClearFix';
import Popover from '../Popover/Popover';
import PopoverAnimationVertical from '../Popover/PopoverAnimationVertical';
import keycode from 'keycode';
import Events from '../utils/events';
import IconButton from '../IconButton';
import propTypes from '../utils/propTypes';
function getStyles(props, context) {
const {disabled} = props;
const spacing = context.muiTheme.baseTheme.spacing;
const palette = context.muiTheme.baseTheme.palette;
const accentColor = context.muiTheme.dropDownMenu.accentColor;
return {
control: {
cursor: disabled ? 'not-allowed' : 'pointer',
height: '100%',
position: 'relative',
width: '100%',
},
icon: {
fill: accentColor,
position: 'absolute',
right: spacing.desktopGutterLess,
top: (spacing.iconSize - 24) / 2 + spacing.desktopGutterMini / 2,
},
iconChildren: {
fill: 'inherit',
},
label: {
color: disabled ? palette.disabledColor : palette.textColor,
height: `${spacing.desktopToolbarHeight}px`,
lineHeight: `${spacing.desktopToolbarHeight}px`,
overflow: 'hidden',
opacity: 1,
position: 'relative',
paddingLeft: spacing.desktopGutter,
paddingRight: spacing.iconSize * 2 + spacing.desktopGutterMini,
textOverflow: 'ellipsis',
top: 0,
whiteSpace: 'nowrap',
},
labelWhenOpen: {
opacity: 0,
top: (spacing.desktopToolbarHeight / 8),
},
root: {
display: 'inline-block',
fontSize: spacing.desktopDropDownMenuFontSize,
height: spacing.desktopSubheaderHeight,
fontFamily: context.muiTheme.baseTheme.fontFamily,
outline: 'none',
position: 'relative',
transition: transitions.easeOut(),
},
rootWhenOpen: {
opacity: 1,
},
underline: {
borderTop: `solid 1px ${accentColor}`,
bottom: 1,
left: 0,
margin: `-1px ${spacing.desktopGutter}px`,
right: 0,
position: 'absolute',
},
};
}
class DropDownMenu extends Component {
static muiName = 'DropDownMenu';
// The nested styles for drop-down-menu are modified by toolbar and possibly
// other user components, so it will give full access to its js styles rather
// than just the parent.
static propTypes = {
/**
* This is the point on the anchor that the popover's
* `targetOrigin` will attach to.
* Options:
* vertical: [top, center, bottom]
* horizontal: [left, middle, right].
*/
anchorOrigin: propTypes.origin,
/**
* If true, the popover will apply transitions when
* it gets added to the DOM.
*/
animated: PropTypes.bool,
/**
* Override the default animation component used.
*/
animation: PropTypes.func,
/**
* The width will automatically be set according to the items inside the menu.
* To control this width in css instead, set this prop to `false`.
*/
autoWidth: PropTypes.bool,
/**
* The `MenuItem`s to populate the `Menu` with. If the `MenuItems` have the
* prop `label` that value will be used to render the representation of that
* item within the field.
*/
children: PropTypes.node,
/**
* The css class name of the root element.
*/
className: PropTypes.string,
/**
* Disables the menu.
*/
disabled: PropTypes.bool,
/**
* Overrides default `SvgIcon` dropdown arrow component.
*/
iconButton: PropTypes.node,
/**
* Overrides the styles of icon element.
*/
iconStyle: PropTypes.object,
/**
* Overrides the styles of label when the `DropDownMenu` is inactive.
*/
labelStyle: PropTypes.object,
/**
* The style object to use to override underlying list style.
*/
listStyle: PropTypes.object,
/**
* The maximum height of the `Menu` when it is displayed.
*/
maxHeight: PropTypes.number,
/**
* Override the inline-styles of menu items.
*/
menuItemStyle: PropTypes.object,
/**
* Overrides the styles of `Menu` when the `DropDownMenu` is displayed.
*/
menuStyle: PropTypes.object,
/**
* If true, `value` must be an array and the menu will support
* multiple selections.
*/
multiple: PropTypes.bool,
/**
* Callback function fired when a menu item is clicked, other than the one currently selected.
*
* @param {object} event Click event targeting the menu item that was clicked.
* @param {number} key The index of the clicked menu item in the `children` collection.
* @param {any} value If `multiple` is true, the menu's `value`
* array with either the menu item's `value` added (if
* it wasn't already selected) or omitted (if it was already selected).
* Otherwise, the `value` of the menu item.
*/
onChange: PropTypes.func,
/**
* Callback function fired when the menu is closed.
*/
onClose: PropTypes.func,
/**
* Set to true to have the `DropDownMenu` automatically open on mount.
*/
openImmediately: PropTypes.bool,
/**
* Override the inline-styles of selected menu items.
*/
selectedMenuItemStyle: PropTypes.object,
/**
* Callback function fired when a menu item is clicked, other than the one currently selected.
*
* @param {any} value If `multiple` is true, the menu's `value`
* array with either the menu item's `value` added (if
* it wasn't already selected) or omitted (if it was already selected).
* Otherwise, the `value` of the menu item.
* @param {any} menuItem The selected `MenuItem`.
* If `multiple` is true, this will be an array with the `MenuItem`s matching the `value`s parameter.
*/
selectionRenderer: PropTypes.func,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* This is the point on the popover which will attach to
* the anchor's origin.
* Options:
* vertical: [top, center, bottom]
* horizontal: [left, middle, right].
*/
targetOrigin: propTypes.origin,
/**
* Overrides the inline-styles of the underline.
*/
underlineStyle: PropTypes.object,
/**
* If `multiple` is true, an array of the `value`s of the selected
* menu items. Otherwise, the `value` of the selected menu item.
* If provided, the menu will be a controlled component.
*/
value: PropTypes.any,
};
static defaultProps = {
animated: true,
autoWidth: true,
disabled: false,
iconButton: <DropDownArrow />,
openImmediately: false,
maxHeight: 500,
multiple: false,
anchorOrigin: {
vertical: 'top',
horizontal: 'left',
},
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
open: false,
};
componentDidMount() {
if (this.props.autoWidth) {
this.setWidth();
}
if (this.props.openImmediately) {
// TODO: Temporary fix to make openImmediately work with popover.
/* eslint-disable react/no-did-mount-set-state */
setTimeout(() => this.setState({
open: true,
anchorEl: this.rootNode,
}), 0);
/* eslint-enable react/no-did-mount-set-state */
}
}
componentWillReceiveProps() {
if (this.props.autoWidth) {
this.setWidth();
}
}
rootNode = undefined;
arrowNode = undefined;
/**
* This method is deprecated but still here because the TextField
* need it in order to work. TODO: That will be addressed later.
*/
getInputNode() {
const rootNode = this.rootNode;
rootNode.focus = () => {
if (!this.props.disabled) {
this.setState({
open: !this.state.open,
anchorEl: this.rootNode,
});
}
};
return rootNode;
}
setWidth() {
const el = this.rootNode;
if (!this.props.style || !this.props.style.hasOwnProperty('width')) {
el.style.width = 'auto';
}
}
handleClickControl = (event) => {
event.preventDefault();
if (!this.props.disabled) {
this.setState({
open: !this.state.open,
anchorEl: this.rootNode,
});
}
};
handleRequestCloseMenu = () => {
this.close(false);
};
handleEscKeyDownMenu = () => {
this.close(true);
};
handleKeyDown = (event) => {
switch (keycode(event)) {
case 'up':
case 'down':
case 'space':
case 'enter':
event.preventDefault();
this.setState({
open: true,
anchorEl: this.rootNode,
});
break;
}
};
handleItemClick = (event, child, index) => {
if (this.props.multiple) {
if (!this.state.open) {
this.setState({open: true});
}
} else {
event.persist();
this.setState({
open: false,
}, () => {
if (this.props.onChange) {
this.props.onChange(event, index, child.props.value);
}
this.close(Events.isKeyboard(event));
});
}
};
handleChange = (event, value) => {
if (this.props.multiple && this.props.onChange) {
this.props.onChange(event, undefined, value);
}
};
close = (isKeyboard) => {
this.setState({
open: false,
}, () => {
if (this.props.onClose) {
this.props.onClose();
}
if (isKeyboard) {
const dropArrow = this.arrowNode;
const dropNode = ReactDOM.findDOMNode(dropArrow);
dropNode.focus();
dropArrow.setKeyboardFocus(true);
}
});
}
render() {
const {
animated,
animation,
autoWidth,
multiple,
children,
className,
disabled,
iconStyle,
labelStyle,
listStyle,
maxHeight,
menuStyle: menuStyleProp,
selectionRenderer,
onClose, // eslint-disable-line no-unused-vars
openImmediately, // eslint-disable-line no-unused-vars
menuItemStyle,
selectedMenuItemStyle,
style,
underlineStyle,
value,
iconButton,
anchorOrigin,
targetOrigin,
...other
} = this.props;
const {
anchorEl,
open,
} = this.state;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
let displayValue = '';
if (!multiple) {
React.Children.forEach(children, (child) => {
if (child && value === child.props.value) {
if (selectionRenderer) {
displayValue = selectionRenderer(value, child);
} else {
// This will need to be improved (in case primaryText is a node)
displayValue = child.props.label || child.props.primaryText;
}
}
});
} else {
const values = [];
const selectionRendererChildren = [];
React.Children.forEach(children, (child) => {
if (child && value && value.indexOf(child.props.value) > -1) {
if (selectionRenderer) {
values.push(child.props.value);
selectionRendererChildren.push(child);
} else {
values.push(child.props.label || child.props.primaryText);
}
}
});
displayValue = [];
if (selectionRenderer) {
displayValue = selectionRenderer(values, selectionRendererChildren);
} else {
displayValue = values.join(', ');
}
}
let menuStyle;
if (anchorEl && !autoWidth) {
menuStyle = Object.assign({
width: anchorEl.clientWidth,
}, menuStyleProp);
} else {
menuStyle = menuStyleProp;
}
return (
<div
{...other}
ref={(node) => {
this.rootNode = node;
}}
className={className}
style={prepareStyles(Object.assign({}, styles.root, open && styles.rootWhenOpen, style))}
>
<ClearFix style={styles.control} onClick={this.handleClickControl}>
<div style={prepareStyles(Object.assign({}, styles.label, open && styles.labelWhenOpen, labelStyle))}>
{displayValue}
</div>
<IconButton
disabled={disabled}
onKeyDown={this.handleKeyDown}
ref={(node) => {
this.arrowNode = node;
}}
style={Object.assign({}, styles.icon, iconStyle)}
iconStyle={styles.iconChildren}
>
{iconButton}
</IconButton>
<div style={prepareStyles(Object.assign({}, styles.underline, underlineStyle))} />
</ClearFix>
<Popover
anchorOrigin={anchorOrigin}
targetOrigin={targetOrigin}
anchorEl={anchorEl}
animation={animation || PopoverAnimationVertical}
open={open}
animated={animated}
onRequestClose={this.handleRequestCloseMenu}
>
<Menu
multiple={multiple}
maxHeight={maxHeight}
desktop={true}
value={value}
onEscKeyDown={this.handleEscKeyDownMenu}
style={menuStyle}
listStyle={listStyle}
onItemClick={this.handleItemClick}
onChange={this.handleChange}
menuItemStyle={menuItemStyle}
selectedMenuItemStyle={selectedMenuItemStyle}
autoWidth={autoWidth}
width={!autoWidth && menuStyle ? menuStyle.width : null}
>
{children}
</Menu>
</Popover>
</div>
);
}
}
export default DropDownMenu;