Skip to content

Commit 19fd161

Browse files
authored
SegmentedControl icon wrapper component change (#3084)
* SegmentedControl icon wrapper component change * revert playground screen
1 parent 2433636 commit 19fd161

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/components/segmentedControl/segment.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React, {useCallback, useMemo} from 'react';
2-
import {LayoutChangeEvent, ImageSourcePropType, ImageStyle, StyleProp, ViewStyle} from 'react-native';
2+
import {LayoutChangeEvent, 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';
66
import TouchableOpacity from '../touchableOpacity';
77
import {SegmentedControlProps} from './index';
8+
import Icon, {IconProps} from '../icon';
89

910
export type SegmentedControlItemProps = Pick<SegmentedControlProps, 'segmentLabelStyle'> & {
1011
/**
@@ -14,7 +15,7 @@ export type SegmentedControlItemProps = Pick<SegmentedControlProps, 'segmentLabe
1415
/**
1516
* An icon for the segment.
1617
*/
17-
iconSource?: ImageSourcePropType;
18+
iconSource?: IconProps['source'];
1819
/**
1920
* An icon for the segment.
2021
*/
@@ -82,6 +83,8 @@ const Segment = React.memo((props: SegmentProps) => {
8283
iconTintColor
8384
} = props;
8485

86+
const AnimatedUIIcon = Reanimated.createAnimatedComponent(Icon);
87+
8588
const animatedTextStyle = useAnimatedStyle(() => {
8689
const color = selectedIndex?.value === index ? activeColor : inactiveColor;
8790
return {color};
@@ -97,8 +100,7 @@ const Segment = React.memo((props: SegmentProps) => {
97100
}, [style]);
98101

99102
const renderIcon = useCallback(() => {
100-
return iconSource && <Reanimated.Image source={iconSource} style={[animatedIconStyle, iconStyle]}/>;
101-
// eslint-disable-next-line react-hooks/exhaustive-deps
103+
return iconSource && <AnimatedUIIcon source={iconSource} style={[animatedIconStyle, iconStyle]}/>;
102104
}, [iconSource, iconStyle]);
103105

104106
const onSegmentPress = useCallback(() => {

webDemo/src/App.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ import AnimatedImage from 'react-native-ui-lib/AnimatedImage';
1818
import Avatar from 'react-native-ui-lib/Avatar';
1919
import Drawer from 'react-native-ui-lib/Drawer';
2020

21-
import {
22-
Colors,
23-
Spacings,
24-
Typography,
25-
Assets,
26-
Text,
27-
Incubator
28-
} from 'react-native-ui-lib';
21+
import {Colors, Spacings, Typography, Assets, Text, Incubator, SegmentedControl} from 'react-native-ui-lib';
2922

3023
import Picker from './examples/Picker';
3124
import RadioGroup from './examples/RadioButtonGroup';
@@ -235,6 +228,20 @@ const itemsToRender: ItemToRender[] = [
235228
);
236229
}
237230
},
231+
{
232+
title: 'SegmentedControl',
233+
FC: () => {
234+
const segments = [
235+
{label: '1'},
236+
{label: '2'},
237+
{label: '3'},
238+
{label: '4', iconSource: Assets.icons.search, iconOnRight: true},
239+
{label: '5'}
240+
];
241+
242+
return <SegmentedControl segments={segments} />;
243+
}
244+
},
238245
{
239246
title: 'Switch',
240247
FC: () => {

0 commit comments

Comments
 (0)