Skip to content

Commit 55731fd

Browse files
authored
[react-interactions] Refine a11y component flow types (#17032)
1 parent a011aac commit 55731fd

File tree

5 files changed

+34
-40
lines changed

5 files changed

+34
-40
lines changed

packages/react-interactions/accessibility/src/FocusContain.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export default function FocusContain({
3030
children,
3131
disabled,
3232
tabScope: TabScope,
33-
}: FocusContainProps) {
33+
}: FocusContainProps): React.Node {
3434
const scopeRef = useRef(null);
3535
// This ensures tabbing works through the React tree (including Portals and Suspense nodes)
3636
const keyboard = useKeyboard({
3737
onKeyDown(event: KeyboardEvent): void {
38-
if (disabled || event.key !== 'Tab') {
38+
if (disabled === true || event.key !== 'Tab') {
3939
event.continuePropagation();
4040
return;
4141
}
@@ -51,7 +51,7 @@ export default function FocusContain({
5151
});
5252
const focusWithin = useFocusWithin({
5353
onBlurWithin: function(event) {
54-
if (disabled) {
54+
if (disabled === true) {
5555
event.continuePropagation();
5656
return;
5757
}
@@ -66,7 +66,7 @@ export default function FocusContain({
6666
useLayoutEffect(
6767
() => {
6868
const scope = scopeRef.current;
69-
if (scope && !disabled) {
69+
if (scope !== null && disabled !== true) {
7070
const elems = scope.getScopedNodes();
7171
if (elems && elems.indexOf(document.activeElement) === -1) {
7272
elems[0].focus();

packages/react-interactions/accessibility/src/FocusGroup.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ function hasModifierKey(event: KeyboardEvent): boolean {
9090
);
9191
}
9292

93-
export function createFocusGroup(scope: ReactScope): Array<React.Component> {
93+
export function createFocusGroup(
94+
scope: ReactScope,
95+
): [(FocusGroupProps) => React.Node, (FocusItemProps) => React.Node] {
9496
const TableScope = React.unstable_createScope(scope.fn);
9597

9698
function Group({
@@ -99,7 +101,7 @@ export function createFocusGroup(scope: ReactScope): Array<React.Component> {
99101
wrap,
100102
tabScope: TabScope,
101103
allowModifiers,
102-
}): FocusGroupProps {
104+
}: FocusGroupProps): React.Node {
103105
const tabScopeRef = useRef(null);
104106
return (
105107
<TableScope
@@ -117,7 +119,7 @@ export function createFocusGroup(scope: ReactScope): Array<React.Component> {
117119
);
118120
}
119121

120-
function Item({children, onKeyDown}): FocusItemProps {
122+
function Item({children, onKeyDown}: FocusItemProps): React.Node {
121123
const scopeRef = useRef(null);
122124
const keyboard = useKeyboard({
123125
onKeyDown(event: KeyboardEvent): void {

packages/react-interactions/accessibility/src/FocusManager.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ export function focusNext(
4141
event.continuePropagation();
4242
}
4343
} else if (focusedElement === lastTabbableElem) {
44-
if (contain) {
44+
if (contain === true) {
4545
focusElem(firstTabbableElem);
4646
if (event) {
4747
event.preventDefault();
4848
}
4949
} else if (event) {
5050
event.continuePropagation();
5151
}
52-
} else {
53-
focusElem((tabbableNodes: any)[currentIndex + 1]);
52+
} else if (tabbableNodes) {
53+
focusElem(tabbableNodes[currentIndex + 1]);
5454
if (event) {
5555
event.preventDefault();
5656
}
@@ -75,16 +75,16 @@ export function focusPrevious(
7575
event.continuePropagation();
7676
}
7777
} else if (focusedElement === firstTabbableElem) {
78-
if (contain) {
78+
if (contain === true) {
7979
focusElem(lastTabbableElem);
8080
if (event) {
8181
event.preventDefault();
8282
}
8383
} else if (event) {
8484
event.continuePropagation();
8585
}
86-
} else {
87-
focusElem((tabbableNodes: any)[currentIndex - 1]);
86+
} else if (tabbableNodes) {
87+
focusElem(tabbableNodes[currentIndex - 1]);
8888
if (event) {
8989
event.preventDefault();
9090
}

packages/react-interactions/accessibility/src/FocusTable.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,6 @@ type FocusTableProps = {|
3838

3939
const {useRef} = React;
4040

41-
export function focusFirstCellOnTable(table: ReactScopeMethods): void {
42-
const rows = table.getChildren();
43-
if (rows !== null) {
44-
const firstRow = rows[0];
45-
if (firstRow !== null) {
46-
const cells = firstRow.getChildren();
47-
if (cells !== null) {
48-
const firstCell = cells[0];
49-
if (firstCell !== null) {
50-
const tabbableNodes = firstCell.getScopedNodes();
51-
if (tabbableNodes !== null) {
52-
const firstElem = tabbableNodes[0];
53-
if (firstElem !== null) {
54-
firstElem.focus();
55-
}
56-
}
57-
}
58-
}
59-
}
60-
}
61-
}
62-
6341
function focusScope(cell: ReactScopeMethods, event?: KeyboardEvent): void {
6442
const tabbableNodes = cell.getScopedNodes();
6543
if (tabbableNodes !== null && tabbableNodes.length > 0) {
@@ -178,7 +156,13 @@ function hasModifierKey(event: KeyboardEvent): boolean {
178156
);
179157
}
180158

181-
export function createFocusTable(scope: ReactScope): Array<React.Component> {
159+
export function createFocusTable(
160+
scope: ReactScope,
161+
): [
162+
(FocusTableProps) => React.Node,
163+
(FocusRowProps) => React.Node,
164+
(FocusCellProps) => React.Node,
165+
] {
182166
const TableScope = React.unstable_createScope(scope.fn);
183167

184168
function Table({
@@ -188,7 +172,7 @@ export function createFocusTable(scope: ReactScope): Array<React.Component> {
188172
wrapY,
189173
tabScope: TabScope,
190174
allowModifiers,
191-
}): FocusTableProps {
175+
}: FocusTableProps): React.Node {
192176
const tabScopeRef = useRef(null);
193177
return (
194178
<TableScope
@@ -207,11 +191,11 @@ export function createFocusTable(scope: ReactScope): Array<React.Component> {
207191
);
208192
}
209193

210-
function Row({children}): FocusRowProps {
194+
function Row({children}: FocusRowProps): React.Node {
211195
return <TableScope type="row">{children}</TableScope>;
212196
}
213197

214-
function Cell({children, onKeyDown, colSpan}): FocusCellProps {
198+
function Cell({children, onKeyDown, colSpan}: FocusCellProps): React.Node {
215199
const scopeRef = useRef(null);
216200
const keyboard = useKeyboard({
217201
onKeyDown(event: KeyboardEvent): void {

packages/react-interactions/accessibility/src/shared/getTabbableNodes.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99

1010
import type {ReactScopeMethods} from 'shared/ReactTypes';
1111

12-
export default function getTabbableNodes(scope: ReactScopeMethods) {
12+
export default function getTabbableNodes(
13+
scope: ReactScopeMethods,
14+
): [
15+
null | Array<HTMLElement>,
16+
null | HTMLElement,
17+
null | HTMLElement,
18+
number,
19+
null | HTMLElement,
20+
] {
1321
const tabbableNodes = scope.getScopedNodes();
1422
if (tabbableNodes === null || tabbableNodes.length === 0) {
1523
return [null, null, null, 0, null];

0 commit comments

Comments
 (0)