Skip to content

Fix/ SegmentedControl fixes #1369

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 20, 2021
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
2 changes: 2 additions & 0 deletions generatedTypes/components/segmentedControl/segment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export declare type SegmentProps = SegmentedControlItemProps & {
* onLayout function.
*/
onLayout?: (index: number, event: LayoutChangeEvent) => void;
testID?: string;
};
declare const _default: React.ComponentClass<SegmentedControlItemProps & {
/**
Expand All @@ -69,6 +70,7 @@ declare const _default: React.ComponentClass<SegmentedControlItemProps & {
* onLayout function.
*/
onLayout?: ((index: number, event: LayoutChangeEvent) => void) | undefined;
testID?: string | undefined;
} & {
useCustomTheme?: boolean | undefined;
}, any>;
Expand Down
30 changes: 20 additions & 10 deletions src/components/segmentedControl/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ const SegmentedControl = (props: SegmentedControlProps) => {
activeBackgroundColor = Colors.white,
inactiveColor = Colors.grey20,
outlineColor = activeColor,
outlineWidth = BORDER_WIDTH
outlineWidth = BORDER_WIDTH,
testID
} = props;
const [selectedSegment, setSelectedSegment] = useState(-1);

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

return segmentsCounter.current === segments?.length && setSelectedSegment(initialIndex);
Expand Down Expand Up @@ -150,20 +151,29 @@ const SegmentedControl = (props: SegmentedControlProps) => {
activeColor={activeColor}
inactiveColor={inactiveColor}
{...segments?.[index]}
testID={testID}
/>
);
});

return (
<View style={containerStyle}>
<View style={containerStyle} testID={testID}>
<View row center style={[styles.container, style, {borderRadius, backgroundColor}]}>
<Reanimated.View
style={[
styles.selectedSegment,
animatedStyle,
{borderColor: outlineColor, borderRadius, backgroundColor: activeBackgroundColor, borderWidth: outlineWidth, height: segmentedControlHeight.current}
]}
/>
{animatedStyle && (
<Reanimated.View
style={[
styles.selectedSegment,
animatedStyle,
{
borderColor: outlineColor,
borderRadius,
backgroundColor: activeBackgroundColor,
borderWidth: outlineWidth,
height: segmentedControlHeight.current
}
]}
/>
)}
{renderSegments()}
</View>
</View>
Expand Down
14 changes: 12 additions & 2 deletions src/components/segmentedControl/segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export type SegmentProps = SegmentedControlItemProps & {
* onLayout function.
*/
onLayout?: (index: number, event: LayoutChangeEvent) => void;
testID?: string;
};

/**
Expand All @@ -66,7 +67,8 @@ const Segment = React.memo((props: SegmentProps) => {
onPress,
inactiveColor,
index,
iconOnRight
iconOnRight,
testID
} = props;

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

return (
<TouchableOpacity onLayout={segmentOnLayout} style={segmentStyle} onPress={onSegmentPress} row flexG center>
<TouchableOpacity
onLayout={segmentOnLayout}
style={segmentStyle}
onPress={onSegmentPress}
row
flexG
center
testID={`${testID}.${index}`}
>
{!iconOnRight && renderIcon()}
{label && (
<Text text90 numberOfLines={1} color={segmentedColor}>
Expand Down