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
175 lines (142 loc) · 3.65 KB
/
dropdown.jsx
File metadata and controls
175 lines (142 loc) · 3.65 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
/**
* 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.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.string,
alignMenu: PropTypes.oneOf(['left', 'right']),
onClick: PropTypes.func,
disabled: PropTypes.bool
};
static defaultProps = {
className: '',
color: 'default',
variant: 'default',
size: 'default',
label: '',
alignMenu: 'left',
onClick: null,
disabled: false
};
componentWillMount() {
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();
}
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() {
if (this.props.onClick) this.props.onClick(this, ev);
}
onOutsideClick(ev) {
let isClickInside = this.refs.wrapperEl.contains(ev.target);
if (!isClickInside) this.close();
}
render() {
let buttonEl,
menuEl;
buttonEl = (
<Button
ref="button"
type="button"
onClick={this.onClickCB}
color={this.props.color}
variant={this.props.variant}
size={this.props.size}
disabled={this.props.disabled}
>
{this.props.label}
<Caret />
</Button>
);
if (this.state.opened) {
let cs = {};
cs[menuClass] = true;
cs[openClass] = this.state.opened;
cs[rightClass] = (this.props.alignMenu === 'right');
cs = util.classNames(cs);
menuEl = (
<ul
ref="menuEl"
className={cs}
style={{top: this.state.menuTop} }
onClick={this.selectCB}
>
{this.props.children}
</ul>
);
}
let { ref, className, children, ...other } = this.props;
return (
<div
{ ...other }
ref="wrapperEl"
className={dropdownClass + ' ' + className}
>
{buttonEl}
{menuEl}
</div>
);
}
}
/** Define module API */
export default Dropdown;