-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
81 lines (73 loc) · 1.99 KB
/
index.js
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
import React from "react"
import { useForm } from "react-hook-form"
export default function App() {
const {
register,
handleSubmit,
formState: { errors },
} = useForm()
const onSubmit = data => {
fetch(`/api/form`, {
method: `POST`,
body: JSON.stringify(data),
headers: {
"content-type": `application/json`,
},
})
.then(res => res.json())
.then(body => {
console.log(`response from API:`, body)
})
}
console.log({ errors })
return (
<form
onSubmit={handleSubmit(onSubmit)}
style={{ display: `block`, width: 400 }}
>
<label htmlFor="first-name">First name</label>
<input
id="first-name"
type="text"
style={{ display: `block`, marginBottom: 16 }}
{...register("First name", { required: true, maxLength: 80 })}
/>
<label htmlFor="last-name">Last name</label>
<input
id="last-name"
type="text"
style={{ display: `block`, marginBottom: 16 }}
{...register("Last name", { required: true, maxLength: 100 })}
/>
<label htmlFor="email">Email</label>
<input
id="email"
type="text"
style={{ display: `block`, marginBottom: 16 }}
{...register("Email", { required: true, pattern: /^\S+@\S+$/i })}
/>
<label htmlFor="tel">Mobile number</label>
<input
id="tel"
type="tel"
style={{ display: `block`, marginBottom: 16 }}
{...register("Mobile number", {
required: true,
minLength: 6,
maxLength: 12,
})}
/>
<label htmlFor="title">Title</label>
<select
{...register("Title", { required: true })}
style={{ display: `block`, marginBottom: 16 }}
>
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss">Miss</option>
<option value="Dr">Dr</option>
</select>
<input type="submit" />
</form>
)
}