-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathApp.tsx
155 lines (135 loc) · 4.25 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
148
149
150
151
152
153
154
155
import { useEffect, useRef, useState } from 'react';
import { createRoot } from 'react-dom/client';
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);
const password = useRef<HTMLInputElement>(null);
const passwordConfirm = useRef<HTMLInputElement>(null);
const [errors, setErrors] = useState<Errors>({
email: [],
password: [],
passwordConfirm: []
});
const [isSubmitted, setIsSubmitted] = useState(false);
useEffect(() => {
if (isSubmitted) {
if (!hasErrors(errors)) {
alert('Valid form');
} else {
alert('Invalid form');
}
setIsSubmitted(false);
}
}, [isSubmitted, errors]);
function handleEmailChange({ target }: React.ChangeEvent<HTMLInputElement>) {
const { value } = target;
setErrors(prevState => {
return {
...prevState,
email: validateEmail(value)
};
});
}
function handlePasswordChange({ target }: React.ChangeEvent<HTMLInputElement>) {
const { value } = target;
setErrors(prevState => {
return {
...prevState,
password: validatePassword(value),
passwordConfirm: validatePasswordConfirm(value, passwordConfirm.current!.value)
};
});
}
function handlePasswordConfirmChange({ target }: React.ChangeEvent<HTMLInputElement>) {
const { value } = target;
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);
}
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 type="submit">Sign Up</button>
</form>
);
}
const root = createRoot(document.getElementById('app')!);
root.render(<Form />);