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

fix: Made ToggleButton.Group TS compatible #3543

Merged
merged 5 commits into from
Jan 9, 2023
Merged
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
37 changes: 29 additions & 8 deletions example/src/Examples/ToggleButtonExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@ import { ToggleButton, List } from 'react-native-paper';
import ScreenWrapper from '../ScreenWrapper';

type StatusState = 'checked' | 'unchecked';
type Fruits = 'watermelon' | 'strawberries';

enum FontsEnum {
NoFormat = 'no-format',
Italic = 'italic',
Bold = 'bold',
Underline = 'underlined',
ColorText = 'format-color',
}

const ToggleButtonExample = () => {
const [first, setFirst] = React.useState<string>('bold');
const [fruit, setFruit] = React.useState<string>('watermelon');
const [first, setFirst] = React.useState('bold');
const [fruit, setFruit] = React.useState<Fruits>('watermelon');
const [status, setStatus] = React.useState<StatusState>('checked');
const [font, setFont] = React.useState<FontsEnum>(FontsEnum.NoFormat);

const handleFruit = (value: Fruits) => setFruit(value);

return (
<ScreenWrapper>
Expand All @@ -26,7 +38,7 @@ const ToggleButtonExample = () => {
/>
</View>
</List.Section>
<List.Section title="Group">
<List.Section title="Row">
<ToggleButton.Row
value={first}
onValueChange={(value: string) => setFirst(value)}
Expand All @@ -38,12 +50,21 @@ const ToggleButtonExample = () => {
<ToggleButton icon="format-color-text" value="format-color" />
</ToggleButton.Row>
</List.Section>
<List.Section title="Custom">
<List.Section title="Group & enums">
<ToggleButton.Group value={font} onValueChange={setFont}>
<ToggleButton
disabled
icon="format-italic"
value={FontsEnum.Italic}
/>
<ToggleButton icon="format-bold" value={FontsEnum.Bold} />
<ToggleButton icon="format-underline" value={FontsEnum.Underline} />
<ToggleButton icon="format-color-text" value={FontsEnum.ColorText} />
</ToggleButton.Group>
</List.Section>
<List.Section title="Custom & union types">
<View style={[styles.padding, styles.row]}>
<ToggleButton.Group
value={fruit}
onValueChange={(value: string) => setFruit(value)}
>
<ToggleButton.Group value={fruit} onValueChange={handleFruit}>
<ImageBackground
style={styles.customImage}
source={{
Expand Down
19 changes: 12 additions & 7 deletions src/components/ToggleButton/ToggleButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import * as React from 'react';

export type Props = {
export type Props<Value = string> = {
/**
* Function to execute on selection change.
*/
onValueChange: (value: string) => void | null;
onValueChange: (value: Value) => void;
/**
* Value of the currently selected toggle button.
*/
value: string | null;
value: Value | null;
/**
* React elements containing toggle buttons.
*/
children: React.ReactNode;
};

type ToggleButtonContextType = {
value: string | null;
onValueChange: (item: string) => void | null;
type ToggleButtonContextType<Value> = {
value: Value | null;
onValueChange: (item: Value) => void;
};

export const ToggleButtonGroupContext =
//@ts-expect-error: TS can't ensure the type from Group to children
React.createContext<ToggleButtonContextType>(null as any);

/**
Expand Down Expand Up @@ -54,7 +55,11 @@ export const ToggleButtonGroupContext =
* export default MyComponent;
*```
*/
const ToggleButtonGroup = ({ value, onValueChange, children }: Props) => (
const ToggleButtonGroup = <Value = string,>({
value,
onValueChange,
children,
}: Props<Value>) => (
<ToggleButtonGroupContext.Provider
value={{
value,
Expand Down