Skip to content

Commit f353119

Browse files
Daksh Bhardwajfacebook-github-bot
Daksh Bhardwaj
authored andcommitted
feat : add aria labelled as alias for accessibilityLabelledBy (#34725)
Summary: This adds the [aria-labelledby](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby) prop to the components where it's used as requested on #34424, equivalent [accessibilityLabelledBy](https://reactnative.dev/docs/accessibility#accessibilitylabelledby-android) ## Changelog [General] [Added] - Add aria-modal prop to basic component ## TestPlan - Enable talkback. - Open the RNTester app and navigate to the Api's tab - Go to the TextInput with aria-labelledby attribute section <img width="495" alt="Screenshot 2022-09-19 at 7 46 05 PM" src="https://user-images.githubusercontent.com/22423684/191038924-017dba93-ea7d-494d-ba6f-161e986f9b54.png"> Pull Request resolved: #34725 Reviewed By: lunaleaps Differential Revision: D40176143 Pulled By: lunaleaps fbshipit-source-id: 003d1ab27bfd01b5c6d4c58a4de501ec7966359d
1 parent 38fcafe commit f353119

File tree

7 files changed

+69
-21
lines changed

7 files changed

+69
-21
lines changed

Libraries/Components/TextInput/TextInput.js

+3
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,8 @@ function InternalTextInput(props: Props): React.Node {
14381438
} else if (Platform.OS === 'android') {
14391439
const style = [props.style];
14401440
const autoCapitalize = props.autoCapitalize || 'sentences';
1441+
const _accessibilityLabelledBy =
1442+
props?.['aria-labelledby'] ?? props?.accessibilityLabelledBy;
14411443
const placeholder = props.placeholder ?? '';
14421444
let children = props.children;
14431445
const childCount = React.Children.count(children);
@@ -1464,6 +1466,7 @@ function InternalTextInput(props: Props): React.Node {
14641466
{...eventHandlers}
14651467
accessible={accessible}
14661468
accessibilityState={_accessibilityState}
1469+
accessibilityLabelledBy={_accessibilityLabelledBy}
14671470
autoCapitalize={autoCapitalize}
14681471
submitBehavior={submitBehavior}
14691472
caretHidden={caretHidden}

Libraries/Components/View/View.js

+28-20
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,25 @@ const View: React.AbstractComponent<
3131
(
3232
{
3333
accessibilityElementsHidden,
34-
accessibilityLiveRegion,
35-
'aria-live': ariaLive,
3634
accessibilityLabel,
35+
accessibilityLabelledBy,
36+
accessibilityLiveRegion,
3737
accessibilityRole,
38-
'aria-label': ariaLabel,
38+
accessibilityState,
39+
accessibilityValue,
40+
'aria-busy': ariaBusy,
41+
'aria-checked': ariaChecked,
42+
'aria-disabled': ariaDisabled,
43+
'aria-expanded': ariaExpanded,
3944
'aria-hidden': ariaHidden,
45+
'aria-label': ariaLabel,
46+
'aria-labelledby': ariaLabelledBy,
47+
'aria-live': ariaLive,
48+
'aria-selected': ariaSelected,
49+
'aria-valuemax': ariaValueMax,
50+
'aria-valuemin': ariaValueMin,
51+
'aria-valuenow': ariaValueNow,
52+
'aria-valuetext': ariaValueText,
4053
focusable,
4154
id,
4255
importantForAccessibility,
@@ -49,14 +62,8 @@ const View: React.AbstractComponent<
4962
}: ViewProps,
5063
forwardedRef,
5164
) => {
52-
const {
53-
accessibilityState,
54-
'aria-busy': ariaBusy,
55-
'aria-checked': ariaChecked,
56-
'aria-disabled': ariaDisabled,
57-
'aria-expanded': ariaExpanded,
58-
'aria-selected': ariaSelected,
59-
} = otherProps;
65+
const _accessibilityLabelledBy =
66+
ariaLabelledBy?.split(/\s*,\s*/g) ?? accessibilityLabelledBy;
6067

6168
const _accessibilityState = {
6269
busy: ariaBusy ?? accessibilityState?.busy,
@@ -66,6 +73,13 @@ const View: React.AbstractComponent<
6673
selected: ariaSelected ?? accessibilityState?.selected,
6774
};
6875

76+
const _accessibilityValue = {
77+
max: ariaValueMax ?? accessibilityValue?.max,
78+
min: ariaValueMin ?? accessibilityValue?.min,
79+
now: ariaValueNow ?? accessibilityValue?.now,
80+
text: ariaValueText ?? accessibilityValue?.text,
81+
};
82+
6983
// Map role values to AccessibilityRole values
7084
const roleToAccessibilityRoleMapping = {
7185
alert: 'alert',
@@ -134,20 +148,13 @@ const View: React.AbstractComponent<
134148
treeitem: undefined,
135149
};
136150

137-
const accessibilityValue = {
138-
max: otherProps['aria-valuemax'] ?? otherProps.accessibilityValue?.max,
139-
min: otherProps['aria-valuemin'] ?? otherProps.accessibilityValue?.min,
140-
now: otherProps['aria-valuenow'] ?? otherProps.accessibilityValue?.now,
141-
text: otherProps['aria-valuetext'] ?? otherProps.accessibilityValue?.text,
142-
};
143-
const restWithDefaultProps = {...otherProps, accessibilityValue};
144-
145151
const flattenedStyle = flattenStyle(style);
146152
const newPointerEvents = flattenedStyle?.pointerEvents || pointerEvents;
147153

148154
return (
149155
<TextAncestor.Provider value={false}>
150156
<ViewNativeComponent
157+
{...otherProps}
151158
accessibilityLiveRegion={
152159
ariaLive === 'off' ? 'none' : ariaLive ?? accessibilityLiveRegion
153160
}
@@ -160,13 +167,14 @@ const View: React.AbstractComponent<
160167
accessibilityElementsHidden={
161168
ariaHidden ?? accessibilityElementsHidden
162169
}
170+
accessibilityLabelledBy={_accessibilityLabelledBy}
171+
accessibilityValue={_accessibilityValue}
163172
importantForAccessibility={
164173
ariaHidden === true
165174
? 'no-hide-descendants'
166175
: importantForAccessibility
167176
}
168177
nativeID={id ?? nativeID}
169-
{...restWithDefaultProps}
170178
style={style}
171179
pointerEvents={newPointerEvents}
172180
ref={forwardedRef}

Libraries/Components/View/ViewPropTypes.js

+7
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,13 @@ export type ViewProps = $ReadOnly<{|
542542
*/
543543
'aria-hidden'?: ?boolean,
544544

545+
/**
546+
* It reperesents the nativeID of the associated label text. When the assistive technology focuses on the component with this props, the text is read aloud.
547+
*
548+
* @platform android
549+
*/
550+
'aria-labelledby'?: ?string,
551+
545552
/**
546553
* Views that are only used to layout their children or otherwise don't draw
547554
* anything may be automatically removed from the native hierarchy as an

Libraries/Image/Image.android.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ const BaseImage = (props: ImagePropsType, forwardedRef) => {
179179
? loadingIndicatorSource.uri
180180
: null,
181181
ref: forwardedRef,
182-
accessible: props.alt !== undefined ? true : props.accessible,
183182
accessibilityLabel:
184183
props['aria-label'] ?? props.accessibilityLabel ?? props.alt,
184+
accessibilityLabelledBy:
185+
props?.['aria-labelledby'] ?? props?.accessibilityLabelledBy,
186+
accessible: props.alt !== undefined ? true : props.accessible,
185187
accessibilityState: {
186188
busy: props['aria-busy'] ?? props.accessibilityState?.busy,
187189
checked: props['aria-checked'] ?? props.accessibilityState?.checked,

Libraries/Image/ImageProps.js

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ export type ImageProps = {|
9292
*/
9393
'aria-label'?: ?Stringish,
9494

95+
/**
96+
* Reperesents the nativeID of the associated label. When the assistive technology focuses on the component with this props.
97+
*
98+
* @platform android
99+
*/
100+
'aria-labelledby'?: ?string,
95101
/**
96102
* The text that's read by the screen reader when the user interacts with
97103
* the image.

Libraries/Text/TextProps.js

+8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ export type TextProps = $ReadOnly<{|
8989
'aria-disabled'?: ?boolean,
9090
'aria-expanded'?: ?boolean,
9191
'aria-selected'?: ?boolean,
92+
93+
/**
94+
* Reperesents the nativeID of the associated label text. When the assistive technology focuses on the component with this props, the text is read aloud.
95+
*
96+
* @platform android
97+
*/
98+
'aria-labelledby'?: ?string,
99+
92100
children?: ?Node,
93101

94102
/**

packages/rn-tester/js/examples/Accessibility/AccessibilityExample.js

+14
Original file line numberDiff line numberDiff line change
@@ -1446,4 +1446,18 @@ exports.examples = [
14461446
);
14471447
},
14481448
},
1449+
{
1450+
title: 'TextInput with aria-labelledby attribute"',
1451+
render(): React.Element<typeof View> {
1452+
return (
1453+
<View>
1454+
<Text nativeID="testAriaLabelledBy">Phone Number</Text>
1455+
<TextInput
1456+
aria-labelledby={'testAriaLabelledBy'}
1457+
style={styles.default}
1458+
/>
1459+
</View>
1460+
);
1461+
},
1462+
},
14491463
];

0 commit comments

Comments
 (0)