Skip to content
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

Add segmentStyle prop to SegmentedControl #2563

Merged
merged 3 commits into from
Apr 19, 2023
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
11 changes: 10 additions & 1 deletion demo/src/screens/componentScreens/SegmentedControlScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const segments = {
},
{label: 'Short'}
],
forth: [{label: 'With'}, {label: 'Custom'}, {label: 'Colors'}],
forth: [{label: 'With'}, {label: 'Custom'}, {label: 'Style'}],
fifth: [{label: 'Full'}, {label: 'Width'}],
sixth: [{label: 'Full'}, {label: 'Width'}, {label: 'With'}, {label: 'A'}, {label: 'Very Long Segment'}]
};
Expand Down Expand Up @@ -49,6 +49,8 @@ const SegmentedControlScreen = () => {
backgroundColor={Colors.$backgroundInverted}
activeBackgroundColor={Colors.$backgroundNeutralIdle}
inactiveColor={Colors.$textDisabled}
style={styles.customStyle}
segmentsStyle={styles.customSegmentsStyle}
/>
</View>
<SegmentedControl
Expand All @@ -70,6 +72,13 @@ const SegmentedControlScreen = () => {
const styles = StyleSheet.create({
container: {
marginTop: 20
},
customStyle: {
height: 50,
width: 300
},
customSegmentsStyle: {
height: 50
}
});

Expand Down
10 changes: 8 additions & 2 deletions src/components/segmentedControl/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import Reanimated, {
import {Colors, BorderRadiuses, Spacings} from '../../style';
import {Constants, asBaseComponent} from '../../commons/new';
import View from '../view';
import Segment, {SegmentedControlItemProps as SegmentProps} from './segment';
import Segment, {SegmentedControlItemProps} from './segment';

const BORDER_WIDTH = 1;
const TIMING_CONFIG: WithTimingConfig = {
duration: 300,
easing: Easing.bezier(0.33, 1, 0.68, 1)
};

export type SegmentedControlItemProps = SegmentProps;
export {SegmentedControlItemProps};
export type SegmentedControlProps = {
/**
* Array on segments.
Expand Down Expand Up @@ -71,6 +71,10 @@ export type SegmentedControlProps = {
* Trailing throttle time of changing index in ms.
*/
throttleTime?: number;
/**
* Additional style for the segment
*/
segmentsStyle?: StyleProp<ViewStyle>;
/**
* Additional spacing styles for the container
*/
Expand Down Expand Up @@ -98,6 +102,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
outlineColor = activeColor,
outlineWidth = BORDER_WIDTH,
throttleTime = 0,
segmentsStyle: segmentsStyleProp,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need the rename here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's another segmentsStyle in this file

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok...

testID
} = props;
const animatedSelectedIndex = useSharedValue(initialIndex);
Expand Down Expand Up @@ -164,6 +169,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
selectedIndex={animatedSelectedIndex}
activeColor={activeColor}
inactiveColor={inactiveColor}
style={segmentsStyleProp}
{...segments?.[index]}
testID={testID}
/>
Expand Down
11 changes: 9 additions & 2 deletions src/components/segmentedControl/segment.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {useCallback, useMemo} from 'react';
import {LayoutChangeEvent, ImageSourcePropType, ImageStyle, StyleProp} from 'react-native';
import {LayoutChangeEvent, ImageSourcePropType, ImageStyle, StyleProp, ViewStyle} from 'react-native';
import Reanimated, {useAnimatedStyle} from 'react-native-reanimated';
import {Spacings, Typography} from '../../style';
import {asBaseComponent} from '../../commons/new';
Expand Down Expand Up @@ -49,6 +49,10 @@ export type SegmentProps = SegmentedControlItemProps & {
* onLayout function.
*/
onLayout?: (index: number, event: LayoutChangeEvent) => void;
/**
* Additional style for the segment.
*/
style?: StyleProp<ViewStyle>;
testID?: string;
};

Expand All @@ -67,6 +71,7 @@ const Segment = React.memo((props: SegmentProps) => {
inactiveColor,
index,
iconOnRight,
style,
testID
} = props;

Expand All @@ -80,7 +85,9 @@ const Segment = React.memo((props: SegmentProps) => {
return {tintColor};
});

const segmentStyle = useMemo(() => ({paddingHorizontal: Spacings.s3, paddingVertical: Spacings.s2}), []);
const segmentStyle = useMemo(() => {
return [{paddingHorizontal: Spacings.s3, paddingVertical: Spacings.s2}, style];
}, [style]);

const renderIcon = useCallback(() => {
return iconSource && <Reanimated.Image source={iconSource} style={[animatedIconStyle, iconStyle]}/>;
Expand Down
1 change: 1 addition & 0 deletions src/components/segmentedControl/segmentedControl.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{"name": "outlineWidth", "type": "number", "description": "The width of the active segment outline"},
{"name": "iconOnRight", "type": "boolean", "description": "Should the icon be on right of the label"},
{"name": "throttleTime", "type": "number", "description": "Trailing throttle time of changing index in ms."},
{"name": "segmentsStyle", "type": "ViewStyle", "description": "Additional style for the segments"},
{"name": "containerStyle", "type": "ViewStyle", "description": "Additional spacing styles for the container"},
{"name": "style", "type": "ViewStyle", "description": "Custom style to inner container"},
{"name": "testID", "type": "string", "description": "Component test id"}
Expand Down