forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropdowns.jsx
More file actions
169 lines (150 loc) · 3.79 KB
/
dropdowns.jsx
File metadata and controls
169 lines (150 loc) · 3.79 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
/**
* MUI React dropdowns module
* @module react/dropdowns
*/
/* jshint quotmark:false */
// jscs:disable validateQuoteMarks
'use strict';
var util = require('../js/lib/util'),
jqLite = require('../js/lib/jqLite'),
buttons = require('./buttons.jsx'),
Button = buttons.Button,
RoundButton = buttons.RoundButton;
var dropdownClass = 'mui-dropdown',
caretClass = 'mui-caret',
menuClass = 'mui-dropdown-menu',
openClass = 'mui-open',
rightClass = 'mui-dropdown-menu-right';
/**
* Dropdown constructor
* @class
*/
var Dropdown = React.createClass({
menuStyle: { top: 0 },
getInitialState: function() {
return {
opened: false
};
},
componentWillMount: function() {
document.addEventListener('click', this._outsideClick);
},
componentWillUnmount: function() {
document.removeEventListener('click', this._outsideClick);
},
render: function() {
var button;
if (this.props.round) {
button = (
<RoundButton
ref="button"
onClick={ this._click }
mini={ this.props.mini }
disabled={ this.props.disabled }
>
{ this.props.label }
<span className={ caretClass } />
</RoundButton>
);
} else {
button = (
<Button
ref="button"
onClick={ this._click }
type={ this.props.type }
flat={ this.props.flat }
raised={ this.props.raised }
large={ this.props.large }
disabled={ this.props.disabled }
>
{ this.props.label }
<span className={ caretClass } />
</Button>
);
}
var cs = {};
cs[menuClass] = true;
cs[openClass] = this.state.opened;
cs[rightClass] = this.props.right;
cs = util.classNames(cs);
return (
<div className={ dropdownClass } style={ {padding: '0px 2px 0px'} } >
{ button }
{ this.state.opened && (
<ul
className={ cs }
style={ this.menuStyle }
ref="menu"
onClick={ this._select }
>
{ this.props.children }
</ul>)
}
</div>
);
},
_click: function (ev) {
// only left clicks
if (ev.button !== 0) return;
// exit if toggle button is disabled
if (this.props.disabled) return;
setTimeout(function () {
if (!ev.defaultPrevented) this._toggle();
}.bind(this), 0);
},
_toggle: function () {
// exit if no menu element
if (!this.props.children) {
return util.raiseError('Dropdown menu element not found');
}
if (this.state.opened) this._close();
else this._open();
},
_open: function () {
// position menu element below toggle button
var wrapperRect = React.findDOMNode(this).getBoundingClientRect(),
toggleRect;
toggleRect = React.findDOMNode(this.refs.button).getBoundingClientRect();
this.menuStyle.top = toggleRect.top - wrapperRect.top + toggleRect.height;
this.setState({
opened: true
});
},
_close: function () {
this.setState({
opened: false
});
},
_select: function (ev) {
if (this.props.onClick) this.props.onClick(this, ev);
},
_outsideClick: function (ev) {
var isClickInside = React.findDOMNode(this).contains(event.target);
if (!isClickInside) {
this._close();
}
}
});
/**
* DropdownItem constructor
* @class
*/
var DropdownItem = React.createClass({
render: function () {
return (
<li>
<a href={ this.props.link || '#' } onClick={ this._click }>
{ this.props.children }
</a>
</li>
);
},
_click: function (ev) {
if (this.props.onClick) this.props.onClick(this, ev);
}
});
/** Define module API */
module.exports = {
Dropdown: Dropdown,
DropdownItem: DropdownItem
};