Skip to content

Commit 0d4adcf

Browse files
jboteroslukewalczak
authored andcommitted
Stricter typing for SegmentedButtons component
Related to #4528 Add stricter typing for `onValueChange` in `SegmentedButtons` component to support union types in TypeScript projects. * Modify `src/components/SegmentedButtons/SegmentedButtons.tsx` to accept a generic type parameter for the `value` and `onValueChange` props. * Update `ConditionalValue` type to use the generic type parameter. * Update `Props` type to use the generic type parameter. * Update example files to demonstrate the usage of the stricter typing: * `example/src/Examples/SegmentedButtons/SegmentedButtonDefault.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonWithSelectedCheck.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonWithDensity.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonMultiselect.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectIcons.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonCustomColorCheck.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonDisabled.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectRealCase.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonOnlyIcons.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonOnlyIconsWithCheck.tsx` * `example/src/Examples/SegmentedButtons/SegmentedButtonRealCase.tsx` * `example/src/Examples/SegmentedButtonsExample.tsx` * `example/src/Examples/ToggleButtonExample.tsx` --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/callstack/react-native-paper/issues/4528?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 23604c0 commit 0d4adcf

12 files changed

+58
-36
lines changed

example/src/Examples/SegmentedButtons/SegmentedButtonCustomColorCheck.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'train' | 'drive';
7+
68
const themeMock = {
79
colors: {
810
onSurface: '#3700B3',
@@ -12,13 +14,13 @@ const themeMock = {
1214
};
1315

1416
const SegmentButtonCustomColorCheck = () => {
15-
const [themeValue, setThemeValue] = React.useState('');
16-
const [colorValue, setColorValue] = React.useState('');
17+
const [themeValue, setThemeValue] = React.useState<TransportMode>('walk');
18+
const [colorValue, setColorValue] = React.useState<TransportMode>('walk');
1719

1820
return (
1921
<List.Section title={`Segmented Button - Custom Colors`}>
2022
<List.Subheader>Via Theme</List.Subheader>
21-
<SegmentedButtons
23+
<SegmentedButtons<TransportMode>
2224
value={themeValue}
2325
onValueChange={setThemeValue}
2426
theme={themeMock}
@@ -46,7 +48,7 @@ const SegmentButtonCustomColorCheck = () => {
4648
style={styles.group}
4749
/>
4850
<List.Subheader>Via Props</List.Subheader>
49-
<SegmentedButtons
51+
<SegmentedButtons<TransportMode>
5052
value={colorValue}
5153
onValueChange={setColorValue}
5254
theme={themeMock}

example/src/Examples/SegmentedButtons/SegmentedButtonDefault.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'train' | 'drive';
7+
68
const SegmentedButtonDefault = () => {
7-
const [value, setValue] = React.useState('');
9+
const [value, setValue] = React.useState<TransportMode>('walk');
810

911
return (
1012
<List.Section title={`Segmented Button`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
value={value}
1315
onValueChange={setValue}
1416
buttons={[

example/src/Examples/SegmentedButtons/SegmentedButtonDisabled.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'disabled' | 'drive';
7+
68
const SegmentedButtonDisabled = () => {
7-
const [value, setValue] = React.useState('');
9+
const [value, setValue] = React.useState<TransportMode>('walk');
810

911
return (
1012
<List.Section title={`Segmented Button - disabled`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
onValueChange={setValue}
1315
buttons={[
1416
{

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselect.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'transit' | 'drive';
7+
68
const SegmentedButtonMultiselect = () => {
7-
const [value, setValue] = React.useState<string[]>([]);
9+
const [value, setValue] = React.useState<TransportMode[]>([]);
810

911
return (
1012
<List.Section title={`Segmented Button - multiselect`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
multiSelect
1315
onValueChange={setValue}
1416
value={value}

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectIcons.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type Size = 'size-s' | 'size-m' | 'size-l' | 'size-xl' | 'size-xxl';
7+
68
const SegmentedButtonMultiselectIcons = () => {
7-
const [value, setValue] = React.useState<string[]>([]);
9+
const [value, setValue] = React.useState<Size[]>([]);
810

911
return (
1012
<List.Section title={`Segmented Button - multiselect only icons`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<Size>
1214
multiSelect
1315
onValueChange={setValue}
1416
value={value}

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectRealCase.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ import { Card, IconButton, SegmentedButtons } from 'react-native-paper';
55

66
import { restaurantsData } from '../../../utils';
77

8+
type PriceRange = '1' | '2' | '3' | '4';
9+
810
const SegmentedButtonMultiselectRealCase = () => {
9-
const [value, setValue] = React.useState<string[]>([]);
11+
const [value, setValue] = React.useState<PriceRange[]>([]);
1012

1113
const filteredData = React.useMemo(
1214
() =>
13-
restaurantsData.filter((item) => value.includes(item.price.toString())),
15+
restaurantsData.filter((item) => value.includes(item.price.toString() as PriceRange)),
1416
[value]
1517
);
1618

1719
return (
1820
<View style={styles.container}>
19-
<SegmentedButtons
21+
<SegmentedButtons<PriceRange>
2022
value={value}
2123
onValueChange={setValue}
2224
multiSelect

example/src/Examples/SegmentedButtons/SegmentedButtonOnlyIcons.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'train' | 'drive';
7+
68
const SegmentedButtonOnlyIcons = () => {
7-
const [value, setValue] = React.useState('');
9+
const [value, setValue] = React.useState<TransportMode>('walk');
810

911
return (
1012
<List.Section title={`Segmented Button - only icons`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
onValueChange={setValue}
1315
style={styles.group}
1416
value={value}
@@ -19,7 +21,7 @@ const SegmentedButtonOnlyIcons = () => {
1921
},
2022
{
2123
icon: 'train',
22-
value: 'trainsit',
24+
value: 'train',
2325
},
2426
{
2527
icon: 'car',

example/src/Examples/SegmentedButtons/SegmentedButtonOnlyIconsWithCheck.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'transit' | 'drive';
7+
68
const SegmentedButtonOnlyIconsWithCheck = () => {
7-
const [value, setValue] = React.useState('');
9+
const [value, setValue] = React.useState<TransportMode>('walk');
810

911
return (
1012
<List.Section title={`Segmented Button - icons + show selected check`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
onValueChange={setValue}
1315
style={styles.group}
1416
value={value}

example/src/Examples/SegmentedButtons/SegmentedButtonRealCase.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { Card, IconButton, SegmentedButtons } from 'react-native-paper';
55

66
import { songsData, albumsData } from '../../../utils';
77

8+
type MediaType = 'songs' | 'albums';
9+
810
const SegmentedButtonRealCase = () => {
9-
const [value, setValue] = React.useState('songs');
11+
const [value, setValue] = React.useState<MediaType>('songs');
1012

1113
return (
1214
<View style={styles.container}>
13-
<SegmentedButtons
15+
<SegmentedButtons<MediaType>
1416
value={value}
1517
onValueChange={setValue}
1618
buttons={[

example/src/Examples/SegmentedButtons/SegmentedButtonWithDensity.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';
33

44
import { List, SegmentedButtons } from 'react-native-paper';
55

6+
type TransportMode = 'walk' | 'transit' | 'drive';
7+
68
const SegmentedButtonWithDensity = () => {
7-
const [value, setValue] = React.useState('');
9+
const [value, setValue] = React.useState<TransportMode>('walk');
810

911
return (
1012
<List.Section title={`Segmented Button - only labels + density`}>
11-
<SegmentedButtons
13+
<SegmentedButtons<TransportMode>
1214
onValueChange={setValue}
1315
value={value}
1416
density="medium"

0 commit comments

Comments
 (0)