forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdropdown.jsx
More file actions
203 lines (167 loc) · 4.28 KB
/
dropdown.jsx
File metadata and controls
203 lines (167 loc) · 4.28 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
/**
* MUI React dropdowns module
* @module react/dropdowns
*/
/* jshint quotmark:false */
// jscs:disable validateQuoteMarks
'use strict';
import React from 'react';
import Button from './button';
import Caret from './caret';
import * as jqLite from '../js/lib/jqLite';
import * as util from '../js/lib/util';
const PropTypes = React.PropTypes,
dropdownClass = 'mui-dropdown',
menuClass = 'mui-dropdown__menu',
openClass = 'mui--is-open',
rightClass = 'mui-dropdown__menu--right';
/**
* Dropdown constructor
* @class
*/
class Dropdown extends React.Component {
constructor(props) {
super(props);
this.state = {
opened: false,
menuTop: 0
}
let cb = util.callback;
this.selectCB = cb(this, 'select');
this.onClickCB = cb(this, 'onClick');
this.onOutsideClickCB = cb(this, 'onOutsideClick');
}
static propTypes = {
color: PropTypes.oneOf(['default', 'primary', 'danger', 'dark',
'accent']),
variant: PropTypes.oneOf(['default', 'flat', 'raised', 'fab']),
size: PropTypes.oneOf(['default', 'small', 'large']),
label: PropTypes.oneOfType([
PropTypes.string,
PropTypes.element
]),
alignMenu: PropTypes.oneOf(['left', 'right']),
onClick: PropTypes.func,
onSelect: PropTypes.func,
disabled: PropTypes.bool
};
static defaultProps = {
className: '',
color: 'default',
variant: 'default',
size: 'default',
label: '',
alignMenu: 'left',
onClick: null,
onSelect: null,
disabled: false
};
componentDidMount() {
document.addEventListener('click', this.onOutsideClickCB);
}
componentWillUnmount() {
document.removeEventListener('click', this.onOutsideClickCB);
}
onClick(ev) {
// only left clicks
if (ev.button !== 0) return;
// exit if toggle button is disabled
if (this.props.disabled) return;
if (!ev.defaultPrevented) {
this.toggle();
// execute <Dropdown> onClick method
let onClickFn = this.props.onClick;
onClickFn && onClickFn(ev);
}
}
toggle() {
// 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() {
// position menu element below toggle button
let wrapperRect = this.refs.wrapperEl.getBoundingClientRect(),
toggleRect;
toggleRect = this.refs.button.refs.buttonEl.getBoundingClientRect();
this.setState({
opened: true,
menuTop: toggleRect.top - wrapperRect.top + toggleRect.height
});
}
close() {
this.setState({opened: false});
}
select(ev) {
// onSelect callback
if (this.props.onSelect && ev.target.tagName === 'A') {
this.props.onSelect(ev.target.getAttribute('data-mui-value'));
}
// close menu
if (!ev.defaultPrevented) this.close();
}
onOutsideClick(ev) {
let isClickInside = this.refs.wrapperEl.contains(ev.target);
if (!isClickInside) this.close();
}
render() {
let buttonEl,
menuEl,
labelEl;
const { children, className, color, variant, size, label, alignMenu,
onClick, onSelect, disabled, ...reactProps } = this.props;
// build label
if (jqLite.type(label) === 'string') {
labelEl = <span>{label} <Caret /></span>;
} else {
labelEl = label;
}
buttonEl = (
<Button
ref="button"
type="button"
onClick={this.onClickCB}
color={color}
variant={variant}
size={size}
disabled={disabled}
>
{labelEl}
</Button>
);
if (this.state.opened) {
let cs = {};
cs[menuClass] = true;
cs[openClass] = this.state.opened;
cs[rightClass] = (alignMenu === 'right');
cs = util.classNames(cs);
menuEl = (
<ul
ref="menuEl"
className={cs}
style={{top: this.state.menuTop}}
onClick={this.selectCB}
>
{children}
</ul>
);
} else {
menuEl = <div></div>;
}
return (
<div
{ ...reactProps }
ref="wrapperEl"
className={dropdownClass + ' ' + className}
>
{buttonEl}
{menuEl}
</div>
);
}
}
/** Define module API */
export default Dropdown;