-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
IconMenu.js
337 lines (311 loc) · 9.11 KB
/
IconMenu.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
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import Events from '../utils/events';
import propTypes from '../utils/propTypes';
import Menu from '../Menu/Menu';
import Popover from '../Popover/Popover';
import warning from 'warning';
class IconMenu extends Component {
static muiName = 'IconMenu';
static propTypes = {
/**
* This is the point on the icon where the menu
* `targetOrigin` will attach.
* 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,
/**
* Should be used to pass `MenuItem` components.
*/
children: PropTypes.node,
/**
* The CSS class name of the root element.
*/
className: PropTypes.string,
/**
* Sets the delay in milliseconds before closing the
* menu when an item is clicked.
* If set to 0 then the auto close functionality
* will be disabled.
*/
clickCloseDelay: PropTypes.number,
/**
* This is the `IconButton` to render. This button will open the menu.
*/
iconButtonElement: PropTypes.element.isRequired,
/**
* Override the inline-styles of the underlying icon element.
*/
iconStyle: PropTypes.object,
/**
* Override the inline-styles of the underlying `List` element.
*/
listStyle: PropTypes.object,
/**
* Override the inline-styles of the menu element.
*/
menuStyle: PropTypes.object,
/**
* If true, the value can an be array and allow the menu to be a multi-select.
*/
multiple: PropTypes.bool,
/**
* Callback function fired when the `IconButton` element is clicked.
*
* @param {object} event Click event targeting the `IconButton` element.
*/
onClick: PropTypes.func,
/**
* Callback function fired when a menu item is selected with a click.
*
* @param {object} event Click event targeting the selected menu item element.
* @param {object} child The selected element.
*/
onItemClick: PropTypes.func,
/**
* Callback function fired when the `IconButton` element is focused or blurred by the keyboard.
*
* @param {object} event `focus` or `blur` event targeting the `IconButton` element.
* @param {boolean} keyboardFocused If true, the `IconButton` element is focused.
*/
onKeyboardFocus: PropTypes.func,
/** @ignore */
onMouseDown: PropTypes.func,
/** @ignore */
onMouseEnter: PropTypes.func,
/** @ignore */
onMouseLeave: PropTypes.func,
/** @ignore */
onMouseUp: PropTypes.func,
/**
* Callback function fired when the `open` state of the menu is requested to be changed.
*
* @param {boolean} open If true, the menu was requested to be opened.
* @param {string} reason The reason for the open or close request. Possible values are
* 'keyboard' and 'iconTap' for open requests; 'enter', 'escape', 'itemTap', and 'clickAway'
* for close requests.
*/
onRequestChange: PropTypes.func,
/**
* If true, the `IconMenu` is opened.
*/
open: PropTypes.bool,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* This is the point on the menu which will stick to the menu
* origin.
* Options:
* vertical: [top, center, bottom]
* horizontal: [left, middle, right].
*/
targetOrigin: propTypes.origin,
/**
* If true, the popover will render on top of an invisible
* layer, which will prevent clicks to the underlying elements.
*/
useLayerForClickAway: PropTypes.bool,
};
static defaultProps = {
anchorOrigin: {
vertical: 'top',
horizontal: 'left',
},
animated: true,
multiple: false,
open: null,
onItemClick: () => {},
onKeyboardFocus: () => {},
onMouseDown: () => {},
onMouseLeave: () => {},
onMouseEnter: () => {},
onMouseUp: () => {},
onRequestChange: () => {},
onClick: () => {},
targetOrigin: {
vertical: 'top',
horizontal: 'left',
},
clickCloseDelay: 200,
useLayerForClickAway: false,
};
static contextTypes = {
muiTheme: PropTypes.object.isRequired,
};
state = {
menuInitiallyKeyboardFocused: false,
open: false,
};
componentWillReceiveProps(nextProps) {
if (nextProps.open != null) {
this.setState({
open: nextProps.open,
anchorEl: this.refs.iconMenuContainer,
});
}
}
componentWillUnmount() {
clearTimeout(this.timerCloseId);
}
isOpen() {
return this.state.open;
}
close(reason, isKeyboard) {
if (!this.state.open) {
return;
}
if (this.props.open !== null) {
this.props.onRequestChange(false, reason);
} else {
this.setState({open: false}, () => {
// Set focus on the icon button when the menu close
if (isKeyboard) {
const iconButton = this.refs.iconButton;
ReactDOM.findDOMNode(iconButton).focus();
iconButton.setKeyboardFocus();
}
});
}
}
open(reason, event) {
if (this.props.open !== null) {
this.props.onRequestChange(true, reason);
return this.setState({
menuInitiallyKeyboardFocused: Events.isKeyboard(event),
anchorEl: event.currentTarget,
});
}
this.setState({
open: true,
menuInitiallyKeyboardFocused: Events.isKeyboard(event),
anchorEl: event.currentTarget,
});
}
handleItemClick = (event, child) => {
if (this.props.clickCloseDelay !== 0 && !child.props.hasOwnProperty('menuItems')) {
const isKeyboard = Events.isKeyboard(event);
this.timerCloseId = setTimeout(() => {
this.close(isKeyboard ? 'enter' : 'itemTap', isKeyboard);
}, this.props.clickCloseDelay);
}
this.props.onItemClick(event, child);
};
handleRequestClose = (reason) => {
this.close(reason);
};
handleEscKeyDownMenu = (event) => {
this.close('escape', event);
};
render() {
const {
anchorOrigin,
className,
animated,
animation,
iconButtonElement,
iconStyle,
onItemClick, // eslint-disable-line no-unused-vars
onKeyboardFocus,
onMouseDown,
onMouseLeave,
onMouseEnter,
onMouseUp,
onRequestChange, // eslint-disable-line no-unused-vars
onClick,
listStyle,
menuStyle,
style,
targetOrigin,
clickCloseDelay, // eslint-disable-line no-unused-vars
useLayerForClickAway,
...other
} = this.props;
const {prepareStyles} = this.context.muiTheme;
const {open, anchorEl} = this.state;
const styles = {
root: {
display: 'inline-block',
position: 'relative',
},
menu: {
position: 'relative',
},
};
const mergedRootStyles = Object.assign(styles.root, style);
const mergedMenuStyles = Object.assign(styles.menu, menuStyle);
warning(iconButtonElement.type.muiName !== 'SvgIcon',
`Material-UI: You shoud not provide an <SvgIcon /> to the 'iconButtonElement' property of <IconMenu />.
You should wrapped it with an <IconButton />.`);
const iconButtonProps = {
onKeyboardFocus: onKeyboardFocus,
onClick: (event) => {
this.open(Events.isKeyboard(event) ? 'keyboard' : 'iconTap', event);
if (iconButtonElement.props.onClick) {
iconButtonElement.props.onClick(event);
}
},
ref: 'iconButton',
};
if (iconStyle || iconButtonElement.props.iconStyle) {
iconButtonProps.iconStyle = iconStyle ?
Object.assign({}, iconStyle, iconButtonElement.props.iconStyle) :
iconButtonElement.props.iconStyle;
}
const iconButton = React.cloneElement(iconButtonElement, iconButtonProps);
const menu = (
<Menu
{...other}
initiallyKeyboardFocused={this.state.menuInitiallyKeyboardFocused}
onEscKeyDown={this.handleEscKeyDownMenu}
onItemClick={this.handleItemClick}
style={mergedMenuStyles}
listStyle={listStyle}
>
{this.props.children}
</Menu>
);
return (
<div
ref="iconMenuContainer"
className={className}
onMouseDown={onMouseDown}
onMouseLeave={onMouseLeave}
onMouseEnter={onMouseEnter}
onMouseUp={onMouseUp}
onClick={onClick}
style={prepareStyles(mergedRootStyles)}
>
{iconButton}
<Popover
anchorOrigin={anchorOrigin}
targetOrigin={targetOrigin}
open={open}
anchorEl={anchorEl}
childContextTypes={this.constructor.childContextTypes}
useLayerForClickAway={useLayerForClickAway}
onRequestClose={this.handleRequestClose}
animated={animated}
animation={animation}
context={this.context}
>
{menu}
</Popover>
</div>
);
}
}
export default IconMenu;