-
Notifications
You must be signed in to change notification settings - Fork 8
/
Menu.js
240 lines (193 loc) · 6.93 KB
/
Menu.js
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
import { decl } from 'bem-react-core';
import PropTypes from 'prop-types';
import warning from 'warning';
import Stylable from 'b:Stylable';
import KeyCodes from 'b:KeyCodes';
const TIMEOUT_KEYBOARD_SEARCH = 1500;
export default decl([Stylable], {
block : 'Menu',
willInit({ focused, disabled }) {
warning(!(focused && disabled), `${this.block}: Can't have both "focused" and "disabled" props.`);
this.state = { focused };
this._id = this.generateId();
this._onFocus = this._onFocus.bind(this);
this._onBlur = this._onBlur.bind(this);
this._onKeyDown = this._onKeyDown.bind(this);
this._onKeyPress = this._onKeyPress.bind(this);
this._registeredItems = [];
this._registerMenuItem = this._registerMenuItem.bind(this);
this._unregisterMenuItem = this._unregisterMenuItem.bind(this);
this._hoverItem = this._hoverItem.bind(this);
this._lastTyping = {
idx : 0,
text : '',
char : '',
time : 0
};
},
willReceiveProps({ focused, disabled }) {;
warning(!(focused && disabled), `${this.block}: Can't have both "focused" and "disabled" props.`);
const newState = {};
if(typeof focused !== 'undefined') {
newState.focused = focused;
this.setState(newState);
}
},
didMount() {
this.state.focused? this._focus() : this._blur();
},
didUpdate() {
this.state.focused? this._focus() : this._blur();
},
getChildContext() {
return {
_menuDisabled : this.props.disabled,
_menuMode : this.props.mode,
_menuRegisterItem : this._registerMenuItem,
_menuUnregisterItem : this._unregisterMenuItem,
_menuHoverItem : this._hoverItem
};
},
mods({ disabled }) {
return {
...this.__base(...arguments),
disabled,
focused : this.state.focused
};
},
attrs({ tabIndex, disabled }) {
let res = {
ref : ref => this._domNode = ref,
role : 'menu'
};
if(disabled)
res['aria-disabled'] = 'true';
else {
this.props.hasOwnProperty('tabIndex') || (tabIndex = 0);
res = {
...res,
onFocus : this._onFocus,
onBlur : this._onBlur,
onKeyPress : this._onKeyPress,
onKeyDown : this._onKeyDown
};
}
res.tabIndex = tabIndex;
return res;
},
_onFocus() {
this.state.focused || this.setState(
{ focused : true },
() => this.props.onFocusChange(true));
},
_onBlur() {
this.setState(
{ focused : false },
() => this.props.onFocusChange(false));
},
_onKeyDown(e) {
if(!e.shiftKey && KeyCodes.is(e.keyCode, 'UP', 'DOWN')) {
e.preventDefault();
const len = this._registeredItems.length,
hoveredIdx = this._registeredItems.indexOf(this._hoveredItem),
dir = e.keyCode - 39; // using the features of key codes for "up"/"down" ;-)
let nextIdx = hoveredIdx,
i = 0;
do {
nextIdx += dir;
nextIdx = nextIdx < 0? len - 1 : nextIdx >= len? 0 : nextIdx;
if(++i === len) return; // if we have no next item to hover
} while(this._registeredItems[nextIdx].props.disabled);
this._lastTyping.idx = nextIdx;
this._hoverItem(nextIdx);
}
},
searchItemIdxByKeyboardEvent(e) {
const currentTime = +new Date(),
charCode = e.charCode,
char = String.fromCharCode(charCode).toLowerCase(),
lastTyping = this._lastTyping,
isSameChar = char === lastTyping.char && lastTyping.text.length === 1,
items = this._registeredItems;
if(charCode <= KeyCodes.SPACE || e.ctrlKey || e.altKey || e.metaKey) {
lastTyping.time = currentTime;
return null;
}
if(currentTime - lastTyping.time > TIMEOUT_KEYBOARD_SEARCH || isSameChar)
lastTyping.text = char;
else
lastTyping.text += char;
lastTyping.char = char;
lastTyping.time = currentTime;
let idx = lastTyping.idx;
// If key is pressed again, then continue to search to next menu item
if(isSameChar && items[idx].getText().search(lastTyping.char) === 0)
idx = idx >= items.length - 1? 0 : idx + 1;
// 2 passes: from idx to items.length and from 0 to idx.
let i = idx, len = items.length;
while(i < len) {
if(this._doesItemMatchText(items[i], lastTyping.text)) {
lastTyping.idx = i;
return i;
}
i++;
if(i === items.length) {
i = 0;
len = idx;
}
}
return null;
},
_doesItemMatchText(item, text) {
return !item.props.disabled &&
item.getText().toLowerCase().search(text) === 0;
},
_onKeyPress(e) {
const itemIdx = this.searchItemIdxByKeyboardEvent(e);
itemIdx !== null && this._hoverItem(itemIdx);
},
_focus() {
document.addEventListener('keydown', this._onKeyDown);
document.addEventListener('keypress', this._onKeyPress);
// TODO: call _onFocus as in
// https://github.com/bem/bem-components/blob/v6/common.blocks/control/control.js#L99-L101
document.activeElement !== this._domNode && this._domNode.focus();
},
_blur() {
document.removeEventListener('keydown', this._onKeyDown);
document.removeEventListener('keypress', this._onKeyPress);
document.activeElement === this._domNode && this._domNode.blur();
},
_registerMenuItem(item) {
return (this._registeredItems.push(item) - 1);
},
_unregisterMenuItem(item) {
this._hoveredItem === item && (this._hoveredItem = null);
return this._registeredItems.splice(this._registeredItems.indexOf(item), 1);
},
_hoverItem(idx) {
if(this._hoveredItem !== this._registeredItems[idx]) {
this._hoveredItem && this._hoveredItem.setHovered(false);
this._hoveredItem = this._registeredItems[idx];
this._hoveredItem && this._hoveredItem.setHovered(true);
}
}
}, {
propTypes : {
disabled : PropTypes.bool,
focused : PropTypes.bool,
onFocusChange : PropTypes.func
},
defaultProps : {
onClick() {},
onFocusChange() {}
},
childContextTypes : {
_menuDisabled : PropTypes.bool,
_menuItemsIndex : PropTypes.object,
_menuMode : PropTypes.string,
_menuRegisterItem : PropTypes.func,
_menuUnregisterItem : PropTypes.func,
_menuHoverItem : PropTypes.func
}
});