forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoginForm.jsx
116 lines (109 loc) · 3.81 KB
/
LoginForm.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
import React, { useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Form, Field } from 'react-final-form';
import { useDispatch } from 'react-redux';
import { AiOutlineEye, AiOutlineEyeInvisible } from 'react-icons/ai';
import Button from '../../../common/Button';
import { validateLogin } from '../../../utils/reduxFormUtils';
import { validateAndLoginUser } from '../actions';
import { useSyncFormTranslations } from '../../../common/useSyncFormTranslations';
function LoginForm() {
const { t, i18n } = useTranslation();
const dispatch = useDispatch();
function onSubmit(formProps) {
return dispatch(validateAndLoginUser(formProps));
}
const [showPassword, setShowPassword] = useState(false);
const formRef = useRef(null);
const handleVisibility = () => {
setShowPassword(!showPassword);
};
useSyncFormTranslations(formRef, i18n.language);
return (
<Form
fields={['email', 'password']}
validate={validateLogin}
onSubmit={onSubmit}
>
{({
handleSubmit,
submitError,
submitting,
modifiedSinceLastSubmit,
form
}) => {
formRef.current = form;
return (
<form className="form" onSubmit={handleSubmit}>
<Field name="email">
{(field) => (
<div className="form__field">
<label htmlFor="email" className="form__label">
{t('LoginForm.UsernameOrEmail')}
</label>
<input
className="form__input"
aria-label={t('LoginForm.UsernameOrEmailARIA')}
type="text"
id="email"
autoComplete="username"
autoCapitalize="none"
{...field.input}
/>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
)}
</Field>
<Field name="password">
{(field) => (
<div className="form__field">
<label htmlFor="password" className="form__label">
{t('LoginForm.Password')}
</label>
<div className="form__field__password">
<input
className="form__input"
aria-label={t('LoginForm.PasswordARIA')}
type={showPassword ? 'text' : 'password'}
id="password"
autoComplete="current-password"
{...field.input}
/>
<button
className="form__eye__icon"
type="button"
onClick={handleVisibility}
aria-hidden="true"
>
{showPassword ? (
<AiOutlineEyeInvisible />
) : (
<AiOutlineEye />
)}
</button>
</div>
{field.meta.touched && field.meta.error && (
<span className="form-error" aria-live="polite">
{field.meta.error}
</span>
)}
</div>
)}
</Field>
{submitError && !modifiedSinceLastSubmit && (
<span className="form-error">{submitError}</span>
)}
<Button type="submit" disabled={submitting}>
{t('LoginForm.Submit')}
</Button>
</form>
);
}}
</Form>
);
}
export default LoginForm;