-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPersonalInfo.jsx
168 lines (150 loc) · 4.31 KB
/
PersonalInfo.jsx
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import styled from "styled-components";
import Heading from "../ui/Heading";
import SubHeading from "../ui/SubHeading";
import FormBody from "../ui/FormBody";
import FormActions from "../ui/FormActions";
import Button from "../ui/Button";
import { addPersonalInfoData } from "./formSlice";
import { useDispatch, useSelector } from "react-redux";
import { useForm } from "react-hook-form";
import FormInputs from "../ui/FormInputs";
import { breakpoints } from "../styles/GlobalStyles";
import InputErrorMessage from "../ui/InputErrorMessage";
const phoneRegex = /^[\d\s()+-]{7,15}$/;
const emailRegex = /\S+@\S+\.\S+/;
const InputWrapper = styled.div`
min-height: 5.5rem;
align-content: start;
display: grid;
grid-template-areas:
"label"
"input"
"error";
@media screen and (min-width: ${breakpoints.md}) {
grid-template-areas:
"label error"
"input input";
grid-template-columns: auto 1fr;
}
label {
font-size: 0.9rem;
font-weight: 500;
grid-area: label;
}
p {
grid-area: error;
}
`;
const Input = styled.input.withConfig({
shouldForwardProp: (prop) => "error" !== prop,
})`
padding: 0.5rem 1rem;
outline: none;
width: 100%;
border: 2px solid var(--light-gray);
border-radius: 5px;
font-weight: 500;
grid-area: input;
border-color: ${(props) =>
props.error === "true" ? "var(--strawberry-red)" : ""};
&:focus {
border-color: var(--marine-blue);
border-color: ${(props) =>
props.error === "true" ? "var(--strawberry-red)" : ""};
}
&[type="number"]::-webkit-outer-spin-button,
&[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
&[type="number"] {
-moz-appearance: textfield;
}
`;
function PersonalInfo() {
const dispatch = useDispatch();
const { name, email, phone } = useSelector((state) => state.form);
const {
register,
handleSubmit,
formState: { errors },
} = useForm({
defaultValues: { name, email, phone },
});
function handleNext(data) {
dispatch(addPersonalInfoData(data));
}
return (
<FormBody onSubmit={handleSubmit(handleNext)} noValidate>
<FormInputs>
<Heading>Personal info</Heading>
<SubHeading>
Please provide your name, email address, and phone number
</SubHeading>
<InputWrapper>
<label htmlFor="name">Name</label>
<Input
error={`${!!errors.name}`}
autoComplete="off"
type="text"
name="name"
id="name"
placeholder="e.g Stephen King"
{...register("name", { required: "Name is required" })}
/>
{errors.name && (
<InputErrorMessage>{errors.name.message}</InputErrorMessage>
)}
</InputWrapper>
<InputWrapper>
<label htmlFor="email">Email Address</label>
<Input
error={`${!!errors.email}`}
autoComplete="off"
type="email"
name="email"
id="email"
placeholder="e.g stephenking.lorem.com"
{...register("email", {
required: "Email address is required",
pattern: {
value: emailRegex,
message: "Please provide your valid email address.",
},
})}
/>
{errors.email && (
<InputErrorMessage>{errors.email.message}</InputErrorMessage>
)}
</InputWrapper>
<InputWrapper>
<label htmlFor="phone">Phone Number</label>
<Input
error={`${!!errors.phone}`}
autoComplete="off"
type="tel"
name="phone"
id="phone"
placeholder="e.g +1 234 567 890"
{...register("phone", {
required: "Phone number is required",
pattern: {
value: /^[\d\s()+-]{7,15}$/,
message: "Please provide a valid mobile number.",
},
})}
/>
{errors.phone && (
<InputErrorMessage>{errors.phone.message}</InputErrorMessage>
)}
</InputWrapper>
</FormInputs>
<FormActions>
<Button type="submit" position="right">
Next step
</Button>
</FormActions>
</FormBody>
);
}
export default PersonalInfo;