Skip to content

Commit 7827ed7

Browse files
authored
fix(Touchable): pass role and the missing aria props to the host view
## Summary: Part of #59377. `Touchable*` build their host view props from a fixed list, and some props they accept in their types never reach it: - `role`: none of `TouchableOpacity`, `TouchableHighlight`, `TouchableNativeFeedback`, `TouchableWithoutFeedback` passes it, so `<TouchableOpacity role="button">` has no role while `accessibilityRole="button"` works. - `TouchableWithoutFeedback` drops `aria-label`. - `TouchableHighlight` drops `aria-busy`, `aria-checked`, `aria-disabled`, `aria-expanded` and `aria-selected`; the other three already map them. This change passes `role` from all four, maps `aria-label` in `TouchableWithoutFeedback` (it wins over `accessibilityLabel`, as in `View`, and the child keeps its own label when neither is set) and maps the state props in `TouchableHighlight` the same way `TouchableOpacity` does. `aria-labelledby` is left to #58173. The shared `rolePropSuite` now also runs for `TouchableNativeFeedback` and `TouchableWithoutFeedback`; before, it ran only for `Text`. It cannot run for `TouchableOpacity` and `TouchableHighlight`: Fantom reads the debug props of a view, and `AccessibilityProps::getDebugProps` does not include `role`, so `role` on their host view is checked on a device instead (pairs G / H of the reproducer). ## Changelog: [GENERAL] [FIXED] - `role` on `Touchable*`, `aria-label` on `TouchableWithoutFeedback` and `aria-*` state props on `TouchableHighlight` reach the native view ## Test Plan: - `yarn fantom packages/react-native/Libraries/Components/Touchable`: 4 suites, 272 tests passed. (Locally with `CXXFLAGS=-Wno-deprecated-literal-operator`: Xcode 27.1 clang rejects the `operator "" _json` syntax of the bundled nlohmann_json 3.11.2 under `-Werror`; unrelated to this change.) - `yarn flow-check`: no errors. `yarn eslint` on the changed files: no warnings. - On a device: the reproducer https://github.com/faciledictu/rn-aria-props-repro, pairs G–L. Before: on Android 17, G is `android.view.ViewGroup`, I's `content-desc` is the child's "×", K has no `checked`; on iOS 27, G has no Button trait, I's label is "×", K's value has no "checked". After, on iOS 27: G has the Button trait, I's label is "Close I", K's value is "checkbox, checked". After, on Android 17: G is `android.widget.Button`, I's `content-desc` is "Close I", K is `checkable="true" checked="true"`. Each matches its `accessibility*` control (H, J, L). Differential Revision: D129326374 Pull Request resolved: #59378
1 parent 3ccb28c commit 7827ed7

7 files changed

Lines changed: 191 additions & 9 deletions

File tree

‎packages/react-native/Libraries/Components/Touchable/TouchableHighlight.js‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,39 @@ class TouchableHighlightImpl extends React.Component<
222222
const {onBlur, onFocus, ...eventHandlersWithoutBlurAndFocus} =
223223
this.state.pressability.getEventHandlers();
224224

225-
const accessibilityState: ?AccessibilityState =
226-
this.props.disabled != null
227-
? {
228-
...this.props.accessibilityState,
229-
disabled: this.props.disabled,
230-
}
231-
: this.props.accessibilityState;
225+
const {
226+
accessibilityState,
227+
'aria-busy': ariaBusy,
228+
'aria-checked': ariaChecked,
229+
'aria-disabled': ariaDisabled,
230+
'aria-expanded': ariaExpanded,
231+
'aria-selected': ariaSelected,
232+
} = this.props;
233+
234+
let _accessibilityState: ?AccessibilityState;
235+
if (
236+
accessibilityState != null ||
237+
ariaBusy != null ||
238+
ariaChecked != null ||
239+
ariaDisabled != null ||
240+
ariaExpanded != null ||
241+
ariaSelected != null
242+
) {
243+
_accessibilityState = {
244+
busy: ariaBusy ?? accessibilityState?.busy,
245+
checked: ariaChecked ?? accessibilityState?.checked,
246+
disabled: ariaDisabled ?? accessibilityState?.disabled,
247+
expanded: ariaExpanded ?? accessibilityState?.expanded,
248+
selected: ariaSelected ?? accessibilityState?.selected,
249+
};
250+
}
251+
252+
if (this.props.disabled != null) {
253+
_accessibilityState = {
254+
..._accessibilityState,
255+
disabled: this.props.disabled,
256+
};
257+
}
232258

233259
const accessibilityValue = {
234260
max: this.props['aria-valuemax'] ?? this.props.accessibilityValue?.max,
@@ -251,7 +277,8 @@ class TouchableHighlightImpl extends React.Component<
251277
accessibilityHint={this.props.accessibilityHint}
252278
accessibilityLanguage={this.props.accessibilityLanguage}
253279
accessibilityRole={this.props.accessibilityRole}
254-
accessibilityState={accessibilityState}
280+
role={this.props.role}
281+
accessibilityState={_accessibilityState}
255282
accessibilityValue={accessibilityValue}
256283
accessibilityActions={this.props.accessibilityActions}
257284
onAccessibilityAction={this.props.onAccessibilityAction}

‎packages/react-native/Libraries/Components/Touchable/TouchableNativeFeedback.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ class TouchableNativeFeedback extends React.Component<
324324
accessibilityLanguage: this.props.accessibilityLanguage,
325325
accessibilityLabel: accessibilityLabel,
326326
accessibilityRole: this.props.accessibilityRole,
327+
role: this.props.role,
327328
accessibilityState: _accessibilityState,
328329
accessibilityActions: this.props.accessibilityActions,
329330
onAccessibilityAction: this.props.onAccessibilityAction,

‎packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class TouchableOpacity extends React.Component<
227227
accessibilityHint={this.props.accessibilityHint}
228228
accessibilityLanguage={this.props.accessibilityLanguage}
229229
accessibilityRole={this.props.accessibilityRole}
230+
role={this.props.role}
230231
accessibilityState={_accessibilityState}
231232
accessibilityActions={this.props.accessibilityActions}
232233
onAccessibilityAction={this.props.onAccessibilityAction}

‎packages/react-native/Libraries/Components/Touchable/TouchableWithoutFeedback.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const PASSTHROUGH_PROPS = [
150150
'onBlur',
151151
'onFocus',
152152
'onLayout',
153+
'role',
153154
'testID',
154155
] as const;
155156

@@ -281,6 +282,11 @@ export default function TouchableWithoutFeedback(
281282
}
282283
}
283284

285+
const accessibilityLabel = props['aria-label'] ?? props.accessibilityLabel;
286+
if (accessibilityLabel !== undefined) {
287+
elementProps.accessibilityLabel = accessibilityLabel;
288+
}
289+
284290
// $FlowFixMe[incompatible-type]
285291
return cloneElement(element, elementProps, ...children);
286292
}

‎packages/react-native/Libraries/Components/Touchable/__tests__/TouchableHighlight-itest.js‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,85 @@ describe('<TouchableHighlight>', () => {
5151

5252
accessibilityPropsSuite(ComponentWithAccessibilityProps);
5353

54+
describe('aria state props', () => {
55+
it('maps aria-checked to accessibilityState', () => {
56+
const root = Fantom.createRoot();
57+
58+
Fantom.runTask(() => {
59+
root.render(
60+
<TouchableHighlight aria-checked={true}>
61+
<View />
62+
</TouchableHighlight>,
63+
);
64+
});
65+
66+
expect(
67+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
68+
).toEqual(
69+
<rn-view accessibilityState="{disabled:false,selected:null,checked:Checked,busy:false,expanded:null}" />,
70+
);
71+
});
72+
73+
it('maps aria-busy, aria-expanded and aria-selected to accessibilityState', () => {
74+
const root = Fantom.createRoot();
75+
76+
Fantom.runTask(() => {
77+
root.render(
78+
<TouchableHighlight
79+
aria-busy={true}
80+
aria-expanded={true}
81+
aria-selected={true}>
82+
<View />
83+
</TouchableHighlight>,
84+
);
85+
});
86+
87+
expect(
88+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
89+
).toEqual(
90+
<rn-view accessibilityState="{disabled:false,selected:true,checked:None,busy:true,expanded:true}" />,
91+
);
92+
});
93+
94+
it('maps aria-disabled to accessibilityState', () => {
95+
const root = Fantom.createRoot();
96+
97+
Fantom.runTask(() => {
98+
root.render(
99+
<TouchableHighlight aria-disabled={true}>
100+
<View />
101+
</TouchableHighlight>,
102+
);
103+
});
104+
105+
expect(
106+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
107+
).toEqual(
108+
<rn-view accessibilityState="{disabled:true,selected:null,checked:None,busy:false,expanded:null}" />,
109+
);
110+
});
111+
112+
it('gives aria-checked precedence over accessibilityState', () => {
113+
const root = Fantom.createRoot();
114+
115+
Fantom.runTask(() => {
116+
root.render(
117+
<TouchableHighlight
118+
aria-checked={false}
119+
accessibilityState={{checked: true}}>
120+
<View />
121+
</TouchableHighlight>,
122+
);
123+
});
124+
125+
expect(
126+
root.getRenderedOutput({props: ['accessibilityState']}).toJSX(),
127+
).toEqual(
128+
<rn-view accessibilityState="{disabled:false,selected:null,checked:Unchecked,busy:false,expanded:null}" />,
129+
);
130+
});
131+
});
132+
54133
describe('style', () => {
55134
it('applies style props', () => {
56135
const root = Fantom.createRoot();

‎packages/react-native/Libraries/Components/Touchable/__tests__/TouchableNativeFeedback-itest.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13+
import type {AccessibilityProps} from 'react-native';
14+
15+
import {rolePropSuite} from '../../../../src/private/__tests__/utilities/accessibilityPropsSuite';
1316
import * as Fantom from '@react-native/fantom';
1417
import * as React from 'react';
1518
import {Text, TouchableNativeFeedback, View} from 'react-native';
@@ -19,6 +22,16 @@ describe('<TouchableNativeFeedback>', () => {
1922
expect(TouchableNativeFeedback.displayName).toBe('TouchableNativeFeedback');
2023
});
2124

25+
component ComponentWithAccessibilityProps(...props: AccessibilityProps) {
26+
return (
27+
<TouchableNativeFeedback {...props}>
28+
<Text>Touchable</Text>
29+
</TouchableNativeFeedback>
30+
);
31+
}
32+
33+
rolePropSuite(ComponentWithAccessibilityProps);
34+
2235
describe('rendering', () => {
2336
it('renders its child as pressable', () => {
2437
const root = Fantom.createRoot();

‎packages/react-native/Libraries/Components/Touchable/__tests__/TouchableWithoutFeedback-itest.js‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
import type {AccessibilityProps} from 'react-native';
1414

15-
import accessibilityPropsSuite from '../../../../src/private/__tests__/utilities/accessibilityPropsSuite';
15+
import accessibilityPropsSuite, {
16+
rolePropSuite,
17+
} from '../../../../src/private/__tests__/utilities/accessibilityPropsSuite';
1618
import * as Fantom from '@react-native/fantom';
1719
import nullthrows from 'nullthrows';
1820
import * as React from 'react';
@@ -47,6 +49,59 @@ describe('<TouchableWithoutFeedback>', () => {
4749
}
4850

4951
accessibilityPropsSuite(ComponentWithAccessibilityProps);
52+
rolePropSuite(ComponentWithAccessibilityProps);
53+
54+
describe('aria-label', () => {
55+
it('is mapped to accessibilityLabel', () => {
56+
const root = Fantom.createRoot();
57+
58+
Fantom.runTask(() => {
59+
root.render(
60+
<TouchableWithoutFeedback aria-label="Close">
61+
<View />
62+
</TouchableWithoutFeedback>,
63+
);
64+
});
65+
66+
expect(
67+
root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(),
68+
).toEqual(<rn-view accessibilityLabel="Close" />);
69+
});
70+
71+
it('takes precedence over accessibilityLabel', () => {
72+
const root = Fantom.createRoot();
73+
74+
Fantom.runTask(() => {
75+
root.render(
76+
<TouchableWithoutFeedback
77+
aria-label="aria value"
78+
accessibilityLabel="native value">
79+
<View />
80+
</TouchableWithoutFeedback>,
81+
);
82+
});
83+
84+
expect(
85+
root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(),
86+
).toEqual(<rn-view accessibilityLabel="aria value" />);
87+
});
88+
89+
it("keeps the child's own label when neither is set", () => {
90+
const root = Fantom.createRoot();
91+
92+
Fantom.runTask(() => {
93+
root.render(
94+
<TouchableWithoutFeedback>
95+
<View accessibilityLabel="child label" />
96+
</TouchableWithoutFeedback>,
97+
);
98+
});
99+
100+
expect(
101+
root.getRenderedOutput({props: ['accessibilityLabel']}).toJSX(),
102+
).toEqual(<rn-view accessibilityLabel="child label" />);
103+
});
104+
});
50105

51106
describe('disabled', () => {
52107
it('sets accessibilityState disabled to true', () => {

0 commit comments

Comments
 (0)