-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
select.jsx
375 lines (297 loc) · 8.4 KB
/
select.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* MUI React select module
* @module react/select
*/
'use strict';
import React from 'react';
import * as formlib from '../js/lib/forms';
import * as jqLite from '../js/lib/jqLite';
import * as util from '../js/lib/util';
import { controlledMessage } from './_helpers';
const PropTypes = React.PropTypes;
/**
* Select constructor
* @class
*/
class Select extends React.Component {
constructor(props) {
super(props);
// warn if value defined but onChange is not
if (props.readOnly === false &&
props.value !== undefined &&
props.onChange === null) {
util.raiseError(controlledMessage, true);
}
this.state.value = props.value;
// bind callback function
let cb = util.callback;
this.onInnerChangeCB = cb(this, 'onInnerChange');
this.onInnerMouseDownCB = cb(this, 'onInnerMouseDown');
this.onOuterClickCB = cb(this, 'onOuterClick');
this.onOuterKeyDownCB = cb(this, 'onOuterKeyDown');
this.hideMenuCB = cb(this, 'hideMenu');
this.onMenuChangeCB = cb(this, 'onMenuChange');
}
state = {
showMenu: false
};
static propTypes = {
label: PropTypes.string,
value: PropTypes.string,
name: PropTypes.string,
defaultValue: PropTypes.string,
readOnly: PropTypes.bool,
useDefault: PropTypes.bool,
onChange: PropTypes.func,
onClick: PropTypes.func,
onKeyDown: PropTypes.func
};
static defaultProps = {
className: '',
name: '',
readOnly: false,
useDefault: (typeof document !== 'undefined' && 'ontouchstart' in document.documentElement) ? true : false,
onChange: null,
onClick: null,
onKeyDown: null
};
componentDidMount() {
// disable MUI CSS/JS
this.refs.selectEl._muiSelect = true;
}
componentWillReceiveProps(nextProps) {
this.setState({value: nextProps.value});
}
componentWillUnmount() {
// ensure that doc event listners have been removed
jqLite.off(window, 'resize', this.hideMenuCB);
jqLite.off(document, 'click', this.hideMenuCB);
}
onInnerChange(ev) {
let value = ev.target.value;
// update state
this.setState({ value });
}
onInnerMouseDown(ev) {
// only left clicks & check flag
if (ev.button !== 0 || this.props.useDefault) return;
// prevent built-in menu from opening
ev.preventDefault();
}
onOuterClick(ev) {
// only left clicks, return if <select> is disabled
if (ev.button !== 0 || this.refs.selectEl.disabled) return;
// execute callback
const fn = this.props.onClick;
fn && fn(ev);
// exit if preventDefault() was called
if (ev.defaultPrevented || this.props.useDefault) return;
// focus wrapper
this.refs.wrapperEl.focus();
// open custom menu
this.showMenu();
}
onOuterKeyDown(ev) {
// execute callback
const fn = this.props.onKeyDown;
fn && fn(ev);
// exit if preventDevault() was called or useDefault is true
if (ev.defaultPrevented || this.props.useDefault) return;
if (this.state.showMenu === false) {
let keyCode = ev.keyCode;
// spacebar, down, up
if (keyCode === 32 || keyCode === 38 || keyCode === 40) {
// prevent default browser action
ev.preventDefault();
// open custom menu
this.showMenu();
}
}
}
showMenu() {
// check useDefault flag
if (this.props.useDefault) return;
// add event listeners
jqLite.on(window, 'resize', this.hideMenuCB);
jqLite.on(document, 'click', this.hideMenuCB);
// re-draw
this.setState({showMenu: true});
}
hideMenu() {
// remove event listeners
jqLite.off(window, 'resize', this.hideMenuCB);
jqLite.off(document, 'click', this.hideMenuCB);
// re-draw
this.setState({showMenu: false});
// refocus
this.refs.wrapperEl.focus();
}
onMenuChange(value) {
if (this.props.readOnly) return;
// update inner <select> and dispatch 'change' event
this.refs.selectEl.value = value;
util.dispatchEvent(this.refs.selectEl, 'change');
}
render() {
let menuElem;
if (this.state.showMenu) {
menuElem = (
<Menu
optionEls={this.refs.selectEl.children}
wrapperEl={this.refs.wrapperEl}
onChange={this.onMenuChangeCB}
onClose={this.hideMenuCB}
/>
);
}
// set tab index so user can focus wrapper element
let tabIndexWrapper = '-1',
tabIndexInner = '0';
if (this.props.useDefault === false) {
tabIndexWrapper = '0';
tabIndexInner = '-1';
}
const { children, className, style, label, defaultValue, readOnly,
useDefault, name, ...reactProps } = this.props;
return (
<div
{ ...reactProps }
ref="wrapperEl"
tabIndex={tabIndexWrapper}
style={style}
className={'mui-select ' + className}
onClick={this.onOuterClickCB}
onKeyDown={this.onOuterKeyDownCB}
>
<select
ref="selectEl"
name={name}
tabIndex={tabIndexInner}
value={this.state.value}
defaultValue={defaultValue}
readOnly={this.props.readOnly}
onChange={this.onInnerChangeCB}
onMouseDown={this.onInnerMouseDownCB}
>
{children}
</select>
<label>{label}</label>
{menuElem}
</div>
);
}
}
/**
* Menu constructor
* @class
*/
class Menu extends React.Component {
constructor(props) {
super(props);
this.onKeyDownCB = util.callback(this, 'onKeyDown');
}
state = {
origIndex: null,
currentIndex: null
};
static defaultProps = {
optionEls: [],
wrapperEl: null,
onChange: null,
onClose: null
};
componentWillMount() {
let optionEls = this.props.optionEls,
m = optionEls.length,
selectedPos = 0,
i;
// get current selected position
for (i=m - 1; i > -1; i--) if (optionEls[i].selected) selectedPos = i;
this.setState({origIndex: selectedPos, currentIndex: selectedPos});
}
componentDidMount() {
// prevent scrolling
util.enableScrollLock();
// set position
let props = formlib.getMenuPositionalCSS(
this.props.wrapperEl,
this.props.optionEls.length,
this.state.currentIndex
);
let el = this.refs.wrapperEl;
jqLite.css(el, props);
jqLite.scrollTop(el, props.scrollTop);
// attach keydown handler
jqLite.on(document, 'keydown', this.onKeyDownCB);
}
componentWillUnmount() {
// remove scroll lock
util.disableScrollLock(true);
// remove keydown handler
jqLite.off(document, 'keydown', this.onKeyDownCB);
}
onClick(pos, ev) {
// don't allow events to bubble
ev.stopPropagation();
this.selectAndDestroy(pos);
}
onKeyDown(ev) {
let keyCode = ev.keyCode;
// tab
if (keyCode === 9) return this.destroy();
// escape | up | down | enter
if (keyCode === 27 || keyCode === 40 || keyCode === 38 || keyCode === 13) {
ev.preventDefault();
}
if (keyCode === 27) this.destroy();
else if (keyCode === 40) this.increment();
else if (keyCode === 38) this.decrement();
else if (keyCode === 13) this.selectAndDestroy();
}
increment() {
if (this.state.currentIndex === this.props.optionEls.length - 1) return;
this.setState({currentIndex: this.state.currentIndex + 1});
}
decrement() {
if (this.state.currentIndex === 0) return;
this.setState({currentIndex: this.state.currentIndex - 1});
}
selectAndDestroy(pos) {
pos = (pos === undefined) ? this.state.currentIndex : pos;
// handle onChange
if (pos !== this.state.origIndex) {
this.props.onChange(this.props.optionEls[pos].value);
}
// close menu
this.destroy();
}
destroy() {
this.props.onClose();
}
render() {
let menuItems = [],
optionEls = this.props.optionEls,
m = optionEls.length,
optionEl,
cls,
i;
// define menu items
for (i=0; i < m; i++) {
cls = (i === this.state.currentIndex) ? 'mui--is-selected ' : '';
// add custom css class from <Option> component
cls += optionEls[i].className;
menuItems.push(
<div
key={i}
className={cls}
onClick={this.onClick.bind(this, i)}
>
{optionEls[i].textContent}
</div>
);
}
return <div ref="wrapperEl" className="mui-select__menu">{menuItems}</div>;
}
}
/** Define module API */
export default Select;