Skip to content

Commit 77f6fba

Browse files
s77rtnecolas
authored andcommitted
[fix] Avoid setting focus tabIndex on <body>
Also don't modify the tabIndex on contenteditable elements when using programmatic focus APIs. Fix #2473 Close #2468
1 parent ccfd936 commit 77f6fba

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

packages/react-native-web/src/exports/UIManager/__tests__/index-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe('apis/UIManager', () => {
4444
expect(node.getAttribute('tabIndex')).toBeNull();
4545
});
4646
});
47+
48+
test('doesn\'t set tabIndex="-1" in other cases', () => {
49+
// on body
50+
UIManager.focus(document.body);
51+
expect(document.body.getAttribute('tabIndex')).toBeNull();
52+
53+
// on contenteditable elements
54+
const div = document.createElement('div');
55+
div.contentEditable = 'true';
56+
div.isContentEditable = true; // jsdom doesn't support this API yet (https://github.com/jsdom/jsdom/issues/1670)
57+
UIManager.focus(div);
58+
expect(div.getAttribute('tabIndex')).toBeNull();
59+
});
4760
});
4861

4962
describe('updateView', () => {

packages/react-native-web/src/exports/UIManager/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ const measureLayout = (node, relativeToNativeNode, callback) => {
3333
}
3434
};
3535

36-
const focusableElements = {
36+
const elementsToIgnore = {
3737
A: true,
38+
BODY: true,
3839
INPUT: true,
3940
SELECT: true,
4041
TEXTAREA: true
@@ -51,11 +52,12 @@ const UIManager = {
5152
try {
5253
const name = node.nodeName;
5354
// A tabIndex of -1 allows element to be programmatically focused but
54-
// prevents keyboard focus, so we don't want to set the value on elements
55-
// that support keyboard focus by default.
55+
// prevents keyboard focus. We don't want to set the tabindex value on
56+
// elements that should not prevent keyboard focus.
5657
if (
5758
node.getAttribute('tabIndex') == null &&
58-
focusableElements[name] == null
59+
node.isContentEditable !== true &&
60+
elementsToIgnore[name] == null
5961
) {
6062
node.setAttribute('tabIndex', '-1');
6163
}

0 commit comments

Comments
 (0)