This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBasicForm.tsx
More file actions
125 lines (115 loc) · 3.2 KB
/
BasicForm.tsx
File metadata and controls
125 lines (115 loc) · 3.2 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import * as React from "react";
// react-yup exports two hooks
// in this example, only useForm is shown
// to see an example of useFormBag,
// see Field.tsx in examples/src
import { useForm, useFormBag } from "../../src";
import * as Yup from "yup";
// Define your Yup schema
const schema = Yup.object({
firstName: Yup.string().required().min(2),
lastName: Yup.string().required().min(1),
gender: Yup.number(),
confirm: Yup.boolean().oneOf([true]).required(),
}).defined();
export const BasicForm = () => {
// By providing a schema to validationSchema,
// Typescript will infer your form data and give you auto completion
// for values, touched, errors and some functions like setValues
const {
values,
getValues,
touched,
errors,
field,
createSubmitHandler,
isChecked,
} = useForm({
validationSchema: schema,
});
const handleSubmit = React.useMemo(() => {
return createSubmitHandler(
(values) => {
// form is valid and you have access to values
console.log(values);
},
(errors, values, yupErrors) => {
// form is invalid and you have access to errors
// this callback function is optional
console.log(errors);
console.log(values);
console.log(yupErrors);
}
);
}, []);
return (
<form onSubmit={handleSubmit}>
<pre>
{JSON.stringify(
{
values,
errors,
touched,
},
null,
2
)}
</pre>
<div className="form-group">
<label htmlFor="firstName">First name</label>
{errors.firstName && touched.firstName && <p>{errors.firstName}</p>}
<input
type="text"
className="form-control"
id="firstName"
name="firstName"
value={values.firstName || ""}
onChange={field.onChange}
onBlur={field.onBlur}
/>
</div>
<div className="form-group">
<label htmlFor="lastName">Last name</label>
{errors.lastName && touched.lastName && <p>{errors.lastName}</p>}
<input
type="text"
className="form-control"
id="lastName"
name="lastName"
value={values.lastName || ""}
{...field}
/>
</div>
<div className="form-group">
<label htmlFor="gender">Gender</label>
{errors.gender && touched.gender && <p>{errors.gender}</p>}
<select
className="custom-select"
id="gender"
name="gender"
value={values.gender || ""}
{...field}
>
<option value={1}>Male</option>
<option value={2}>Female</option>
</select>
</div>
<div className="form-group">
{errors.confirm && touched.confirm && <p>{errors.confirm}</p>}
<div className="form-check">
<input
id="confirm"
type="checkbox"
checked={isChecked("confirm")}
name="confirm"
{...field}
/>
<label htmlFor="confirm">Confirm</label>
</div>
</div>
<button className="btn btn-primary" type="submit">
Submit
</button>
</form>
);
};