Skip to content

Commit 26fc8e3

Browse files
authored
done with adding testIDS (#894)
1 parent 6b06902 commit 26fc8e3

6 files changed

+26
-2
lines changed

ios/RNCSegmentedControl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
@interface RNCSegmentedControl : UISegmentedControl
1212
@property(nonatomic, assign) NSInteger selectedIndex;
1313
@property(nonatomic, copy) RCTBubblingEventBlock onChange;
14+
@property(nonatomic, copy) NSArray *testIDS;
1415

1516
@end

ios/RNCSegmentedControl.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,17 @@ - (void)setAppearance:(NSString *)appearanceString {
8888
#endif
8989
}
9090

91+
- (NSArray *)accessibilityElements {
92+
NSArray *elements = [super accessibilityElements];
93+
[elements enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
94+
@try {
95+
obj.accessibilityIdentifier = self.testIDS[idx];
96+
} @catch (NSException *exception) {
97+
NSLog(@"%@", exception);
98+
}
99+
}];
100+
101+
return elements;
102+
}
103+
91104
@end

ios/RNCSegmentedControlManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ - (UIView *)view {
2727
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
2828
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
2929
RCT_EXPORT_VIEW_PROPERTY(appearance, NSString)
30+
RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray)
3031

3132
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) {
3233
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \

js/SegmentedControl.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const SegmentedControl = ({
3737
activeFontStyle,
3838
appearance,
3939
accessibilityHintSeperator = 'out of',
40+
testIDS,
4041
}: SegmentedControlProps): React.Node => {
4142
const colorSchemeHook = useColorScheme();
4243
const colorScheme = appearance || colorSchemeHook;
@@ -113,6 +114,7 @@ const SegmentedControl = ({
113114
return (
114115
<SegmentedControlTab
115116
enabled={enabled}
117+
testID={(testIDS?.length ?? 0) > index ? testIDS[index] : `${index}`}
116118
selected={selectedIndex === index}
117119
accessibilityHint={`${
118120
index + 1

js/SegmentedControlTab.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ export const SegmentedControlTab = ({
9494
onPress={onSelect}
9595
accessibilityHint={accessibilityHint}
9696
accessibilityRole="button"
97-
accessibilityState={{selected: selected, disabled: !enabled}}>
98-
<View testID={testID} style={styles.default}>
97+
accessibilityState={{selected: selected, disabled: !enabled}}
98+
testID={testID}>
99+
<View style={styles.default}>
99100
{typeof value === 'number' || typeof value === 'object' ? (
100101
<Image source={value} style={styles.segmentImage} />
101102
) : isBase64(value) ? (

js/types.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,10 @@ export type SegmentedControlProps = $ReadOnly<{|
107107
* Touchable style properties for Segmented Control Tab
108108
*/
109109
tabStyle?: ViewStyle,
110+
111+
112+
/**
113+
* array testID to each segment button
114+
*/
115+
testIDS: $ReadOnlyArray<string | number | Object>,
110116
|}>;

0 commit comments

Comments
 (0)