-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRegistrationForm.jsx
254 lines (248 loc) · 6.78 KB
/
RegistrationForm.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import {
Box,
Button,
CircularProgress,
FormControl,
FormHelperText,
Grid2 as Grid,
MenuItem,
TextField
} from "@mui/material";
import { Controller } from "react-hook-form";
import PropTypes from "prop-types";
import React from "react";
import { useMaterialForm } from "../utils/form";
import zxcvbn from "zxcvbn";
/**
* Registration form
*/
export default function RegistrationForm({
loading = false,
teams,
onRegister
}) {
// form state
const {
register,
formState: { errors },
handleSubmit,
watch,
control
} = useMaterialForm({
mode: "onTouched"
});
// watch password so we can validate its duplicate
const password = React.useRef({});
password.current = watch("password", "");
// create score ref so that we avoid multiple calculations
const score = React.useRef();
React.useEffect(() => {
if (!password.current.length) {
score.current = null;
}
});
// return components
return (
<form onSubmit={handleSubmit(onRegister)} noValidate>
<Grid container spacing={2}>
<Grid
size={{
sm: 6,
xs: 12
}}
>
<TextField
id="firstName"
autoComplete="fname"
name="firstName"
variant="outlined"
required
fullWidth
label="First Name"
autoFocus={!loading}
slotProps={{ input: { "aria-label": "firstName" } }}
error={Boolean(errors.firstName)}
disabled={loading}
{...register("firstName", { required: true })}
/>
</Grid>
<Grid
size={{
sm: 6,
xs: 12
}}
>
<TextField
id="lastName"
variant="outlined"
required
fullWidth
label="Last Name"
autoComplete="lname"
slotProps={{ input: { "aria-label": "lastName" } }}
error={Boolean(errors.lastName)}
disabled={loading}
{...register("lastName", { required: true })}
/>
</Grid>
<Grid size={12}>
<TextField
id="email"
variant="outlined"
required
fullWidth
label="Email Address"
autoComplete="username"
slotProps={{ input: { "aria-label": "email" } }}
error={Boolean(errors.email)}
helperText={errors?.email?.message}
disabled={loading}
{...register("email", {
pattern: {
message: "Please enter a valid email address",
value:
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
},
required: true
})}
/>
</Grid>
<Grid size={12}>
<FormControl
required
variant="outlined"
error={Boolean(errors.team)}
fullWidth
disabled={loading}
>
<Controller
render={({ field }) => (
<TextField
select
data-testid="team"
id="team"
variant="outlined"
label="Team"
required
fullWidth
disabled={loading}
value={field.value || ""}
error={Boolean(errors.team)}
{...field}
>
{teams.map(team => (
<MenuItem value={team} key={team}>
{team}
</MenuItem>
))}
</TextField>
)}
name="team"
rules={{ required: true }}
control={control}
defaultValue=""
/>
</FormControl>
</Grid>
<Grid size={12}>
<TextField
variant="outlined"
required
fullWidth
id="password"
label="Password"
type="password"
autoComplete="new-password"
slotProps={{ input: { "aria-label": "password" } }}
error={Boolean(errors.password)}
helperText={errors?.password?.message}
disabled={loading}
{...register("password", {
required: true,
validate: value => {
const result = zxcvbn(value);
score.current = result.score;
return (
result.score > 2 ||
result.feedback.warning ||
result.feedback.suggestions[0]
);
}
})}
/>
{score.current != null && (
<FormHelperText
style={{ marginLeft: 14, marginRight: 14 }}
error={score.current <= 2}
>
Password strength: {score.current}/4
{score.current <= 2 && ". Minimum required 3+."}
</FormHelperText>
)}
</Grid>
<Grid size={12}>
<TextField
variant="outlined"
required
fullWidth
id="passwordRepeat"
label="Confirm Password"
type="password"
autoComplete="new-password"
slotProps={{ input: { "aria-label": "passwordRepeat" } }}
error={Boolean(errors.passwordRepeat)}
helperText={errors?.passwordRepeat?.message}
disabled={loading}
{...register("passwordRepeat", {
required: true,
validate: value =>
value === password.current || "Password does not match"
})}
/>
</Grid>
</Grid>
<Box
sx={{
mb: 1,
mt: 2
}}
>
<Button
type="submit"
fullWidth
id="submit"
variant="contained"
color="primary"
disabled={loading}
endIcon={loading ? <CircularProgress size={24} /> : null}
>
Register
</Button>
</Box>
</form>
);
}
// prop types
RegistrationForm.propTypes = {
/**
* Loading state of the login form. Disables submit button and shows loading indicator.
*/
loading: PropTypes.bool,
/**
* Callback function fired when user submits form.
*
* **Signature**
* ```
* function(data, event) => void
* ```
*
* _data_: Object containing _firstName, lastName, email, team, password, passwordRepeat_
*
* _event_: Synthetic event
*/
onRegister: PropTypes.func.isRequired,
/**
* List of teams that user can select from.
*/
teams: PropTypes.arrayOf(PropTypes.string).isRequired
};