-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathApp.tsx
93 lines (84 loc) · 2.64 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import * as React from "react";
import { Text, View, Button, ScrollView } from "react-native";
import { useFormik } from "formik";
import OtherForm from "./OtherForm";
import Input from "./Input";
import { styles } from "./styles";
import { FormDetails, formValidation, FormValues } from "./Validation";
export const mainFormInitialValues = {
name: "",
subForm: {},
};
export default function App() {
// The output of the form
const [result, setResult] = React.useState({});
const [initialValues, setInitialValues] = React.useState<FormValues>(
mainFormInitialValues
);
const [validation, setValidation] = React.useState(formValidation);
// Just save the output of the form to be
const onSubmit = (values: FormValues) => setResult(values);
// Define the formik hook
const formik = useFormik({
initialValues,
validationSchema: validation,
onSubmit: (values) => onSubmit(values),
validateOnBlur: true,
});
// Destructure the formik bag
const {
values,
errors,
touched,
handleChange,
handleSubmit,
validateForm,
handleBlur,
} = formik;
// Any time we dynamically change the validation schema revalidate the
// form
React.useEffect(() => {
validateForm();
}, [validation]);
// If a dynamic form changes then handle the update of the initial values
// and validation schema here
const handleFormChange = (formDetails: FormDetails) => {
// Set the intitial values and validation schema based on the form change
setInitialValues({ ...initialValues, ...formDetails.values });
const newSchema = validation.shape(formDetails.validation);
setValidation(newSchema);
};
return (
<ScrollView>
<View style={styles.container}>
<Input
style={styles.input}
placeholder="name"
onChangeText={handleChange("name")}
onBlur={handleBlur("name")}
value={values.name}
error={errors.name}
touched={touched.name}
/>
<OtherForm
formik={formik}
onChangeForm={(formDetails: FormDetails) =>
handleFormChange(formDetails)
}
/>
<View style={{ width: "100%", marginBottom: 20 }}>
<Button onPress={handleSubmit as any} title="Submit" />
</View>
<Text style={styles.output}>
{"Initial Values: " + JSON.stringify(initialValues, null, 2)}
</Text>
<Text style={styles.output}>
{"Live Values: " + JSON.stringify(values, null, 2)}
</Text>
<Text style={styles.output}>
{"Form Output: " + JSON.stringify(result, null, 2)}
</Text>
</View>
</ScrollView>
);
}