Skip to content

Commit 4b82094

Browse files
committed
feat: add accessibilityLabelledBy
1 parent ebe5417 commit 4b82094

File tree

11 files changed

+66
-0
lines changed

11 files changed

+66
-0
lines changed

Libraries/Components/View/ViewPropTypes.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ export type ViewProps = $ReadOnly<{|
432432
*/
433433
accessibilityActions?: ?$ReadOnlyArray<AccessibilityActionInfo>,
434434

435+
/**
436+
* Specifies the nativeID of the associated label text. When the assistive technology focuses on the component with this props, the text is read aloud.
437+
*
438+
* @platform android
439+
*/
440+
accessibilityLabelledBy?: ?string,
441+
435442
/**
436443
* Views that are only used to layout their children or otherwise don't draw
437444
* anything may be automatically removed from the native hierarchy as an

Libraries/Text/Text.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const Text: React.AbstractComponent<
2929
> = React.forwardRef((props: TextProps, forwardedRef) => {
3030
const {
3131
accessible,
32+
accessibilityLabelledBy,
3233
allowFontScaling,
3334
ellipsizeMode,
3435
onLongPress,
@@ -179,6 +180,7 @@ const Text: React.AbstractComponent<
179180
numberOfLines={numberOfLines}
180181
selectionColor={selectionColor}
181182
style={style}
183+
accessibilityLabelledBy={accessibilityLabelledBy}
182184
ref={forwardedRef}
183185
/>
184186
</TextAncestor.Provider>

Libraries/Text/TextProps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,6 @@ export type TextProps = $ReadOnly<{|
199199
* See https://reactnative.dev/docs/text.html#supperhighlighting
200200
*/
201201
suppressHighlighting?: ?boolean,
202+
203+
accessibilityLabelledBy?: ?string,
202204
|}>;

ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ public void setNativeId(@NonNull T view, @Nullable String nativeId) {
137137
ReactFindViewUtil.notifyViewRendered(view);
138138
}
139139

140+
@Override
141+
@ReactProp(name = ViewProps.ACCESSIBILITY_LABELLED_BY)
142+
public void setAccessibilityLabelledBy(@NonNull T view, String nativeId) {
143+
view.setTag(R.id.labelled_by, nativeId);
144+
}
145+
140146
@Override
141147
@ReactProp(name = ViewProps.ACCESSIBILITY_LABEL)
142148
public void setAccessibilityLabel(@NonNull T view, @Nullable String accessibilityLabel) {

ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManagerAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public void setImportantForAccessibility(
6464
@Override
6565
public void setNativeId(@NonNull T view, String nativeId) {}
6666

67+
@Override
68+
public void setAccessibilityLabelledBy(@NonNull T view, String nativeId) {}
69+
6770
@Override
6871
public void setOpacity(@NonNull T view, float opacity) {}
6972

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactAccessibilityDelegate.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.facebook.react.uimanager.events.Event;
3737
import com.facebook.react.uimanager.events.EventDispatcher;
3838
import java.util.HashMap;
39+
import com.facebook.react.uimanager.util.ReactFindViewUtil;
3940

4041
/**
4142
* Utility class that handles the addition of a "role" for accessibility to either a View or
@@ -200,6 +201,24 @@ public void onInitializeAccessibilityNodeInfo(View host, AccessibilityNodeInfoCo
200201
setRole(info, accessibilityRole, host.getContext());
201202
}
202203

204+
final Object accessibilityLabelledBy = host.getTag(R.id.labelled_by);
205+
if (accessibilityLabelledBy != null) {
206+
ReactFindViewUtil.findView(
207+
host.getRootView(),
208+
new ReactFindViewUtil.OnViewFoundListener() {
209+
@Override
210+
public String getNativeId() {
211+
return (String) accessibilityLabelledBy;
212+
}
213+
214+
@Override
215+
public void onViewFound(View view) {
216+
info.setLabeledBy(view);
217+
}
218+
}
219+
);
220+
}
221+
203222
// state is changeable.
204223
final ReadableMap accessibilityState = (ReadableMap) host.getTag(R.id.accessibility_state);
205224
if (accessibilityState != null) {

ReactAndroid/src/main/java/com/facebook/react/uimanager/interfaces/BaseViewManagerDelegate.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public void setProperty(T view, String propName, @Nullable Object value) {
8484
case ViewProps.NATIVE_ID:
8585
mViewManager.setNativeId(view, (String) value);
8686
break;
87+
case ViewProps.ACCESSIBILITY_LABELLED_BY:
88+
mViewManager.setAccessibilityLabelledBy(view, (String) value);
89+
break;
8790
case ViewProps.OPACITY:
8891
mViewManager.setOpacity(view, value == null ? 1.0f : ((Double) value).floatValue());
8992
break;

ReactAndroid/src/main/java/com/facebook/react/uimanager/interfaces/BaseViewManagerInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public interface BaseViewManagerInterface<T extends View> {
4949

5050
void setNativeId(T view, @Nullable String nativeId);
5151

52+
void setAccessibilityLabelledBy(T view, @Nullable String nativeId);
53+
5254
void setOpacity(T view, float opacity);
5355

5456
void setRenderToHardwareTexture(T view, boolean useHWTexture);

ReactAndroid/src/main/java/com/facebook/react/uimanager/interfaces/ViewProps.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ public class ViewProps {
152152
public static final String ACCESSIBILITY_STATE = "accessibilityState";
153153
public static final String ACCESSIBILITY_ACTIONS = "accessibilityActions";
154154
public static final String ACCESSIBILITY_VALUE = "accessibilityValue";
155+
public static final String ACCESSIBILITY_LABELLED_BY = "accessibilityLabelledBy";
155156
public static final String IMPORTANT_FOR_ACCESSIBILITY = "importantForAccessibility";
156157

157158
// DEPRECATED

ReactAndroid/src/main/res/views/uimanager/values/ids.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,7 @@
2727
<!--tag is used to store accessibilityValue tag -->
2828
<item type="id" name="accessibility_value"/>
2929

30+
<!--tag is used to store accessibilityLabelledBy tag -->
31+
<item type="id" name="labelled_by"/>
32+
3033
</resources>

0 commit comments

Comments
 (0)