Repository navigation
Expand file tree
/
Copy pathDropDownMenu.js
More file actions
327 lines (304 loc) · 8.54 KB
/
Copy pathDropDownMenu.js
File metadata and controls
327 lines (304 loc) · 8.54 KB
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
import React, {Component, PropTypes} from 'react';
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';
const anchorOrigin = {
vertical: 'top',
horizontal: 'left',
};
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.desktopToolbarHeight - 24) / 2),
},
label: {
color: disabled ? palette.disabledColor : palette.textColor,
lineHeight: `${spacing.desktopToolbarHeight}px`,
opacity: 1,
position: 'relative',
paddingLeft: spacing.desktopGutter,
paddingRight: spacing.iconSize +
spacing.desktopGutterLess +
spacing.desktopGutterMini,
top: 0,
},
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 = {
/**
* 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 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,
/**
* Overrides the styles of `Menu` when the `DropDownMenu` is displayed.
*/
menuStyle: PropTypes.object,
/**
* Callback function fired when a menu item is clicked, other than the one currently selected.
*
* @param {object} event TouchTap event targeting the menu item that was clicked.
* @param {number} key The index of the clicked menu item in the `children` collection.
* @param {any} payload The `value` prop of the clicked menu item.
*/
onChange: PropTypes.func,
/**
* Set to true to have the `DropDownMenu` automatically open on mount.
*/
openImmediately: PropTypes.bool,
/**
* Override the inline-styles of the root element.
*/
style: PropTypes.object,
/**
* Overrides the inline-styles of the underline.
*/
underlineStyle: PropTypes.object,
/**
* The value that is currently selected.
*/
value: PropTypes.any,
};
static defaultProps = {
animated: true,
autoWidth: true,
disabled: false,
openImmediately: false,
maxHeight: 500,
};
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.refs.root}));
setTimeout(() => this.setState({
open: true,
anchorEl: this.refs.root,
}), 0);
/* eslint-enable react/no-did-mount-set-state */
}
}
componentWillReceiveProps() {
if (this.props.autoWidth) {
this.setWidth();
}
}
/**
* This method is deprecated but still here because the TextField
* need it in order to work. TODO: That will be addressed later.
*/
getInputNode() {
const root = this.refs.root;
root.focus = () => {
if (!this.props.disabled) {
this.setState({
open: !this.state.open,
anchorEl: this.refs.root,
});
}
};
return root;
}
setWidth() {
const el = this.refs.root;
if (!this.props.style || !this.props.style.hasOwnProperty('width')) {
el.style.width = 'auto';
}
}
handleTouchTapControl = (event) => {
event.preventDefault();
if (!this.props.disabled) {
this.setState({
open: !this.state.open,
anchorEl: this.refs.root,
});
}
};
handleRequestCloseMenu = () => {
this.setState({
open: false,
anchorEl: null,
});
};
handleItemTouchTap = (event, child, index) => {
event.persist();
this.setState({
open: false,
}, () => {
if (this.props.onChange) {
this.props.onChange(event, index, child.props.value);
}
});
};
render() {
const {
animated,
animation,
autoWidth,
children,
className,
iconStyle,
labelStyle,
listStyle,
maxHeight,
menuStyle: menuStyleProp,
openImmediately, // eslint-disable-line no-unused-vars
style,
underlineStyle,
value,
...other,
} = this.props;
const {
anchorEl,
open,
} = this.state;
const {prepareStyles} = this.context.muiTheme;
const styles = getStyles(this.props, this.context);
let displayValue = '';
React.Children.forEach(children, (child) => {
if (value === child.props.value) {
// This will need to be improved (in case primaryText is a node)
displayValue = child.props.label || child.props.primaryText;
}
});
let menuStyle;
if (anchorEl && !autoWidth) {
menuStyle = Object.assign({
width: anchorEl.clientWidth,
}, menuStyleProp);
} else {
menuStyle = menuStyleProp;
}
return (
<div
{...other}
ref="root"
className={className}
style={prepareStyles(Object.assign({}, styles.root, open && styles.rootWhenOpen, style))}
>
<ClearFix style={styles.control} onTouchTap={this.handleTouchTapControl}>
<div
style={prepareStyles(Object.assign({}, styles.label, open && styles.labelWhenOpen, labelStyle))}
>
{displayValue}
</div>
<DropDownArrow style={Object.assign({}, styles.icon, iconStyle)} />
<div style={prepareStyles(Object.assign({}, styles.underline, underlineStyle))} />
</ClearFix>
<Popover
anchorOrigin={anchorOrigin}
anchorEl={anchorEl}
animation={animation || PopoverAnimationVertical}
open={open}
animated={animated}
onRequestClose={this.handleRequestCloseMenu}
>
<Menu
maxHeight={maxHeight}
desktop={true}
value={value}
style={menuStyle}
listStyle={listStyle}
onItemTouchTap={this.handleItemTouchTap}
>
{children}
</Menu>
</Popover>
</div>
);
}
}
export default DropDownMenu;