-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginForm.js
62 lines (56 loc) · 1.46 KB
/
LoginForm.js
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
import React, { useState } from 'react'
import PT from 'prop-types'
const initialFormValues = {
username: '',
password: '',
}
export default function LoginForm(props) {
const [values, setValues] = useState(initialFormValues)
// ✨ where are my props? Destructure them here
const { login } = props;
const onChange = evt => {
const { id, value } = evt.target
setValues({ ...values, [id]: value })
}
const onSubmit = evt => {
evt.preventDefault()
// ✨ implement
//console.log(values);
login(values);
}
const isDisabled = () => {
// ✨ implement
// Trimmed username must be >= 3, and
// trimmed password must be >= 8 for
// the button to become enabled
if(values.username.trim().length >= 3 && values.password.trim() >= 8) {
return false;
} else {
return true;
}
}
return (
<form id="loginForm" onSubmit={onSubmit}>
<h2>Login</h2>
<input
maxLength={20}
value={values.username}
onChange={onChange}
placeholder="Enter username"
id="username"
/>
<input
maxLength={20}
value={values.password}
onChange={onChange}
placeholder="Enter password"
id="password"
/>
<button disabled={isDisabled()} id="submitCredentials">Submit credentials</button>
</form>
)
}
// 🔥 No touchy: LoginForm expects the following props exactly:
LoginForm.propTypes = {
login: PT.func.isRequired,
}