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 pathPureForm.tsx
More file actions
62 lines (55 loc) · 1.4 KB
/
PureForm.tsx
File metadata and controls
62 lines (55 loc) · 1.4 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
import * as React from "react";
import * as Yup from "yup";
import { useForm } from "../../src";
import { PureField } from "./Field";
const SCHEMA = Yup.object().shape({
firstName: Yup.string().min(2).required(),
lastName: Yup.string().min(2).required(),
age: Yup.number().required(),
});
/**
* <PureField /> will not rerender unless one of it's props changes
*/
export const PureForm = () => {
const {
field,
getValue,
getError,
isTouched,
values,
createSubmitHandler,
} = useForm({
validationSchema: SCHEMA,
});
const handleSubmit = React.useMemo(() => {
return createSubmitHandler((v) => {
console.log(v);
});
}, [createSubmitHandler]);
return (
<form onSubmit={handleSubmit}>
<PureField
label="First name"
name="firstName"
id="firstName"
{...field}
value={getValue((v) => v.firstName) || ""}
error={getError((e) => e.firstName)}
touched={isTouched((t) => t.firstName)}
/>
<PureField
label="Last name"
name="lastName"
id="lastName"
{...field}
value={getValue((v) => v.lastName) || ""}
error={getError((e) => e.lastName)}
touched={isTouched((t) => t.lastName)}
/>
<pre>{JSON.stringify({ values }, null, 2)}</pre>
<button className="btn btn-primary" type="submit">
Submit
</button>
</form>
);
};