-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
/
drop-down-menu.jsx
78 lines (63 loc) · 2.1 KB
/
drop-down-menu.jsx
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
/**
* @jsx React.DOM
*/
var $ = require('jquery'),
React = require('react'),
Classable = require('./mixins/classable.js'),
ClickAwayable = require('./mixins/click-awayable'),
KeyLine = require('./utils/key-line.js'),
Paper = require('./paper.jsx'),
Icon = require('./icon.jsx'),
Menu = require('./menu.jsx');
var DropDownMenu = React.createClass({
mixins: [Classable, ClickAwayable],
propTypes: {
onChange: React.PropTypes.func,
menuItems: React.PropTypes.array.isRequired
},
getInitialState: function() {
return {
open: false,
selectedIndex: this.props.selectedIndex || 0
}
},
componentClickAway: function() {
this.setState({ open: false });
},
componentDidMount: function() {
var $el = $(this.getDOMNode()),
$menuItems = $(this.refs.menuItems.getDOMNode());
$el.css('width', $menuItems.width());
},
componentWillReceiveProps: function(nextProps) {
if (nextProps.hasOwnProperty('selectedIndex')) this.setState({selectedIndex: nextProps.selectedIndex});
},
render: function() {
var classes = this.getClasses('mui-drop-down-menu', {
'mui-open': this.state.open
});
return (
<div className={classes}>
<div className="mui-menu-control" onClick={this._onControlClick}>
<Paper className="mui-menu-control-bg"zDepth={0} />
<div className="mui-menu-label">
{this.props.menuItems[this.state.selectedIndex].text}
</div>
<Icon className="mui-menu-drop-down-icon" icon="navigation-arrow-drop-down" />
</div>
<Menu ref="menuItems" selectedIndex={this.state.selectedIndex} menuItems={this.props.menuItems} hideable={true} visible={this.state.open} onItemClick={this._onMenuItemClick} />
</div>
);
},
_onControlClick: function(e) {
this.setState({ open: !this.state.open });
},
_onMenuItemClick: function(e, key, payload) {
if (this.props.onChange && this.state.selectedIndex !== key) this.props.onChange(e, key, payload);
this.setState({
selectedIndex: key,
open: false
});
}
});
module.exports = DropDownMenu;