-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
FocusGroup.js
227 lines (212 loc) · 6.72 KB
/
FocusGroup.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ReactScopeMethods} from 'shared/ReactTypes';
import type {KeyboardEvent} from 'react-interactions/events/keyboard';
import React from 'react';
import {useKeyboard} from 'react-interactions/events/keyboard';
import setElementCanTab from './shared/setElementCanTab';
type FocusItemProps = {
children?: React.Node,
onKeyDown?: KeyboardEvent => void,
};
type FocusGroupProps = {|
children: React.Node,
portrait: boolean,
wrap?: boolean,
tabScopeQuery?: (type: string | Object, props: Object) => boolean,
allowModifiers?: boolean,
|};
const {useRef} = React;
function focusGroupItem(
scopeQuery: (type: string | Object, props: Object) => boolean,
cell: ReactScopeMethods,
event: KeyboardEvent,
): void {
const firstScopedNode = cell.queryFirstNode(scopeQuery);
if (firstScopedNode !== null) {
firstScopedNode.focus();
event.preventDefault();
}
}
function getPreviousGroupItem(
group: ReactScopeMethods,
currentItem: ReactScopeMethods,
): null | ReactScopeMethods {
const items = group.getChildren();
if (items !== null) {
const currentItemIndex = items.indexOf(currentItem);
const wrap = getGroupProps(currentItem).wrap;
if (currentItemIndex === 0 && wrap) {
return items[items.length - 1] || null;
} else if (currentItemIndex > 0) {
return items[currentItemIndex - 1] || null;
}
}
return null;
}
function getNextGroupItem(
group: ReactScopeMethods,
currentItem: ReactScopeMethods,
): null | ReactScopeMethods {
const items = group.getChildren();
if (items !== null) {
const currentItemIndex = items.indexOf(currentItem);
const wrap = getGroupProps(currentItem).wrap;
const end = currentItemIndex === items.length - 1;
if (end && wrap) {
return items[0] || null;
} else if (currentItemIndex !== -1 && !end) {
return items[currentItemIndex + 1] || null;
}
}
return null;
}
function getGroupProps(currentCell: ReactScopeMethods): Object {
const group = currentCell.getParent();
if (group !== null) {
const groupProps = group.getProps();
if (groupProps && groupProps.type === 'group') {
return groupProps;
}
}
return {};
}
function hasModifierKey(event: KeyboardEvent): boolean {
const {altKey, ctrlKey, metaKey, shiftKey} = event;
return (
altKey === true || ctrlKey === true || metaKey === true || shiftKey === true
);
}
export function createFocusGroup(
scopeQuery: (type: string | Object, props: Object) => boolean,
): [(FocusGroupProps) => React.Node, (FocusItemProps) => React.Node] {
const TableScope = React.unstable_createScope();
function Group({
children,
portrait,
wrap,
tabScopeQuery,
allowModifiers,
}: FocusGroupProps): React.Node {
return (
<TableScope
type="group"
portrait={portrait}
wrap={wrap}
tabScopeQuery={tabScopeQuery}
allowModifiers={allowModifiers}>
{children}
</TableScope>
);
}
function Item({children, onKeyDown}: FocusItemProps): React.Node {
const scopeRef = useRef(null);
const keyboard = useKeyboard({
onKeyDown(event: KeyboardEvent): void {
const currentItem = scopeRef.current;
if (currentItem !== null) {
const group = currentItem.getParent();
const groupProps = group && group.getProps();
if (group !== null && groupProps.type === 'group') {
const portrait = groupProps.portrait;
const key = event.key;
if (key === 'Tab') {
const tabScopeQuery = getGroupProps(currentItem).tabScopeQuery;
if (tabScopeQuery) {
const groupScope = currentItem.getParent();
if (groupScope) {
const activeNode = document.activeElement;
const nodes = groupScope.queryAllNodes(tabScopeQuery);
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];
if (node !== activeNode) {
setElementCanTab(node, false);
} else {
setElementCanTab(node, true);
}
}
return;
}
}
event.continuePropagation();
return;
}
// Using modifier keys with keyboard arrow events should be no-ops
// unless an explicit allowModifiers flag is set on the FocusGroup.
if (hasModifierKey(event)) {
const allowModifiers = getGroupProps(currentItem).allowModifiers;
if (!allowModifiers) {
event.continuePropagation();
return;
}
}
switch (key) {
case 'ArrowUp': {
if (portrait) {
const previousGroupItem = getPreviousGroupItem(
group,
currentItem,
);
if (previousGroupItem) {
focusGroupItem(scopeQuery, previousGroupItem, event);
return;
}
}
break;
}
case 'ArrowDown': {
if (portrait) {
const nextGroupItem = getNextGroupItem(group, currentItem);
if (nextGroupItem) {
focusGroupItem(scopeQuery, nextGroupItem, event);
return;
}
}
break;
}
case 'ArrowLeft': {
if (!portrait) {
const previousGroupItem = getPreviousGroupItem(
group,
currentItem,
);
if (previousGroupItem) {
focusGroupItem(scopeQuery, previousGroupItem, event);
return;
}
}
break;
}
case 'ArrowRight': {
if (!portrait) {
const nextGroupItem = getNextGroupItem(group, currentItem);
if (nextGroupItem) {
focusGroupItem(scopeQuery, nextGroupItem, event);
return;
}
}
break;
}
}
}
}
if (onKeyDown) {
onKeyDown(event);
}
event.continuePropagation();
},
});
return (
<TableScope listeners={keyboard} ref={scopeRef} type="item">
{children}
</TableScope>
);
}
return [Group, Item];
}