Skip to content

Commit e5d5bfd

Browse files
committed
Add segmentStyle prop to SegmentedControl (#2563)
* Add segmentStyle prop to SegmentedControl * renaming * renaming #2
1 parent b8509b5 commit e5d5bfd

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

demo/src/screens/componentScreens/SegmentedControlScreen.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const segments = {
1414
},
1515
{label: 'Short'}
1616
],
17-
forth: [{label: 'With'}, {label: 'Custom'}, {label: 'Colors'}],
17+
forth: [{label: 'With'}, {label: 'Custom'}, {label: 'Style'}],
1818
fifth: [{label: 'Full'}, {label: 'Width'}],
1919
sixth: [{label: 'Full'}, {label: 'Width'}, {label: 'With'}, {label: 'A'}, {label: 'Very Long Segment'}]
2020
};
@@ -49,6 +49,8 @@ const SegmentedControlScreen = () => {
4949
backgroundColor={Colors.$backgroundInverted}
5050
activeBackgroundColor={Colors.$backgroundNeutralIdle}
5151
inactiveColor={Colors.$textDisabled}
52+
style={styles.customStyle}
53+
segmentsStyle={styles.customSegmentsStyle}
5254
/>
5355
</View>
5456
<SegmentedControl
@@ -70,6 +72,13 @@ const SegmentedControlScreen = () => {
7072
const styles = StyleSheet.create({
7173
container: {
7274
marginTop: 20
75+
},
76+
customStyle: {
77+
height: 50,
78+
width: 300
79+
},
80+
customSegmentsStyle: {
81+
height: 50
7382
}
7483
});
7584

src/components/segmentedControl/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ import Reanimated, {
1313
import {Colors, BorderRadiuses, Spacings} from '../../style';
1414
import {Constants, asBaseComponent} from '../../commons/new';
1515
import View from '../view';
16-
import Segment, {SegmentedControlItemProps as SegmentProps} from './segment';
16+
import Segment, {SegmentedControlItemProps} from './segment';
1717

1818
const BORDER_WIDTH = 1;
1919
const TIMING_CONFIG: WithTimingConfig = {
2020
duration: 300,
2121
easing: Easing.bezier(0.33, 1, 0.68, 1)
2222
};
2323

24-
export type SegmentedControlItemProps = SegmentProps;
24+
export {SegmentedControlItemProps};
2525
export type SegmentedControlProps = {
2626
/**
2727
* Array on segments.
@@ -71,6 +71,10 @@ export type SegmentedControlProps = {
7171
* Trailing throttle time of changing index in ms.
7272
*/
7373
throttleTime?: number;
74+
/**
75+
* Additional style for the segment
76+
*/
77+
segmentsStyle?: StyleProp<ViewStyle>;
7478
/**
7579
* Additional spacing styles for the container
7680
*/
@@ -98,6 +102,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
98102
outlineColor = activeColor,
99103
outlineWidth = BORDER_WIDTH,
100104
throttleTime = 0,
105+
segmentsStyle: segmentsStyleProp,
101106
testID
102107
} = props;
103108
const animatedSelectedIndex = useSharedValue(initialIndex);
@@ -164,6 +169,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
164169
selectedIndex={animatedSelectedIndex}
165170
activeColor={activeColor}
166171
inactiveColor={inactiveColor}
172+
style={segmentsStyleProp}
167173
{...segments?.[index]}
168174
testID={testID}
169175
/>

src/components/segmentedControl/segment.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {useCallback, useMemo} from 'react';
2-
import {LayoutChangeEvent, ImageSourcePropType, ImageStyle, StyleProp} from 'react-native';
2+
import {LayoutChangeEvent, ImageSourcePropType, ImageStyle, StyleProp, ViewStyle} from 'react-native';
33
import Reanimated, {useAnimatedStyle} from 'react-native-reanimated';
44
import {Spacings, Typography} from '../../style';
55
import {asBaseComponent} from '../../commons/new';
@@ -49,6 +49,10 @@ export type SegmentProps = SegmentedControlItemProps & {
4949
* onLayout function.
5050
*/
5151
onLayout?: (index: number, event: LayoutChangeEvent) => void;
52+
/**
53+
* Additional style for the segment.
54+
*/
55+
style?: StyleProp<ViewStyle>;
5256
testID?: string;
5357
};
5458

@@ -67,6 +71,7 @@ const Segment = React.memo((props: SegmentProps) => {
6771
inactiveColor,
6872
index,
6973
iconOnRight,
74+
style,
7075
testID
7176
} = props;
7277

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

83-
const segmentStyle = useMemo(() => ({paddingHorizontal: Spacings.s3, paddingVertical: Spacings.s2}), []);
88+
const segmentStyle = useMemo(() => {
89+
return [{paddingHorizontal: Spacings.s3, paddingVertical: Spacings.s2}, style];
90+
}, [style]);
8491

8592
const renderIcon = useCallback(() => {
8693
return iconSource && <Reanimated.Image source={iconSource} style={[animatedIconStyle, iconStyle]}/>;

src/components/segmentedControl/segmentedControl.api.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
{"name": "outlineWidth", "type": "number", "description": "The width of the active segment outline"},
1717
{"name": "iconOnRight", "type": "boolean", "description": "Should the icon be on right of the label"},
1818
{"name": "throttleTime", "type": "number", "description": "Trailing throttle time of changing index in ms."},
19+
{"name": "segmentsStyle", "type": "ViewStyle", "description": "Additional style for the segments"},
1920
{"name": "containerStyle", "type": "ViewStyle", "description": "Additional spacing styles for the container"},
2021
{"name": "style", "type": "ViewStyle", "description": "Custom style to inner container"},
2122
{"name": "testID", "type": "string", "description": "Component test id"}

0 commit comments

Comments
 (0)