-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
177 lines (151 loc) · 4.41 KB
/
index.tsx
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
169
170
171
172
173
174
175
176
177
import ReactDOM from "react-dom";
import React, { useState } from "react";
import "./styles.css";
interface FormValuesInterface {
firstName: string;
lastName: string;
phoneNumber: string;
receiveNewsletter: boolean;
}
interface ErrorInterface {
message: string;
}
interface ErrorsObjectInterface {
firstName?: ErrorInterface;
lastName?: ErrorInterface;
phoneNumber?: ErrorInterface;
}
interface TextInputProps extends React.HTMLProps<HTMLInputElement> {
name: keyof ErrorsObjectInterface;
value: any;
errors: ErrorsObjectInterface | null | undefined;
}
interface CheckboxInputProps extends React.HTMLProps<HTMLInputElement> {}
// "Server-side" code
const mockPost = (
profile: FormValuesInterface
): Promise<{ profile?: FormValuesInterface; errors?: ErrorsObjectInterface }> =>
new Promise((resolve) => {
const errors: ErrorsObjectInterface = {};
console.log({ profile });
if (!profile.firstName)
errors.firstName = { message: "Missing first name!" };
if (!profile.lastName) errors.lastName = { message: "Missing last name!" };
if (!profile.phoneNumber) {
errors.phoneNumber = { message: "Missing phone number!" };
} else if (profile.phoneNumber.replace(/[^0-9]/, "").length !== 8) {
errors.phoneNumber = {
message: "Phone number must be 8 digits",
};
}
if (Object.keys(errors).length > 0) {
resolve({ errors });
return;
}
resolve({ profile });
});
// Client side code below
const TextInput: React.FC<TextInputProps> = ({
name,
value,
errors,
...props
}) => (
<>
<input name={name} value={value} {...props} />
{errors?.hasOwnProperty(name) && (
<p className="error">{errors?.[name]?.message}</p>
)}
</>
);
const CheckboxInput: React.FC<CheckboxInputProps> = ({ ...props }) => (
<input type="checkbox" {...props} />
);
const ProfileForm: React.FC = () => {
const [formValues, setFormValues] = useState<FormValuesInterface>({
firstName: "",
lastName: "",
phoneNumber: "",
receiveNewsletter: false,
});
const [errors, setErrors] = useState<ErrorsObjectInterface | null>(null);
const { firstName, lastName, phoneNumber, receiveNewsletter } = formValues;
const onInputChange: React.ChangeEventHandler<HTMLInputElement> = (event) => {
const { value, name, checked, type } = event.target;
if (type === "text" || type === "tel") {
setFormValues({ ...formValues, [name]: value });
}
if (type === "checkbox") {
setFormValues({ ...formValues, [name]: checked });
}
};
const onSubmit: React.FormEventHandler<HTMLFormElement> = async (event) => {
event.preventDefault();
setErrors(null);
console.log({ formValues });
const response = await mockPost({
firstName,
lastName,
phoneNumber,
receiveNewsletter,
});
if (response.errors) {
setErrors(response.errors);
} else if (response.profile) {
setFormValues(response.profile);
}
};
return (
<form onSubmit={onSubmit}>
<label htmlFor="firstName">
First name:
<TextInput
id="firstName"
name="firstName"
value={firstName}
onChange={onInputChange}
errors={errors}
required
/>
</label>
<label htmlFor="lastName">
Last name:
<TextInput
id="lastName"
name="lastName"
value={lastName}
onChange={onInputChange}
errors={errors}
required
/>
</label>
<label htmlFor="phoneNumber" id="phoneLabel">
Phone number:
<TextInput
id="phoneNumber"
name="phoneNumber"
type="tel"
value={phoneNumber}
onChange={onInputChange}
errors={errors}
pattern="[0-9]{8}"
title="A valid phone number (8 characters), for example 99882233"
aria-labelledby="phoneLabel phoneDescription"
required
/>
</label>
<span id="phoneDescription">8 characters, for example 99882233</span>
<label htmlFor="receiveNewsletter">
Receive newsletter?
<CheckboxInput
id="receiveNewsletter"
name="receiveNewsletter"
checked={receiveNewsletter}
onChange={onInputChange}
/>
</label>
<button type="submit">Save changes</button>
</form>
);
};
ReactDOM.render(<ProfileForm />, document.getElementById("root"));