diff --git a/src/components/forms/Toggle.tsx b/src/components/forms/Toggle.tsx index 0e913854a6..4145895d81 100644 --- a/src/components/forms/Toggle.tsx +++ b/src/components/forms/Toggle.tsx @@ -4,7 +4,6 @@ import {Pressable, PressableProps, View, ViewStyle} from 'react-native' import {useTheme, atoms as a, web} from '#/alf' import {Text} from '#/components/Typography' import {useInteractionState} from '#/components/hooks/useInteractionState' -import {StyleProp} from 'react-native' type ItemState = { name: string @@ -47,7 +46,6 @@ export type GroupProps = React.PropsWithChildren<{ disabled?: boolean onChange: (value: string[]) => void label: string - style?: StyleProp }> export type ItemProps = Omit< @@ -66,22 +64,15 @@ export type ItemProps = Omit< function Group({ children, - values: initialValues, + values: providedValues, onChange, disabled = false, type = 'checkbox', maxSelections, - style, label, }: GroupProps) { - if (!initialValues) { - throw new Error(`Don't forget to pass in 'values' to your Toggle.Group`) - } - const groupRole = type === 'radio' ? 'radiogroup' : undefined - const [values, setValues] = React.useState( - type === 'radio' ? initialValues.slice(0, 1) : initialValues, - ) + const values = type === 'radio' ? providedValues.slice(0, 1) : providedValues const [maxReached, setMaxReached] = React.useState(false) const setFieldValue = React.useCallback< @@ -89,21 +80,16 @@ function Group({ >( ({name, value}) => { if (type === 'checkbox') { - setValues(s => { - const state = s.filter(v => v !== name) - return value ? state.concat(name) : state - }) + const pruned = values.filter(v => v !== name) + const next = value ? pruned.concat(name) : pruned + onChange(next) } else { - setValues([name]) + onChange([name]) } }, - [type, setValues], + [type, onChange, values], ) - React.useEffect(() => { - onChange(values) - }, [values, onChange]) - React.useEffect(() => { if (type === 'checkbox') { if ( @@ -137,7 +123,6 @@ function Group({ & + AccessibilityProps & + React.PropsWithChildren<{}> + +export type GroupProps = Omit & { + multiple?: boolean +} + +export function Group({children, multiple, ...props}: GroupProps) { + const t = useTheme() + return ( + + + {children} + + + ) +} + +export function Button({children, ...props}: ItemProps) { + const t = useTheme() + return ( + + {state => ( + + {typeof children === 'string' ? ( + + {children} + + ) : ( + children + )} + + )} + + ) +} + +export default { + Group, + Button, +} diff --git a/src/view/screens/Storybook/Forms.tsx b/src/view/screens/Storybook/Forms.tsx index b76509c3a6..1c2a9f4c46 100644 --- a/src/view/screens/Storybook/Forms.tsx +++ b/src/view/screens/Storybook/Forms.tsx @@ -9,8 +9,11 @@ import {InputGroup} from '#/components/forms/InputGroup' import {Logo} from '#/view/icons/Logo' import Toggle from '#/components/forms/Toggle' import ToggleButton from '#/components/forms/ToggleButton' +import {Button} from '#/components/Button' export function Forms() { + const [toggleGroupValues, setToggleGroupValues] = React.useState(['a']) + return (

Forms

@@ -135,85 +138,98 @@ export function Forms() { type="checkbox" maxSelections={2} values={['a', 'b']} - onChange={e => console.log(e)} - style={[a.gap_sm]}> - - - Click me - - - - Click me - - - - Click me - - - - Click me - - - - Click me - + onChange={e => console.log(e)}> + + + + Click me + + + + Click me + + + + Click me + + + + Click me + + + + Click me + + console.log(e)} - style={[a.gap_sm]}> - - - Click me - - - - Click me - - - - Click me - - - - Click me - - - - Click me - + values={toggleGroupValues} + onChange={setToggleGroupValues}> + + + + Click me + + + + Click me + + + + Click me + + + + Click me + + + + Click me + + + + console.log(e)} - style={[a.gap_sm]}> - - - Click me - - - - Click me - - - - Click me - - - - Click me - - - - Click me - + onChange={e => console.log(e)}> + + + + Click me + + + + Click me + + + + Click me + + + + Click me + + + + Click me + +