Skip to content

Commit f773419

Browse files
feat: add testIDs for android with automation supports
1 parent 6801a6d commit f773419

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/OtpInput/OtpInput.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
2323
theme = {},
2424
textInputProps,
2525
textProps,
26+
buttonProps,
2627
type = "numeric",
2728
} = props;
2829
const {
@@ -82,7 +83,13 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
8283
disabled={disabled}
8384
onPress={handlePress}
8485
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
85-
testID="otp-input"
86+
{...buttonProps}
87+
testID={buttonProps?.testID ? `${buttonProps.testID}${index}` : `otp-input-${index}`}
88+
{...(Platform.OS === "android" && {
89+
accessibilityLabel: buttonProps?.testID
90+
? `${buttonProps.testID}${index}`
91+
: `otp-input-${index}`,
92+
})}
8693
>
8794
{isFocusedInput && !hideStick ? (
8895
<VerticalStick
@@ -93,7 +100,12 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
93100
) : (
94101
<Text
95102
{...textProps}
96-
testID={textProps?.testID ? `${textProps.testID}-${index}` : undefined}
103+
testID={textProps?.testID ? `${textProps.testID}${index}` : undefined}
104+
{...(Platform.OS === "android" && {
105+
accessibilityLabel: textProps?.testID
106+
? `${textProps.testID}${index}`
107+
: `otp-input-text-${index}`,
108+
})}
97109
style={[
98110
styles.codeText,
99111
pinCodeTextStyle,
@@ -120,6 +132,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
120132
aria-disabled={disabled}
121133
editable={!disabled}
122134
testID="otp-input-hidden"
135+
{...(Platform.OS === "android" && { accessibilityLabel: "otp-input-hidden" })}
123136
onFocus={handleFocus}
124137
onBlur={handleBlur}
125138
caretHidden={Platform.OS === "ios"}

src/OtpInput/OtpInput.types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { ColorValue, TextInputProps, TextProps, TextStyle, ViewStyle } from "react-native";
1+
import {
2+
ColorValue,
3+
TextInputProps,
4+
TextProps,
5+
TextStyle,
6+
ViewStyle,
7+
PressableProps,
8+
} from "react-native";
29

310
export interface OtpInputProps {
411
numberOfDigits?: number;
@@ -16,6 +23,7 @@ export interface OtpInputProps {
1623
disabled?: boolean;
1724
textInputProps?: TextInputProps;
1825
textProps?: TextProps;
26+
buttonProps?: PressableProps;
1927
type?: "alpha" | "numeric" | "alphanumeric";
2028
placeholder?: string;
2129
}

src/OtpInput/VerticalStick.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import { memo, useEffect, useRef } from "react";
3-
import { Animated, ColorValue, View, ViewStyle } from "react-native";
3+
import { Animated, ColorValue, View, ViewStyle, Platform } from "react-native";
44
import { styles } from "./OtpInput.styles";
55

66
interface VerticalStickProps {
@@ -38,6 +38,9 @@ export const VerticalStick: React.FC<VerticalStickProps> = memo(
3838
<View
3939
style={[styles.stick, focusColor ? { backgroundColor: focusColor } : {}, style]}
4040
testID="otp-input-stick"
41+
{...(Platform.OS === "android" && {
42+
accessibilityLabel: "otp-input-stick",
43+
})}
4144
/>
4245
</Animated.View>
4346
);

0 commit comments

Comments
 (0)