Skip to content

Commit a2119f8

Browse files
authored
Fix/ SegmentedControl fixes (#1369)
* pass testID * Fix animation leap
1 parent e918dea commit a2119f8

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

generatedTypes/components/segmentedControl/segment.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export declare type SegmentProps = SegmentedControlItemProps & {
4343
* onLayout function.
4444
*/
4545
onLayout?: (index: number, event: LayoutChangeEvent) => void;
46+
testID?: string;
4647
};
4748
declare const _default: React.ComponentClass<SegmentedControlItemProps & {
4849
/**
@@ -69,6 +70,7 @@ declare const _default: React.ComponentClass<SegmentedControlItemProps & {
6970
* onLayout function.
7071
*/
7172
onLayout?: ((index: number, event: LayoutChangeEvent) => void) | undefined;
73+
testID?: string | undefined;
7274
} & {
7375
useCustomTheme?: boolean | undefined;
7476
}, any>;

src/components/segmentedControl/index.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ const SegmentedControl = (props: SegmentedControlProps) => {
8484
activeBackgroundColor = Colors.white,
8585
inactiveColor = Colors.grey20,
8686
outlineColor = activeColor,
87-
outlineWidth = BORDER_WIDTH
87+
outlineWidth = BORDER_WIDTH,
88+
testID
8889
} = props;
8990
const [selectedSegment, setSelectedSegment] = useState(-1);
9091

@@ -114,7 +115,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
114115
const onLayout = useCallback((index: number, event: LayoutChangeEvent) => {
115116
const {x, width, height} = event.nativeEvent.layout;
116117
segmentsStyle.current[index] = {x, width};
117-
segmentedControlHeight.current = height + (2 * (HORIZONTAL_PADDING - BORDER_WIDTH));
118+
segmentedControlHeight.current = height + 2 * (HORIZONTAL_PADDING - BORDER_WIDTH);
118119
segmentsCounter.current++;
119120

120121
return segmentsCounter.current === segments?.length && setSelectedSegment(initialIndex);
@@ -150,20 +151,29 @@ const SegmentedControl = (props: SegmentedControlProps) => {
150151
activeColor={activeColor}
151152
inactiveColor={inactiveColor}
152153
{...segments?.[index]}
154+
testID={testID}
153155
/>
154156
);
155157
});
156158

157159
return (
158-
<View style={containerStyle}>
160+
<View style={containerStyle} testID={testID}>
159161
<View row center style={[styles.container, style, {borderRadius, backgroundColor}]}>
160-
<Reanimated.View
161-
style={[
162-
styles.selectedSegment,
163-
animatedStyle,
164-
{borderColor: outlineColor, borderRadius, backgroundColor: activeBackgroundColor, borderWidth: outlineWidth, height: segmentedControlHeight.current}
165-
]}
166-
/>
162+
{animatedStyle && (
163+
<Reanimated.View
164+
style={[
165+
styles.selectedSegment,
166+
animatedStyle,
167+
{
168+
borderColor: outlineColor,
169+
borderRadius,
170+
backgroundColor: activeBackgroundColor,
171+
borderWidth: outlineWidth,
172+
height: segmentedControlHeight.current
173+
}
174+
]}
175+
/>
176+
)}
167177
{renderSegments()}
168178
</View>
169179
</View>

src/components/segmentedControl/segment.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type SegmentProps = SegmentedControlItemProps & {
5050
* onLayout function.
5151
*/
5252
onLayout?: (index: number, event: LayoutChangeEvent) => void;
53+
testID?: string;
5354
};
5455

5556
/**
@@ -66,7 +67,8 @@ const Segment = React.memo((props: SegmentProps) => {
6667
onPress,
6768
inactiveColor,
6869
index,
69-
iconOnRight
70+
iconOnRight,
71+
testID
7072
} = props;
7173

7274
const segmentedColor = useMemo(() => (isSelected ? activeColor : inactiveColor),
@@ -88,7 +90,15 @@ const Segment = React.memo((props: SegmentProps) => {
8890
[onLayout, index]);
8991

9092
return (
91-
<TouchableOpacity onLayout={segmentOnLayout} style={segmentStyle} onPress={onSegmentPress} row flexG center>
93+
<TouchableOpacity
94+
onLayout={segmentOnLayout}
95+
style={segmentStyle}
96+
onPress={onSegmentPress}
97+
row
98+
flexG
99+
center
100+
testID={`${testID}.${index}`}
101+
>
92102
{!iconOnRight && renderIcon()}
93103
{label && (
94104
<Text text90 numberOfLines={1} color={segmentedColor}>

0 commit comments

Comments
 (0)