forked from muicss/mui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.jsx
More file actions
396 lines (314 loc) · 8.81 KB
/
select.jsx
File metadata and controls
396 lines (314 loc) · 8.81 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
* 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.hideMenuCB = cb(this, 'hideMenu');
this.onInnerChangeCB = cb(this, 'onInnerChange');
this.onInnerClickCB = cb(this, 'onInnerClick');
this.onInnerFocusCB = cb(this, 'onInnerFocus');
this.onInnerMouseDownCB = cb(this, 'onInnerMouseDown');
this.onKeydownCB = cb(this, 'onKeydown');
this.onMenuChangeCB = cb(this, 'onMenuChange');
this.onOuterFocusCB = cb(this, 'onOuterFocus');
this.onOuterBlurCB = cb(this, 'onOuterBlur');
}
state = {
showMenu: false
};
static propTypes = {
label: PropTypes.string,
value: PropTypes.string,
defaultValue: PropTypes.string,
readOnly: PropTypes.bool,
useDefault: PropTypes.bool,
onChange: PropTypes.func
};
static defaultProps = {
className: '',
readOnly: false,
useDefault: false,
onChange: null
};
componentDidMount() {
// disable MUI js
this.refs.selectEl._muiSelect = true;
// make wrapper element focusable (to enable Firefox bugfix)
this.refs.wrapperEl.tabIndex = -1;
// handle autofocus
if (this.props.autoFocus) this.refs.wrapperEl.focus();
}
componentWillReceiveProps(nextProps) {
this.setState({value: nextProps.value});
}
onInnerMouseDown(ev) {
if (ev.button !== 0 || this.props.useDefault === true) return;
ev.preventDefault();
// execute callback
const fn = this.props.onMouseDown;
fn && fn(ev);
}
onInnerChange(ev) {
let value = ev.target.value;
this.setState({ value });
// execute callback
const fn = this.props.onChange;
fn && fn(value);
}
onInnerClick(ev) {
if (ev.button !== 0) return; // only left clicks
this.showMenu();
// execute callback
const fn = this.props.onClick;
fn && fn(ev);
}
onInnerFocus(ev) {
// check flag
if (this.props.useDefault === true) return;
// defer focus to parent
setTimeout(() => {this.refs.wrapperEl.focus();}, 0);
}
onOuterFocus(ev) {
// ignore focus on inner element (react artifact)
if (ev.target !== this.refs.wrapperEl) return;
// disable tabfocus on inner element
var selectEl = this.refs.selectEl;
selectEl._muiOrigIndex = selectEl.tabIndex;
selectEl.tabIndex = -1;
// firefox bugfix
if (selectEl.disabled) return this.refs.wrapperEl.blur();
// attach keydown handler
jqLite.on(document, 'keydown', this.onKeydownCB);
// execute callback
const fn = this.onFocus;
fn && fn(ev);
}
onOuterBlur(ev) {
// ignore blur on inner element
if (ev.target !== this.refs.wrapperEl) return;
// restore tab focus on inner element
let selectEl = this.refs.selectEl;
selectEl.tabIndex = selectEl._muiOrigIndex;
// remove keydown handler
jqLite.off(document, 'keydown', this.onKeydownCB);
// execute callback
const fn = this.onBlur;
fn && fn(ev);
}
onKeydown(ev) {
// spacebar, down, up
if (ev.keyCode === 32 || ev.keyCode === 38 || ev.keyCode === 40) {
// prevent win scroll
ev.preventDefault();
if (this.refs.selectEl.disabled !== true) this.showMenu();
}
}
showMenu() {
// check useDefault flag
if (this.props.useDefault === true) return;
// add scroll lock
util.enableScrollLock();
// add event listeners
jqLite.on(window, 'resize', this.hideMenuCB);
jqLite.on(document, 'click', this.hideMenuCB);
// re-draw
this.setState({showMenu: true});
}
hideMenu() {
// remove scroll lock
util.disableScrollLock();
// remove event listeners
jqLite.off(window, 'resize', this.hideMenuCB);
jqLite.off(document, 'click', this.hideMenuCB);
// re-draw
this.setState({showMenu: false});
// refocus
this.refs.selectEl.focus();
}
onMenuChange(value) {
if (this.props.readOnly === true) return;
this.setState({ value });
// execute onChange method
let fn = this.props.onChange;
if (fn) fn(value);
}
render() {
let menuElem;
if (this.state.showMenu) {
menuElem = (
<Menu
optionEls={this.refs.selectEl.children}
wrapperEl={this.refs.wrapperEl}
onChange={this.onMenuChangeCB}
onClose={this.hideMenuCB}
/>
);
}
const { children, className, style, label, value, defaultValue, readOnly,
useDefault, ...reactProps } = this.props;
return (
<div
ref="wrapperEl"
style={style}
className={'mui-select ' + className}
onFocus={this.onOuterFocusCB}
onBlur={this.onOuterBlurCB}
>
<select
{ ...reactProps }
ref="selectEl"
value={value}
defaultValue={defaultValue}
readOnly={this.props.readOnly}
onChange={this.onInnerChangeCB}
onMouseDown={this.onInnerMouseDownCB}
onClick={this.onInnerClickCB}
onFocus={this.onInnerFocusCB}
>
{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() {
// blur active element (IE10 bugfix)
this.blurTimer = setTimeout(function() {
let el = document.activeElement;
if (el.nodeName.toLowerCase() !== 'body') el.blur();
}, 0);
// 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() {
// clear timer
clearTimeout(this.blurTimer);
// 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' : '';
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;