-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.jsx
More file actions
202 lines (188 loc) · 6.24 KB
/
Copy pathApp.jsx
File metadata and controls
202 lines (188 loc) · 6.24 KB
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
import { useState } from 'react'
import './App.css'
function App() {
const [formData, setFormData] = useState({
fullName: '',
email: '',
password: '',
role: '',
agreeToTerms: false,
})
const [errors, setErrors] = useState({})
const [isSubmitted, setIsSubmitted] = useState(false)
const handleChange = (e) => {
const { name, value, type, checked } = e.target
setFormData((prev) => ({
...prev,
[name]: type === 'checkbox' ? checked : value,
}))
}
const validate = () => {
const newErrors = {}
if (!formData.fullName.trim()) {
newErrors.fullName = 'Full name is required'
}
if (!formData.email.trim()) {
newErrors.email = 'Email address is required'
} else if (!/\S+@\S+\.\S+/.test(formData.email)) {
newErrors.email = 'Email address is invalid'
}
if (!formData.password) {
newErrors.password = 'Password is required'
} else if (formData.password.length < 6) {
newErrors.password = 'Password must be at least 6 characters long'
}
if (!formData.role) {
newErrors.role = 'Please select a role'
}
if (!formData.agreeToTerms) {
newErrors.agreeToTerms = 'You must agree to the terms and conditions'
}
return newErrors
}
const handleSubmit = (e) => {
e.preventDefault()
const validationErrors = validate()
if (Object.keys(validationErrors).length === 0) {
setIsSubmitted(true)
setErrors({})
} else {
setErrors(validationErrors)
setIsSubmitted(false)
}
}
const handleReset = () => {
setFormData({
fullName: '',
email: '',
password: '',
role: '',
agreeToTerms: false,
})
setIsSubmitted(false)
setErrors({})
}
return (
<div className="app-container">
<header className="app-header">
<h1>Learn Playwright Automation</h1>
<p>Use Playwright to automate filling and submitting this form!</p>
</header>
<main className="form-card">
{isSubmitted ? (
<div className="success-message" id="success-container">
<div className="success-icon">✓</div>
<h2>Registration Successful!</h2>
<p>
Thank you, <strong id="display-name">{formData.fullName}</strong>!
</p>
<p>
Your account has been successfully created. We've sent a confirmation email to{' '}
<strong id="display-email">{formData.email}</strong> as a{' '}
<strong id="display-role">{formData.role}</strong>.
</p>
<button onClick={handleReset} className="btn-secondary" id="reset-btn">
Register Another Account
</button>
</div>
) : (
<form onSubmit={handleSubmit} noValidate>
<div className="form-group">
<label htmlFor="fullName">Full Name</label>
<input
type="text"
id="fullName"
name="fullName"
placeholder="John Doe"
value={formData.fullName}
onChange={handleChange}
className={errors.fullName ? 'input-error' : ''}
/>
{errors.fullName && (
<span className="error-text" id="fullName-error">
{errors.fullName}
</span>
)}
</div>
<div className="form-group">
<label htmlFor="email">Email Address</label>
<input
type="email"
id="email"
name="email"
placeholder="john.doe@example.com"
value={formData.email}
onChange={handleChange}
className={errors.email ? 'input-error' : ''}
/>
{errors.email && (
<span className="error-text" id="email-error">
{errors.email}
</span>
)}
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input
type="password"
id="password"
name="password"
placeholder="At least 6 characters"
value={formData.password}
onChange={handleChange}
className={errors.password ? 'input-error' : ''}
/>
{errors.password && (
<span className="error-text" id="password-error">
{errors.password}
</span>
)}
</div>
<div className="form-group">
<label htmlFor="role">Preferred Role</label>
<select
id="role"
name="role"
value={formData.role}
onChange={handleChange}
className={errors.role ? 'input-error' : ''}
>
<option value="">-- Select a role --</option>
<option value="developer">Developer</option>
<option value="designer">Designer</option>
<option value="manager">Product Manager</option>
<option value="other">Other</option>
</select>
{errors.role && (
<span className="error-text" id="role-error">
{errors.role}
</span>
)}
</div>
<div className="form-group checkbox-group">
<label className="checkbox-label" htmlFor="agreeToTerms">
<input
type="checkbox"
id="agreeToTerms"
name="agreeToTerms"
checked={formData.agreeToTerms}
onChange={handleChange}
/>
<span>I agree to the terms and conditions</span>
</label>
{errors.agreeToTerms && (
<span className="error-text" id="agreeToTerms-error">
{errors.agreeToTerms}
</span>
)}
</div>
<button type="submit" className="btn-primary" id="submit-btn">
Submit Form
</button>
</form>
)}
</main>
</div>
)
}
export default App