Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Added testIDS prop which can be used to pass testID to individual segment buttons #894

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ios/RNCSegmentedControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
@interface RNCSegmentedControl : UISegmentedControl
@property(nonatomic, assign) NSInteger selectedIndex;
@property(nonatomic, copy) RCTBubblingEventBlock onChange;
@property(nonatomic, copy) NSArray *testIDS;

@end
13 changes: 13 additions & 0 deletions ios/RNCSegmentedControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ - (void)setAppearance:(NSString *)appearanceString {
#endif
}

- (NSArray *)accessibilityElements {
NSArray *elements = [super accessibilityElements];
[elements enumerateObjectsUsingBlock:^(UIView *obj, NSUInteger idx, BOOL *stop) {
@try {
obj.accessibilityIdentifier = self.testIDS[idx];
} @catch (NSException *exception) {
NSLog(@"%@", exception);
}
}];

return elements;
}

@end
1 change: 1 addition & 0 deletions ios/RNCSegmentedControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ - (UIView *)view {
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(appearance, NSString)
RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray)

RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) {
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
Expand Down
2 changes: 2 additions & 0 deletions js/SegmentedControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
activeFontStyle,
appearance,
accessibilityHintSeperator = 'out of',
testIDS,
}: SegmentedControlProps): React.Node => {
const colorSchemeHook = useColorScheme();
const colorScheme = appearance || colorSchemeHook;
Expand Down Expand Up @@ -97,7 +98,7 @@
<Animated.View
style={[
styles.slider,
{

Check warning on line 101 in js/SegmentedControl.js

View workflow job for this annotation

GitHub Actions / lint (20)

Inline style: { zIndex: -1 }
transform: [{translateX: animation}],
width: segmentWidth - 4,
zIndex: -1,
Expand All @@ -113,6 +114,7 @@
return (
<SegmentedControlTab
enabled={enabled}
testID={(testIDS?.length ?? 0) > index ? testIDS[index] : `${index}`}

Check failure on line 117 in js/SegmentedControl.js

View workflow job for this annotation

GitHub Actions / lint (20)

Replace `(testIDS?.length·??·0)·>·index·?·testIDS[index]·:·`${index}`` with `⏎··················(testIDS?.length·??·0)·>·index·?·testIDS[index]·:·`${index}`⏎················`
selected={selectedIndex === index}
accessibilityHint={`${
index + 1
Expand Down
5 changes: 3 additions & 2 deletions js/SegmentedControlTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ export const SegmentedControlTab = ({
onPress={onSelect}
accessibilityHint={accessibilityHint}
accessibilityRole="button"
accessibilityState={{selected: selected, disabled: !enabled}}>
<View testID={testID} style={styles.default}>
accessibilityState={{selected: selected, disabled: !enabled}}
testID={testID}>
<View style={styles.default}>
{typeof value === 'number' || typeof value === 'object' ? (
<Image source={value} style={styles.segmentImage} />
) : isBase64(value) ? (
Expand Down
6 changes: 6 additions & 0 deletions js/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,10 @@
* Touchable style properties for Segmented Control Tab
*/
tabStyle?: ViewStyle,


Check failure on line 111 in js/types.js

View workflow job for this annotation

GitHub Actions / lint (20)

Delete `⏎`
/**
* array testID to each segment button
*/
testIDS: $ReadOnlyArray<string | number | Object>,
|}>;
Loading