-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ArrowKeyFocusManager.js
94 lines (73 loc) · 3.36 KB
/
ArrowKeyFocusManager.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
import {Component} from 'react';
import PropTypes from 'prop-types';
import CONST from '../CONST';
import KeyboardShortcut from '../libs/KeyboardShortcut';
const propTypes = {
/** Children to render. */
children: PropTypes.oneOfType([
PropTypes.func,
PropTypes.node,
]).isRequired,
/** Array of disabled indexes. */
disabledIndexes: PropTypes.arrayOf(PropTypes.number),
/** The current focused index. */
focusedIndex: PropTypes.number.isRequired,
/** The maximum index – provided so that the focus can be sent back to the beginning of the list when the end is reached. */
maxIndex: PropTypes.number.isRequired,
/** A callback executed when the focused input changes. */
onFocusedIndexChanged: PropTypes.func.isRequired,
/** If this value is true, then we exclude TextArea Node. */
shouldExcludeTextAreaNodes: PropTypes.bool,
};
const defaultProps = {
disabledIndexes: [],
shouldExcludeTextAreaNodes: true,
};
class ArrowKeyFocusManager extends Component {
componentDidMount() {
const arrowUpConfig = CONST.KEYBOARD_SHORTCUTS.ARROW_UP;
const arrowDownConfig = CONST.KEYBOARD_SHORTCUTS.ARROW_DOWN;
this.unsubscribeArrowUpKey = KeyboardShortcut.subscribe(arrowUpConfig.shortcutKey, () => {
if (this.props.maxIndex < 0) {
return;
}
const currentFocusedIndex = this.props.focusedIndex > 0 ? this.props.focusedIndex - 1 : this.props.maxIndex;
let newFocusedIndex = currentFocusedIndex;
while (this.props.disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex > 0 ? newFocusedIndex - 1 : this.props.maxIndex;
if (newFocusedIndex === currentFocusedIndex) { // all indexes are disabled
return; // no-op
}
}
this.props.onFocusedIndexChanged(newFocusedIndex);
}, arrowUpConfig.descriptionKey, arrowUpConfig.modifiers, true, false, 1, true, [this.props.shouldExcludeTextAreaNodes && 'TEXTAREA']);
this.unsubscribeArrowDownKey = KeyboardShortcut.subscribe(arrowDownConfig.shortcutKey, () => {
if (this.props.maxIndex < 0) {
return;
}
const currentFocusedIndex = this.props.focusedIndex < this.props.maxIndex ? this.props.focusedIndex + 1 : 0;
let newFocusedIndex = currentFocusedIndex;
while (this.props.disabledIndexes.includes(newFocusedIndex)) {
newFocusedIndex = newFocusedIndex < this.props.maxIndex ? newFocusedIndex + 1 : 0;
if (newFocusedIndex === currentFocusedIndex) { // all indexes are disabled
return; // no-op
}
}
this.props.onFocusedIndexChanged(newFocusedIndex);
}, arrowDownConfig.descriptionKey, arrowDownConfig.modifiers, true, false, 1, true, [this.props.shouldExcludeTextAreaNodes && 'TEXTAREA']);
}
componentWillUnmount() {
if (this.unsubscribeArrowUpKey) {
this.unsubscribeArrowUpKey();
}
if (this.unsubscribeArrowDownKey) {
this.unsubscribeArrowDownKey();
}
}
render() {
return this.props.children;
}
}
ArrowKeyFocusManager.propTypes = propTypes;
ArrowKeyFocusManager.defaultProps = defaultProps;
export default ArrowKeyFocusManager;