Massive/SetForm.tsx

108 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-08-06 02:49:30 +00:00
import React, {useRef, useState} from 'react';
2022-07-20 03:48:48 +00:00
import {ScrollView, StyleSheet} from 'react-native';
import DateTimePickerModal from 'react-native-modal-datetime-picker';
import {Button, TextInput} from 'react-native-paper';
import Set from './set';
2022-08-06 02:49:30 +00:00
import {format} from './time';
2022-07-20 03:48:48 +00:00
export default function SetForm({
save,
set,
}: {
set: Set;
save: (set: Set) => void;
}) {
const [name, setName] = useState(set.name);
const [reps, setReps] = useState(set.reps.toString());
const [weight, setWeight] = useState(set.weight.toString());
const [created, setCreated] = useState(new Date(set.created));
const [unit, setUnit] = useState(set.unit);
const [showDate, setShowDate] = useState(false);
const weightRef = useRef<any>(null);
const onConfirm = (date: Date) => {
setCreated(date);
setShowDate(false);
};
const handleSubmit = () => {
save({
name,
reps: Number(reps),
created: created.toISOString(),
weight: Number(weight),
id: set.id,
unit,
});
};
const textInputs = (
<>
<TextInput
style={styles.marginBottom}
label="Name"
value={name}
onChangeText={setName}
autoCorrect={false}
/>
<TextInput
style={styles.marginBottom}
label="Reps"
keyboardType="numeric"
value={reps}
onChangeText={setReps}
onSubmitEditing={() => weightRef.current?.focus()}
2022-08-06 03:10:48 +00:00
selection={{start: 0, end: set.reps.toString().length}}
autoFocus
2022-07-20 03:48:48 +00:00
/>
<TextInput
style={styles.marginBottom}
label="Weight"
keyboardType="numeric"
value={weight}
onChangeText={setWeight}
onSubmitEditing={handleSubmit}
ref={weightRef}
2022-08-04 02:27:07 +00:00
selectTextOnFocus
2022-07-20 03:48:48 +00:00
/>
<TextInput
style={styles.marginBottom}
label="Unit (kg)"
value={unit}
onChangeText={setUnit}
onSubmitEditing={handleSubmit}
/>
</>
);
return (
<>
<ScrollView style={{height: '90%'}}>
{textInputs}
<Button
style={styles.marginBottom}
icon="calendar-outline"
onPress={() => setShowDate(true)}>
{format(created)}
</Button>
<DateTimePickerModal
isVisible={showDate}
mode="datetime"
onConfirm={onConfirm}
onCancel={() => setShowDate(false)}
date={created}
/>
</ScrollView>
<Button mode="contained" icon="save" onPress={handleSubmit}>
Save
</Button>
</>
);
}
const styles = StyleSheet.create({
marginBottom: {
marginBottom: 10,
},
});