Simplify Switch.tsx

This commit is contained in:
Brandon Presley 2022-12-24 19:55:38 +13:00
parent 27b7e91e91
commit 48432188c3
3 changed files with 8 additions and 16 deletions

View File

@ -79,8 +79,7 @@ export default function EditPlan() {
{DAYS.map(day => (
<Switch
key={day}
onValueChange={value => toggleDay(value, day)}
onPress={() => toggleDay(!days.includes(day), day)}
onChange={value => toggleDay(value, day)}
value={days.includes(day)}>
{day}
</Switch>
@ -94,9 +93,8 @@ export default function EditPlan() {
names.map(name => (
<Switch
key={name}
onValueChange={value => toggleWorkout(value, name)}
value={workouts.includes(name)}
onPress={() => toggleWorkout(!workouts.includes(name), name)}>
onChange={value => toggleWorkout(value, name)}
value={workouts.includes(name)}>
{name}
</Switch>
))

View File

@ -192,11 +192,7 @@ export default function SettingsPage() {
const renderSwitch = useCallback(
(item: Input<boolean>) => (
<Switch
onPress={() => item.onChange(!item.value)}
key={item.name}
value={item.value}
onValueChange={item.onChange}>
<Switch key={item.name} value={item.value} onChange={item.onChange}>
{item.name}
</Switch>
),

View File

@ -4,20 +4,18 @@ import {MARGIN} from './constants'
export default function Switch({
value,
onValueChange,
onPress,
onChange,
children,
}: {
value?: boolean
onValueChange: (value: boolean) => void
onPress: () => void
onChange: (value: boolean) => void
children: string
}) {
const {colors} = useTheme()
return (
<Pressable
onPress={onPress}
onPress={() => onChange(!value)}
style={{
flexDirection: 'row',
flexWrap: 'wrap',
@ -28,7 +26,7 @@ export default function Switch({
color={colors.primary}
style={{marginRight: MARGIN}}
value={value}
onValueChange={onValueChange}
onValueChange={onChange}
/>
<Text>{children}</Text>
</Pressable>