Skip to content

Commit e82d849

Browse files
authored
Segmented Control - Component label (#3212)
* added a label to the segmented control * Added sanity tests
1 parent 6d68599 commit e82d849

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

demo/src/screens/componentScreens/SegmentedControlScreen.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const segments: Record<string, Array<SegmentedControlItemProps>> = {
1818
fifth: [{label: 'Full'}, {label: 'Width'}],
1919
sixth: [{label: 'Full'}, {label: 'Width'}, {label: 'With'}, {label: 'A'}, {label: 'Very Long Segment'}],
2020
seventh: [{label: '$'}, {label: '%'}],
21-
eighth: [{label: 'Plus', iconSource: Assets.icons.plusSmall}, {label: 'Minus', iconSource: Assets.icons.minusSmall}, {label: 'Check', iconSource: Assets.icons.checkSmall}]
21+
eighth: [{label: 'Plus', iconSource: Assets.icons.plusSmall}, {label: 'Minus', iconSource: Assets.icons.minusSmall}, {label: 'Check', iconSource: Assets.icons.checkSmall}],
22+
ninth: [{label: 'with'}, {label: 'a'}, {label: 'label'}]
2223
};
2324

2425
const SegmentedControlScreen = () => {
@@ -87,6 +88,15 @@ const SegmentedControlScreen = () => {
8788
style={styles.customStyle}
8889
segmentsStyle={styles.customSegmentsStyle}
8990
/>
91+
<Text marginT-s4 center>
92+
With a label
93+
</Text>
94+
<SegmentedControl
95+
containerStyle={styles.container}
96+
segments={segments.ninth}
97+
preset={screenPreset}
98+
label="Control label"
99+
/>
90100
</View>
91101
</View>
92102
);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import React from 'react';
2+
import {render} from '@testing-library/react-native';
3+
import SegmentedControl, {type SegmentedControlProps} from '../index';
4+
5+
const testSegmentes = [{label: 'Segmented 1'}, {label: 'Segmented 2'}, {label: 'Segmented 3'}, {label: 'Segmented 4'}];
6+
7+
const TestCase = (props: Partial<SegmentedControlProps>) => {
8+
return <SegmentedControl segments={testSegmentes} {...props}/>;
9+
};
10+
11+
describe('SegmentedControl', () => {
12+
it('should render', () => {
13+
const renderTree = render(<TestCase/>);
14+
expect(renderTree).toBeTruthy();
15+
});
16+
it('should render with a label', () => {
17+
const renderTree = render(<TestCase label="Test Label"/>);
18+
expect(renderTree.queryByText('Test Label')).toBeTruthy();
19+
});
20+
});

src/components/segmentedControl/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Constants, asBaseComponent} from '../../commons/new';
1414
import View from '../view';
1515
import Segment, {SegmentedControlItemProps} from './segment';
1616
import useSegmentedControlPreset from './useSegmentedControlPreset';
17+
import Text, {TextProps} from '../text';
1718

1819
const CONTAINER_BORDER_WIDTH = 1;
1920
const TIMING_CONFIG = {
@@ -94,6 +95,14 @@ export type SegmentedControlProps = {
9495
* Preset type
9596
*/
9697
preset?: Presets | `${Presets}`;
98+
/**
99+
* SegmentedControl label
100+
*/
101+
label?: string;
102+
/**
103+
* Pass props for the SegmentedControl label
104+
*/
105+
labelProps?: TextProps;
97106
};
98107

99108
const nonAreUndefined = <T, >(array: Array<T | undefined>): array is Array<T> => {
@@ -130,7 +139,9 @@ const SegmentedControl = (props: SegmentedControlProps) => {
130139
testID,
131140
iconTintColor,
132141
segmentDividerWidth,
133-
segmentDividerColor
142+
segmentDividerColor,
143+
label,
144+
labelProps
134145
} = useSegmentedControlPreset(props);
135146
const animatedSelectedIndex = useSharedValue(initialIndex);
136147
const segmentsStyle = useSharedValue([] as {x: number; width: number}[]);
@@ -233,6 +244,7 @@ const SegmentedControl = (props: SegmentedControlProps) => {
233244
});
234245
return (
235246
<View style={containerStyle} testID={testID}>
247+
{label && <Text bodySmall $textNeutralHeavy marginB-s1 {...labelProps}>{label}</Text>}
236248
<View row center onLayout={containerOnLayout} style={[styles.container, style, {borderRadius, backgroundColor}]}>
237249
<View
238250
reanimated

src/components/segmentedControl/segmentedControl.api.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
{"name": "containerStyle", "type": "ViewStyle", "description": "Additional spacing styles for the container"},
2121
{"name": "style", "type": "ViewStyle", "description": "Custom style to inner container"},
2222
{"name": "segmentLabelStyle", "type": "TextStyle", "description": "Segment label style"},
23-
{"name": "testID", "type": "string", "description": "Component test id"}
23+
{"name": "testID", "type": "string", "description": "Component test id"},
24+
{"name": "label", "type": "string", "description": "SegmentedControl label"},
25+
{"name": "labelProps", "type": "TextProps", "description": "Pass props for the SegmentedControl label"}
2426
],
2527
"snippet": [
2628
"<SegmentedControl segments={[{label: '1st'}, {label: '2nd'}]$1}/>"

0 commit comments

Comments
 (0)