Skip to content

Commit e199d17

Browse files
khiga8ljharb
authored andcommitted
[New] Add isFocusable utils method
1 parent bbae2c4 commit e199d17

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import expect from 'expect';
2+
import { elementType } from 'jsx-ast-utils';
3+
import isFocusable from '../../../src/util/isFocusable';
4+
import {
5+
genElementSymbol,
6+
genInteractiveElements,
7+
genNonInteractiveElements,
8+
} from '../../../__mocks__/genInteractives';
9+
import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock';
10+
11+
function mergeTabIndex(index, attributes) {
12+
return [...attributes, JSXAttributeMock('tabIndex', index)];
13+
}
14+
15+
describe('isFocusable', () => {
16+
describe('interactive elements', () => {
17+
genInteractiveElements().forEach(({ openingElement }) => {
18+
it(`should identify \`${genElementSymbol(openingElement)}\` as a focusable element`, () => {
19+
expect(isFocusable(
20+
elementType(openingElement),
21+
openingElement.attributes,
22+
)).toBe(true);
23+
});
24+
25+
it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`, () => {
26+
expect(isFocusable(
27+
elementType(openingElement),
28+
mergeTabIndex(-1, openingElement.attributes),
29+
)).toBe(false);
30+
});
31+
32+
it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`, () => {
33+
expect(isFocusable(
34+
elementType(openingElement),
35+
mergeTabIndex(0, openingElement.attributes),
36+
)).toBe(true);
37+
});
38+
39+
it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`, () => {
40+
expect(isFocusable(
41+
elementType(openingElement),
42+
mergeTabIndex(1, openingElement.attributes),
43+
)).toBe(true);
44+
});
45+
});
46+
});
47+
48+
describe('non-interactive elements', () => {
49+
genNonInteractiveElements().forEach(({ openingElement }) => {
50+
it(`should not identify \`${genElementSymbol(openingElement)}\` as a focusable element`, () => {
51+
expect(isFocusable(
52+
elementType(openingElement),
53+
openingElement.attributes,
54+
)).toBe(false);
55+
});
56+
57+
it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`, () => {
58+
expect(isFocusable(
59+
elementType(openingElement),
60+
mergeTabIndex(-1, openingElement.attributes),
61+
)).toBe(false);
62+
});
63+
64+
it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`, () => {
65+
expect(isFocusable(
66+
elementType(openingElement),
67+
mergeTabIndex(0, openingElement.attributes),
68+
)).toBe(true);
69+
});
70+
71+
it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`, () => {
72+
expect(isFocusable(
73+
elementType(openingElement),
74+
mergeTabIndex(1, openingElement.attributes),
75+
)).toBe(true);
76+
});
77+
78+
it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of 'bogus' as a focusable element`, () => {
79+
expect(isFocusable(
80+
elementType(openingElement),
81+
mergeTabIndex('bogus', openingElement.attributes),
82+
)).toBe(false);
83+
});
84+
});
85+
});
86+
});

src/util/isFocusable.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { getProp } from 'jsx-ast-utils';
2+
import getTabIndex from './getTabIndex';
3+
import isInteractiveElement from './isInteractiveElement';
4+
5+
/**
6+
* Returns boolean indicating whether an element appears in tab focus.
7+
* Identifies an element as focusable if it is an interactive element, or an element with a tabIndex greater than or equal to 0.
8+
*/
9+
function isFocusable(type, attributes) {
10+
const tabIndex = getTabIndex(getProp(attributes, 'tabIndex'));
11+
if (isInteractiveElement(type, attributes)) {
12+
return (tabIndex === undefined || tabIndex >= 0);
13+
}
14+
return tabIndex >= 0;
15+
}
16+
17+
export default isFocusable;

0 commit comments

Comments
 (0)