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

View File

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

View File

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