Skip to content

Commit

Permalink
Add segmentStyle prop to SegmentedControl (#2563)
Browse files Browse the repository at this point in the history
* Add segmentStyle prop to SegmentedControl

* renaming

* renaming #2
  • Loading branch information
lidord-wix authored Apr 19, 2023
1 parent 0d913c7 commit 2cb96c2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
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,
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

0 comments on commit 2cb96c2

Please sign in to comment.