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 pathNestedForm.tsx
More file actions
150 lines (137 loc) · 4.28 KB
/
NestedForm.tsx
File metadata and controls
150 lines (137 loc) · 4.28 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as React from "react";
import { useForm } from "../../src/index";
import * as Yup from "yup";
import { Field, FieldCheck } from "./Field";
import { RecursivePartial } from "../../src/types";
const schema = Yup.object({
form: Yup.object({
email: Yup.string().email().required("Email is required").defined(),
firstName: Yup.string().required("First name is required").defined(),
lastName: Yup.string(),
address: Yup.object({
number: Yup.number(),
street: Yup.string(),
}),
}).defined(),
shouldValidate: Yup.boolean(),
}).defined();
type FormValues = Yup.InferType<typeof schema>;
interface NestedFormProps {
defaultValues?: RecursivePartial<FormValues>;
}
export const NestedForm: React.FC<NestedFormProps> = ({ defaultValues }) => {
const [validationSchema, setValidationSchema] = React.useState<
Yup.Schema<FormValues> | undefined
>(schema);
const {
createSubmitHandler,
field,
FormProvider,
values,
setValues,
} = useForm<FormValues>({
validationSchema,
defaultValues,
});
React.useEffect(() => {
if (values.shouldValidate) {
setValidationSchema(schema);
} else {
setValidationSchema(void 0);
}
}, [values.shouldValidate]);
const handleSubmit = React.useMemo(() => {
return createSubmitHandler((values) => {
console.log(values);
});
}, [createSubmitHandler]);
return (
<FormProvider>
<div>
<button
onClick={(e) => {
setValues((v) => {
v.form = {
...v.form,
firstName: "test",
};
return v;
});
}}
>
Update first name
</button>
</div>
<FieldCheck
id="shouldValidate"
label="Should validate"
name="shouldValidate"
type="checkbox"
{...field}
/>
<form onSubmit={handleSubmit}>
<Field name="form.email" label="Email Address" type="email" />
<Field name="form.firstName" label="First name" />
<Field name="form.lastName" label="First name" />
<Field name="form.address.street" label="Street" />
<fieldset className="form-group">
<div className="row">
<legend className="col-form-label col-sm-2 pt-0">Gender</legend>
<div className="col-sm-10">
<div className="form-check">
<FieldCheck
id="male"
label="Male"
value="male"
name="form.gender"
type="radio"
{...field}
/>
</div>
<div className="form-check">
<FieldCheck
id="female"
label="Female"
value="female"
name="form.gender"
type="radio"
{...field}
/>
</div>
<div className="form-check">
<FieldCheck
id="unknown"
label="Prefer not to say"
value="unknown"
name="form.gender"
type="radio"
{...field}
/>
</div>
</div>
</div>
</fieldset>
<Field label="Age" name="form.age" type="number" />
<Field label="Range" name="form.range" type="range" />
<Field label="Color" name="form.color" type="color" />
<Field label="Date" name="form.date" type="date" />
<Field
label="DateTime local"
name="form.dateTimeLocal"
type="datetime-local"
/>
<Field label="Time" name="form.time" type="time" />
<Field label="File" name="form.file" type="file" />
<Field label="Month" name="form.month" type="month" />
<Field label="Password" name="form.password" type="password" />
<Field label="Search" name="form.search" type="search" />
<Field label="Telephone" name="form.telephone" type="tel" />
<Field label="URL" name="form.url" type="url" />
<Field label="Week" name="form.week" type="week" />
<button className="btn btn-primary" type="submit">
Submit
</button>
</form>
</FormProvider>
);
};