Skip to content

Commit 27a6556

Browse files
authored
refactor: segmented button typing (#4686)
1 parent d4cca22 commit 27a6556

12 files changed

+50
-26
lines changed

example/src/Examples/SegmentedButtons/SegmentedButtonCustomColorCheck.tsx

Lines changed: 5 additions & 3 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,8 +14,8 @@ 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`}>
@@ -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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

example/src/Examples/SegmentedButtons/SegmentedButtonDisabled.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselect.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectIcons.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

example/src/Examples/SegmentedButtons/SegmentedButtonMultiselectRealCase.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ 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) =>
16+
value.includes(item.price.toString() as PriceRange)
17+
),
1418
[value]
1519
);
1620

example/src/Examples/SegmentedButtons/SegmentedButtonOnlyIcons.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>
@@ -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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

example/src/Examples/SegmentedButtons/SegmentedButtonRealCase.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ 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}>

example/src/Examples/SegmentedButtons/SegmentedButtonWithDensity.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ 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`}>

0 commit comments

Comments
 (0)