Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter typing for SegmentedButtons component #4532

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'train' | 'drive';

const themeMock = {
colors: {
onSurface: '#3700B3',
Expand All @@ -12,13 +14,13 @@ const themeMock = {
};

const SegmentButtonCustomColorCheck = () => {
const [themeValue, setThemeValue] = React.useState('');
const [colorValue, setColorValue] = React.useState('');
const [themeValue, setThemeValue] = React.useState<TransportMode>('walk');
const [colorValue, setColorValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - Custom Colors`}>
<List.Subheader>Via Theme</List.Subheader>
<SegmentedButtons
<SegmentedButtons<TransportMode>
value={themeValue}
onValueChange={setThemeValue}
theme={themeMock}
Expand Down Expand Up @@ -46,7 +48,7 @@ const SegmentButtonCustomColorCheck = () => {
style={styles.group}
/>
<List.Subheader>Via Props</List.Subheader>
<SegmentedButtons
<SegmentedButtons<TransportMode>
value={colorValue}
onValueChange={setColorValue}
theme={themeMock}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'train' | 'drive';

const SegmentedButtonDefault = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
value={value}
onValueChange={setValue}
buttons={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'disabled' | 'drive';

const SegmentedButtonDisabled = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - disabled`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
onValueChange={setValue}
buttons={[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'transit' | 'drive';

const SegmentedButtonMultiselect = () => {
const [value, setValue] = React.useState<string[]>([]);
const [value, setValue] = React.useState<TransportMode[]>([]);

return (
<List.Section title={`Segmented Button - multiselect`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
multiSelect
onValueChange={setValue}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type Size = 'size-s' | 'size-m' | 'size-l' | 'size-xl' | 'size-xxl';

const SegmentedButtonMultiselectIcons = () => {
const [value, setValue] = React.useState<string[]>([]);
const [value, setValue] = React.useState<Size[]>([]);

return (
<List.Section title={`Segmented Button - multiselect only icons`}>
<SegmentedButtons
<SegmentedButtons<Size>
multiSelect
onValueChange={setValue}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import { Card, IconButton, SegmentedButtons } from 'react-native-paper';

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

type PriceRange = '1' | '2' | '3' | '4';

const SegmentedButtonMultiselectRealCase = () => {
const [value, setValue] = React.useState<string[]>([]);
const [value, setValue] = React.useState<PriceRange[]>([]);

const filteredData = React.useMemo(
() =>
restaurantsData.filter((item) => value.includes(item.price.toString())),
value.includes(item.price.toString() as PriceRange)
),
[value]
);

return (
<View style={styles.container}>
<SegmentedButtons
<SegmentedButtons<PriceRange>
value={value}
onValueChange={setValue}
multiSelect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'train' | 'drive';

const SegmentedButtonOnlyIcons = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - only icons`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
onValueChange={setValue}
style={styles.group}
value={value}
Expand All @@ -19,7 +21,7 @@ const SegmentedButtonOnlyIcons = () => {
},
{
icon: 'train',
value: 'trainsit',
value: 'train',
},
{
icon: 'car',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'transit' | 'drive';

const SegmentedButtonOnlyIconsWithCheck = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - icons + show selected check`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
onValueChange={setValue}
style={styles.group}
value={value}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { Card, IconButton, SegmentedButtons } from 'react-native-paper';

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

type MediaType = 'songs' | 'albums';

const SegmentedButtonRealCase = () => {
const [value, setValue] = React.useState('songs');
const [value, setValue] = React.useState<MediaType>('songs');

return (
<View style={styles.container}>
<SegmentedButtons
<SegmentedButtons<MediaType>
value={value}
onValueChange={setValue}
buttons={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'transit' | 'drive';

const SegmentedButtonWithDensity = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - only labels + density`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
onValueChange={setValue}
value={value}
density="medium"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { StyleSheet } from 'react-native';

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

type TransportMode = 'walk' | 'train' | 'drive';

const SegmentedButtonWithSelectedCheck = () => {
const [value, setValue] = React.useState('');
const [value, setValue] = React.useState<TransportMode>('walk');

return (
<List.Section title={`Segmented Button - show selected check`}>
<SegmentedButtons
<SegmentedButtons<TransportMode>
onValueChange={setValue}
value={value}
style={styles.group}
Expand Down
20 changes: 10 additions & 10 deletions src/components/SegmentedButtons/SegmentedButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@ import { getDisabledSegmentedButtonStyle } from './utils';
import { useInternalTheme } from '../../core/theming';
import type { IconSource } from '../Icon';

type ConditionalValue =
type ConditionalValue<T> =
| {
/**
* Array of the currently selected segmented button values.
*/
value: string[];
value: T[];
/**
* Support multiple selected options.
*/
multiSelect: true;
/**
* Function to execute on selection change
*/
onValueChange: (value: string[]) => void;
onValueChange: (value: T[]) => void;
}
| {
/**
* Value of the currently selected segmented button.
*/
value: string;
value: T;
/**
* Support multiple selected options.
*/
multiSelect?: false;
/**
* Function to execute on selection change
*/
onValueChange: (value: string) => void;
onValueChange: (value: T) => void;
};

export type Props = {
export type Props<T> = {
/**
* Buttons to display as options in toggle button.
* Button should contain the following properties:
Expand All @@ -62,7 +62,7 @@ export type Props = {
* - `testID`: testID to be used on tests
*/
buttons: {
value: string;
value: T;
icon?: IconSource;
disabled?: boolean;
accessibilityLabel?: string;
Expand All @@ -81,7 +81,7 @@ export type Props = {
density?: 'regular' | 'small' | 'medium' | 'high';
style?: StyleProp<ViewStyle>;
theme?: ThemeProp;
} & ConditionalValue;
} & ConditionalValue<T>;

/**
* Segmented buttons can be used to select options, switch views or sort elements.</br>
Expand Down Expand Up @@ -126,15 +126,15 @@ export type Props = {
* export default MyComponent;
*```
*/
const SegmentedButtons = ({
const SegmentedButtons = <T,>({
value,
onValueChange,
buttons,
multiSelect,
density,
style,
theme: themeOverrides,
}: Props) => {
}: Props<T>) => {
const theme = useInternalTheme(themeOverrides);

return (
Expand Down