@react-native-segmented-control/segmented-control 2.5.4 → 2.5.6

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.
@@ -11,5 +11,6 @@
11
11
  @interface RNCSegmentedControl : UISegmentedControl
12
12
  @property(nonatomic, assign) NSInteger selectedIndex;
13
13
  @property(nonatomic, copy) RCTBubblingEventBlock onChange;
14
+ @property(nonatomic, copy) NSArray *testIDS;
14
15
 
15
16
  @end
@@ -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) && \
@@ -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, updateSegmentWidth]);
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
@@ -13,6 +13,7 @@ import {
13
13
  TouchableOpacity,
14
14
  View,
15
15
  useColorScheme,
16
+ Platform,
16
17
  } from 'react-native';
17
18
 
18
19
  import type {FontStyle, ViewStyle} from './types';
@@ -94,8 +95,14 @@ export const SegmentedControlTab = ({
94
95
  onPress={onSelect}
95
96
  accessibilityHint={accessibilityHint}
96
97
  accessibilityRole="button"
97
- accessibilityState={{selected: selected, disabled: !enabled}}>
98
- <View testID={testID} style={styles.default}>
98
+ accessibilityState={{selected: selected, disabled: !enabled}}
99
+ testID={testID}
100
+ accessible={true}
101
+ accessibilityLabel={Platform.select({
102
+ android: testID,
103
+ ios: typeof value === 'string' ? value : testID,
104
+ })}>
105
+ <View style={styles.default}>
99
106
  {typeof value === 'number' || typeof value === 'object' ? (
100
107
  <Image source={value} style={styles.segmentImage} />
101
108
  ) : isBase64(value) ? (
package/js/types.js CHANGED
@@ -107,4 +107,9 @@ export type SegmentedControlProps = $ReadOnly<{|
107
107
  * Touchable style properties for Segmented Control Tab
108
108
  */
109
109
  tabStyle?: ViewStyle,
110
+
111
+ /**
112
+ * array testID to each segment button
113
+ */
114
+ testIDS: $ReadOnlyArray<string>,
110
115
  |}>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-segmented-control/segmented-control",
3
- "version": "2.5.4",
3
+ "version": "2.5.6",
4
4
  "description": "React Native SegmentedControlIOS library",
5
5
  "main": "js/index.js",
6
6
  "types": "index.d.ts",