@react-native-segmented-control/segmented-control 2.5.4 → 2.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -88,4 +88,17 @@
|
|
|
88
88
|
#endif
|
|
89
89
|
}
|
|
90
90
|
|
|
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
|
+
|
|
91
104
|
@end
|
|
@@ -27,6 +27,7 @@ RCT_EXPORT_VIEW_PROPERTY(momentary, BOOL)
|
|
|
27
27
|
RCT_EXPORT_VIEW_PROPERTY(enabled, BOOL)
|
|
28
28
|
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
|
|
29
29
|
RCT_EXPORT_VIEW_PROPERTY(appearance, NSString)
|
|
30
|
+
RCT_EXPORT_VIEW_PROPERTY(testIDS, NSArray)
|
|
30
31
|
|
|
31
32
|
RCT_CUSTOM_VIEW_PROPERTY(fontStyle, NSObject, RNCSegmentedControl) {
|
|
32
33
|
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
|
package/js/SegmentedControl.js
CHANGED
|
@@ -37,11 +37,13 @@ const SegmentedControl = ({
|
|
|
37
37
|
activeFontStyle,
|
|
38
38
|
appearance,
|
|
39
39
|
accessibilityHintSeperator = 'out of',
|
|
40
|
+
testIDS,
|
|
40
41
|
}: SegmentedControlProps): React.Node => {
|
|
41
42
|
const colorSchemeHook = useColorScheme();
|
|
42
43
|
const colorScheme = appearance || colorSchemeHook;
|
|
43
44
|
const [segmentWidth, setSegmentWidth] = React.useState(0);
|
|
44
45
|
const animation = React.useRef(new Animated.Value(0)).current;
|
|
46
|
+
const ref = React.useRef();
|
|
45
47
|
|
|
46
48
|
const handleChange = (index: number) => {
|
|
47
49
|
// mocks iOS's nativeEvent
|
|
@@ -55,6 +57,17 @@ const SegmentedControl = ({
|
|
|
55
57
|
onValueChange && onValueChange(values[index]);
|
|
56
58
|
};
|
|
57
59
|
|
|
60
|
+
const updateSegmentWidth = React.useCallback(
|
|
61
|
+
(width: number) => {
|
|
62
|
+
const newSegmentWidth = values.length ? width / values.length : 0;
|
|
63
|
+
if (newSegmentWidth !== segmentWidth) {
|
|
64
|
+
animation.setValue(newSegmentWidth * (selectedIndex || 0));
|
|
65
|
+
setSegmentWidth(newSegmentWidth);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
[values.length, segmentWidth, animation, selectedIndex],
|
|
69
|
+
);
|
|
70
|
+
|
|
58
71
|
React.useEffect(() => {
|
|
59
72
|
if (animation && segmentWidth) {
|
|
60
73
|
let isRTL = I18nManager.isRTL ? -segmentWidth : segmentWidth;
|
|
@@ -67,8 +80,15 @@ const SegmentedControl = ({
|
|
|
67
80
|
}
|
|
68
81
|
}, [animation, segmentWidth, selectedIndex]);
|
|
69
82
|
|
|
83
|
+
React.useEffect(() => {
|
|
84
|
+
if (ref.current) {
|
|
85
|
+
ref.current.measure((_x, _y, width) => updateSegmentWidth(width));
|
|
86
|
+
}
|
|
87
|
+
}, [values]);
|
|
88
|
+
|
|
70
89
|
return (
|
|
71
90
|
<View
|
|
91
|
+
ref={ref}
|
|
72
92
|
style={[
|
|
73
93
|
styles.default,
|
|
74
94
|
style,
|
|
@@ -80,13 +100,7 @@ const SegmentedControl = ({
|
|
|
80
100
|
nativeEvent: {
|
|
81
101
|
layout: {width},
|
|
82
102
|
},
|
|
83
|
-
}) =>
|
|
84
|
-
const newSegmentWidth = values.length ? width / values.length : 0;
|
|
85
|
-
if (newSegmentWidth !== segmentWidth) {
|
|
86
|
-
animation.setValue(newSegmentWidth * (selectedIndex || 0));
|
|
87
|
-
setSegmentWidth(newSegmentWidth);
|
|
88
|
-
}
|
|
89
|
-
}}>
|
|
103
|
+
}) => updateSegmentWidth(width)}>
|
|
90
104
|
{!backgroundColor && !tintColor && (
|
|
91
105
|
<SegmentsSeparators
|
|
92
106
|
values={values.length}
|
|
@@ -113,6 +127,9 @@ const SegmentedControl = ({
|
|
|
113
127
|
return (
|
|
114
128
|
<SegmentedControlTab
|
|
115
129
|
enabled={enabled}
|
|
130
|
+
testID={
|
|
131
|
+
(testIDS?.length ?? 0) > index ? testIDS[index] : `${index}`
|
|
132
|
+
}
|
|
116
133
|
selected={selectedIndex === index}
|
|
117
134
|
accessibilityHint={`${
|
|
118
135
|
index + 1
|
|
@@ -94,8 +94,9 @@ export const SegmentedControlTab = ({
|
|
|
94
94
|
onPress={onSelect}
|
|
95
95
|
accessibilityHint={accessibilityHint}
|
|
96
96
|
accessibilityRole="button"
|
|
97
|
-
accessibilityState={{selected: selected, disabled: !enabled}}
|
|
98
|
-
|
|
97
|
+
accessibilityState={{selected: selected, disabled: !enabled}}
|
|
98
|
+
testID={testID}>
|
|
99
|
+
<View style={styles.default}>
|
|
99
100
|
{typeof value === 'number' || typeof value === 'object' ? (
|
|
100
101
|
<Image source={value} style={styles.segmentImage} />
|
|
101
102
|
) : isBase64(value) ? (
|
package/js/types.js
CHANGED