-
Notifications
You must be signed in to change notification settings - Fork 20
/
App.tsx
147 lines (127 loc) · 4.27 KB
/
App.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
import React, { useState, useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
import './index.html';
import './style.css';
// Inspired by
// - Validating a React form upon submit https://goshakkk.name/submit-time-validation-react/
// - How to do Simple Form Validation in #Reactjs https://learnetto.com/blog/how-to-do-simple-form-validation-in-reactjs
interface Errors {
email: string[];
password: string[];
passwordConfirm: string[];
}
function validateEmail(email: string) {
const errors = [] as string[];
if (email.length === 0) errors.push("Can't be empty");
if (!email.includes('@')) errors.push('Should contain @');
return errors;
}
function validatePassword(password: string) {
const errors = [] as string[];
if (password.length === 0) errors.push("Can't be empty");
if (password.length < 5) errors.push('Should be at least 5 characters long');
return errors;
}
function validatePasswordConfirm(password: string, passwordConfirm: string) {
const errors = [] as string[];
if (passwordConfirm !== password) errors.push('Not the same password');
return errors;
}
function hasErrors(errors: Errors) {
return errors.email.length > 0 || errors.password.length > 0 || errors.passwordConfirm.length > 0;
}
function Form() {
const email = useRef<HTMLInputElement | null>(null);
const password = useRef<HTMLInputElement | null>(null);
const passwordConfirm = useRef<HTMLInputElement | null>(null);
const [errors, setErrors] = useState<Errors>({
email: [],
password: [],
passwordConfirm: []
});
function handleEmailChange(e: React.ChangeEvent<HTMLInputElement>) {
const value = e.target.value;
setErrors(prevState => {
return {
...prevState,
email: validateEmail(value)
};
});
}
function handlePasswordChange(e: React.ChangeEvent<HTMLInputElement>) {
const value = e.target.value;
setErrors(prevState => {
return {
...prevState,
password: validatePassword(value),
passwordConfirm: validatePasswordConfirm(value, passwordConfirm.current!.value)
};
});
}
function handlePasswordConfirmChange(e: React.ChangeEvent<HTMLInputElement>) {
const value = e.target.value;
setErrors(prevState => {
return {
...prevState,
passwordConfirm: validatePasswordConfirm(password.current!.value, value)
};
});
}
function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
setErrors(prevState => {
return {
...prevState,
email: validateEmail(email.current!.value),
password: validatePassword(password.current!.value),
passwordConfirm: validatePasswordConfirm(password.current!.value, passwordConfirm.current!.value)
};
});
setIsSubmitted(true);
}
// FIXME See [SetStateAction returned from useState hook does not accept a second callback argument](https://github.com/facebook/react/issues/14174)
const [isSubmitted, setIsSubmitted] = useState(false);
useEffect(() => {
if (isSubmitted) {
if (!hasErrors(errors)) {
alert('Valid form');
} else {
alert('Invalid form');
}
setIsSubmitted(false);
}
});
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="email">Email</label>
<input id="email"
ref={email}
onChange={handleEmailChange} />
<div className="error">
{errors.email.map(error => <div key={error}>{error}</div>)}
</div>
</div>
<div>
<label htmlFor="password">Password</label>
<input type="password" id="password"
ref={password}
onChange={handlePasswordChange} />
<div className="error">
{errors.password.map(error => <div key={error}>{error}</div>)}
</div>
</div>
<div>
<label htmlFor="password-confirm">Confirm Password</label>
<input type="password" id="password-confirm"
ref={passwordConfirm}
onChange={handlePasswordConfirmChange} />
<div className="error">
{errors.passwordConfirm.map(error => <div key={error}>{error}</div>)}
</div>
</div>
<button>Sign Up</button>
</form>
);
}
ReactDOM.render(<Form />, document.getElementById('app'));