Skip to content

Commit 146426d

Browse files
committed
[fix] Allow focusable={false} to set tabIndex="-1"
Close #2046
1 parent 3a0cc73 commit 146426d

File tree

7 files changed

+28
-20
lines changed

7 files changed

+28
-20
lines changed

packages/react-native-web/src/exports/Button/__tests__/__snapshots__/index-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ exports[`components/Button prop "disabled" 1`] = `
3535
class="css-view-1dbjc4n r-backgroundColor-11mpjr4 r-borderRadius-1jkafct r-transitionProperty-1i6wzkk r-userSelect-lrvibr"
3636
role="button"
3737
style="transition-duration: 0s;"
38+
tabindex="-1"
3839
>
3940
<div
4041
class="css-text-901oao r-color-c68hjy r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw"

packages/react-native-web/src/exports/Image/__tests__/__snapshots__/index-test.js.snap

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@ exports[`components/Image prop "accessibilityLabel" 1`] = `
1818
</div>
1919
`;
2020

21-
exports[`components/Image prop "accessible" 1`] = `
22-
<div
23-
class="css-view-1dbjc4n r-flexBasis-1mlwlqe r-overflow-1udh08x r-zIndex-417010"
24-
>
25-
<div
26-
class="css-view-1dbjc4n r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-backgroundSize-4gszlv r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw"
27-
/>
28-
</div>
29-
`;
30-
3121
exports[`components/Image prop "blurRadius" 1`] = `
3222
<div
3323
class="css-view-1dbjc4n r-flexBasis-1mlwlqe r-overflow-1udh08x r-zIndex-417010"
@@ -132,6 +122,17 @@ exports[`components/Image prop "draggable" 1`] = `
132122
</div>
133123
`;
134124

125+
exports[`components/Image prop "focusable" 1`] = `
126+
<div
127+
class="css-view-1dbjc4n r-flexBasis-1mlwlqe r-overflow-1udh08x r-zIndex-417010"
128+
tabindex="0"
129+
>
130+
<div
131+
class="css-view-1dbjc4n r-backgroundColor-1niwhzg r-backgroundPosition-vvn4in r-backgroundRepeat-u6sd8q r-backgroundSize-4gszlv r-bottom-1p0dtai r-height-1pi2tsx r-left-1d2f490 r-position-u8s1d r-right-zchlnj r-top-ipm5af r-width-13qz1uu r-zIndex-1wyyakw"
132+
/>
133+
</div>
134+
`;
135+
135136
exports[`components/Image prop "nativeID" 1`] = `
136137
<div
137138
class="css-view-1dbjc4n r-flexBasis-1mlwlqe r-overflow-1udh08x r-zIndex-417010"

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ describe('components/Image', () => {
2929
expect(container.firstChild).toMatchSnapshot();
3030
});
3131

32-
test('prop "accessible"', () => {
33-
const { container } = render(<Image accessible={false} />);
34-
expect(container.firstChild).toMatchSnapshot();
35-
});
36-
3732
test('prop "blurRadius"', () => {
3833
const defaultSource = { uri: 'https://google.com/favicon.ico' };
3934
const { container } = render(<Image blurRadius={5} defaultSource={defaultSource} />);
@@ -83,6 +78,11 @@ describe('components/Image', () => {
8378
expect(container.firstChild).toMatchSnapshot();
8479
});
8580

81+
test('prop "focusable"', () => {
82+
const { container } = render(<Image focusable={true} />);
83+
expect(container.firstChild).toMatchSnapshot();
84+
});
85+
8686
test('prop "nativeID"', () => {
8787
const { container } = render(<Image nativeID="nativeID" />);
8888
expect(container.firstChild).toMatchSnapshot();

packages/react-native-web/src/exports/Pressable/__tests__/__snapshots__/index-test.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ exports[`components/Pressable prop "disabled" 1`] = `
132132
<div
133133
aria-disabled="true"
134134
class="css-view-1dbjc4n"
135+
tabindex="-1"
135136
/>
136137
`;
137138

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ describe('exports/createElement', () => {
515515
const { container: isFalseFocusableRole } = render(
516516
createElement('div', { accessibilityRole: 'button', focusable: false })
517517
);
518-
expect(getAttribute(isFalseFocusableRole, 'tabindex')).toBeNull();
518+
expect(getAttribute(isFalseFocusableRole, 'tabindex')).toBe('-1');
519519
});
520520
});
521521
});

packages/react-native-web/src/modules/createDOMProps/__tests__/index-test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('modules/createDOMProps', () => {
2727

2828
test('when "focusable" is false', () => {
2929
expect(createProps({ accessibilityRole, focusable: false })).toEqual(
30-
expect.not.objectContaining({ tabIndex: '-1' })
30+
expect.objectContaining({ tabIndex: '-1' })
3131
);
3232
});
3333

@@ -58,8 +58,8 @@ describe('modules/createDOMProps', () => {
5858
});
5959

6060
test('when "focusable" is false', () => {
61-
expect(createProps({ accessibilityRole, focusable: false })).not.toEqual(
62-
expect.objectContaining({ tabIndex: '0' })
61+
expect(createProps({ accessibilityRole, focusable: false })).toEqual(
62+
expect.objectContaining({ tabIndex: '-1' })
6363
);
6464
});
6565

@@ -92,7 +92,9 @@ describe('modules/createDOMProps', () => {
9292
});
9393

9494
test('when "focusable" is false', () => {
95-
expect(createProps({ focusable: false })).toEqual({});
95+
expect(createProps({ focusable: false })).toEqual(
96+
expect.objectContaining({ tabIndex: '-1' })
97+
);
9698
});
9799
});
98100
});

packages/react-native-web/src/modules/createDOMProps/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ const createDOMProps = (elementType, props) => {
346346
// FOCUS
347347
// "focusable" indicates that an element may be a keyboard tab-stop.
348348
const _focusable = focusable != null ? focusable : accessible;
349+
if (_focusable === false) {
350+
domProps.tabIndex = '-1';
351+
}
349352
if (
350353
// These native elements are focusable by default
351354
elementType === 'a' ||

0 commit comments

Comments
 (0)