Skip to content

Commit 13bf34e

Browse files
committed
[change] Map 'button' and 'paragraph' role to HTML elements
Fix #1899 Close #1944
1 parent ff2538d commit 13bf34e

File tree

9 files changed

+29
-30
lines changed

9 files changed

+29
-30
lines changed

packages/react-native-web-docs/src/pages/docs/concepts/accessibility.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,11 @@ The value of the `accessibilityRole` prop is used to infer an [analogous HTML el
277277
</View>
278278
/*
279279
<article>
280-
<div role="paragraph">This is an article</div>
280+
<p>This is an article</p>
281281
</article>
282282
*/
283283
```
284284

285-
The `"paragraph"` role isn't mapped to a `<p>` tag because it's an HTML conformance error to include block-level children within the element; both `Text` and `View` support block-level children.
286-
287285
If the `"heading"` role is combined with an `accessibilityLevel`, the equivalent HTML heading element is rendered. Otherwise, it is rendered as `<h1>`.
288286

289287
```jsx
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,78 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`components/Button prop "accessibilityLabel" 1`] = `
4-
<div
4+
<button
55
aria-label="accessibility label"
66
class="css-view-175oi2r r-transitionProperty-1i6wzkk r-userSelect-lrvibr r-cursor-1loqt21 r-touchAction-1otgn73 r-backgroundColor-14sbq61 r-borderRadius-1jkafct"
77
role="button"
88
style="transition-duration: 0s;"
9-
tabindex="0"
9+
type="button"
1010
>
1111
<div
1212
class="css-text-1rynq56 r-color-jwli3a r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw"
1313
dir="auto"
1414
/>
15-
</div>
15+
</button>
1616
`;
1717

1818
exports[`components/Button prop "color" 1`] = `
19-
<div
19+
<button
2020
class="css-view-175oi2r r-transitionProperty-1i6wzkk r-userSelect-lrvibr r-cursor-1loqt21 r-touchAction-1otgn73 r-borderRadius-1jkafct"
2121
role="button"
2222
style="background-color: rgb(0, 0, 255); transition-duration: 0s;"
23-
tabindex="0"
23+
type="button"
2424
>
2525
<div
2626
class="css-text-1rynq56 r-color-jwli3a r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw"
2727
dir="auto"
2828
/>
29-
</div>
29+
</button>
3030
`;
3131

3232
exports[`components/Button prop "disabled" 1`] = `
33-
<div
33+
<button
3434
aria-disabled="true"
3535
class="css-view-175oi2r r-transitionProperty-1i6wzkk r-userSelect-lrvibr r-borderRadius-1jkafct r-backgroundColor-11mpjr4 r-pointerEvents-633pao"
36+
disabled=""
3637
role="button"
3738
style="transition-duration: 0s;"
3839
tabindex="-1"
40+
type="button"
3941
>
4042
<div
4143
class="css-text-1rynq56 r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw r-color-c68hjy"
4244
dir="auto"
4345
/>
44-
</div>
46+
</button>
4547
`;
4648

4749
exports[`components/Button prop "testID" 1`] = `
48-
<div
50+
<button
4951
class="css-view-175oi2r r-transitionProperty-1i6wzkk r-userSelect-lrvibr r-cursor-1loqt21 r-touchAction-1otgn73 r-backgroundColor-14sbq61 r-borderRadius-1jkafct"
5052
data-testid="123"
5153
role="button"
5254
style="transition-duration: 0s;"
53-
tabindex="0"
55+
type="button"
5456
>
5557
<div
5658
class="css-text-1rynq56 r-color-jwli3a r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw"
5759
dir="auto"
5860
/>
59-
</div>
61+
</button>
6062
`;
6163

6264
exports[`components/Button prop "title" 1`] = `
63-
<div
65+
<button
6466
class="css-view-175oi2r r-transitionProperty-1i6wzkk r-userSelect-lrvibr r-cursor-1loqt21 r-touchAction-1otgn73 r-backgroundColor-14sbq61 r-borderRadius-1jkafct"
6567
role="button"
6668
style="transition-duration: 0s;"
67-
tabindex="0"
69+
type="button"
6870
>
6971
<div
7072
class="css-text-1rynq56 r-color-jwli3a r-fontWeight-majxgm r-padding-edyy15 r-textAlign-q4m81j r-textTransform-tsynxw"
7173
dir="auto"
7274
>
7375
Click me
7476
</div>
75-
</div>
77+
</button>
7678
`;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ exports[`components/Pressable prop "accessibilityRole" value alters HTML element
134134
`;
135135

136136
exports[`components/Pressable prop "accessibilityRole" value is "button" 1`] = `
137-
<div
137+
<button
138138
class="css-view-175oi2r r-cursor-1loqt21 r-touchAction-1otgn73"
139139
role="button"
140-
tabindex="0"
140+
type="button"
141141
/>
142142
`;
143143

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ exports[`components/Text prop "accessibilityRole" value alters HTML element 1`]
5252
`;
5353

5454
exports[`components/Text prop "accessibilityRole" value is "button" 1`] = `
55-
<div
55+
<button
5656
class="css-text-1rynq56"
5757
dir="auto"
5858
role="button"
59-
tabindex="0"
59+
type="button"
6060
/>
6161
`;
6262

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ exports[`components/View prop "accessibilityRole" value alters HTML element 1`]
4646
`;
4747

4848
exports[`components/View prop "accessibilityRole" value is "button" 1`] = `
49-
<div
49+
<button
5050
class="css-view-175oi2r"
5151
role="button"
52-
tabindex="0"
52+
type="button"
5353
/>
5454
`;
5555

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ describe('exports/createElement', () => {
471471
createElement('div', { accessibilityRole: 'button' })
472472
);
473473
expect(getAttribute(hasValue, 'role')).toBe('button');
474-
expect(getAttribute(hasValue, 'tabIndex')).toBe('0');
475474
const { container: roleIsNone } = render(
476475
createElement('div', { accessibilityRole: 'none' })
477476
);
@@ -649,11 +648,6 @@ describe('exports/createElement', () => {
649648
);
650649
expect(getAttribute(isTrueNativelyFocusable, 'tabindex')).toBeNull();
651650

652-
const { container: isFocusableRole } = render(
653-
createElement('div', { accessibilityRole: 'button', focusable: true })
654-
);
655-
expect(getAttribute(isFocusableRole, 'tabindex')).toBe('0');
656-
657651
const { container: isFalseFocusableRole } = render(
658652
createElement('div', { accessibilityRole: 'button', focusable: false })
659653
);

packages/react-native-web/src/modules/AccessibilityUtil/__tests__/propsToAccessibilityComponent-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('modules/AccessibilityUtil/propsToAccessibilityComponent', () => {
1515
test('when "accessibilityRole" is "button"', () => {
1616
expect(
1717
propsToAccessibilityComponent({ accessibilityRole: 'button' })
18-
).toBeUndefined();
18+
).toEqual('button');
1919
});
2020

2121
test('when "accessibilityRole" is "heading"', () => {

packages/react-native-web/src/modules/AccessibilityUtil/propsToAccessibilityComponent.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const roleComponents = {
1313
article: 'article',
1414
banner: 'header',
1515
blockquote: 'blockquote',
16+
button: 'button',
1617
code: 'code',
1718
complementary: 'aside',
1819
contentinfo: 'footer',
@@ -25,6 +26,7 @@ const roleComponents = {
2526
listitem: 'li',
2627
main: 'main',
2728
navigation: 'nav',
29+
paragraph: 'p',
2830
region: 'section',
2931
strong: 'strong'
3032
};

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ const createDOMProps = (elementType, props, options) => {
349349
domProps['data-testid'] = testID;
350350
}
351351

352+
if (domProps.type == null && elementType === 'button') {
353+
domProps.type = 'button';
354+
}
352355
return domProps;
353356
};
354357

0 commit comments

Comments
 (0)