|
1 | | -import React, { useState } from 'react'; |
| 1 | +import React, { useRef, useState } from 'react'; |
2 | 2 | import { useTranslation } from 'react-i18next'; |
3 | 3 | import { Form, Field } from 'react-final-form'; |
4 | 4 | import { useDispatch } from 'react-redux'; |
5 | 5 | import { AiOutlineEye, AiOutlineEyeInvisible } from 'react-icons/ai'; |
6 | 6 | import Button from '../../../common/Button'; |
7 | 7 | import { validateLogin } from '../../../utils/reduxFormUtils'; |
8 | 8 | import { validateAndLoginUser } from '../actions'; |
| 9 | +import { useSyncFormTranslations } from '../../../common/useSyncFormTranslations'; |
9 | 10 |
|
10 | 11 | function LoginForm() { |
11 | | - const { t } = useTranslation(); |
| 12 | + const { t, i18n } = useTranslation(); |
12 | 13 |
|
13 | 14 | const dispatch = useDispatch(); |
14 | 15 | function onSubmit(formProps) { |
15 | 16 | return dispatch(validateAndLoginUser(formProps)); |
16 | 17 | } |
17 | 18 | const [showPassword, setShowPassword] = useState(false); |
| 19 | + const formRef = useRef(null); |
| 20 | + |
18 | 21 | const handleVisibility = () => { |
19 | 22 | setShowPassword(!showPassword); |
20 | 23 | }; |
21 | 24 |
|
| 25 | + useSyncFormTranslations(formRef, i18n.language); |
| 26 | + |
22 | 27 | return ( |
23 | 28 | <Form |
24 | 29 | fields={['email', 'password']} |
25 | 30 | validate={validateLogin} |
26 | 31 | onSubmit={onSubmit} |
27 | 32 | > |
28 | | - {({ handleSubmit, submitError, submitting, modifiedSinceLastSubmit }) => ( |
29 | | - <form className="form" onSubmit={handleSubmit}> |
30 | | - <Field name="email"> |
31 | | - {(field) => ( |
32 | | - <div className="form__field"> |
33 | | - <label htmlFor="email" className="form__label"> |
34 | | - {t('LoginForm.UsernameOrEmail')} |
35 | | - </label> |
36 | | - <input |
37 | | - className="form__input" |
38 | | - aria-label={t('LoginForm.UsernameOrEmailARIA')} |
39 | | - type="text" |
40 | | - id="email" |
41 | | - autoComplete="username" |
42 | | - autoCapitalize="none" |
43 | | - {...field.input} |
44 | | - /> |
45 | | - {field.meta.touched && field.meta.error && ( |
46 | | - <span className="form-error" aria-live="polite"> |
47 | | - {field.meta.error} |
48 | | - </span> |
49 | | - )} |
50 | | - </div> |
51 | | - )} |
52 | | - </Field> |
53 | | - <Field name="password"> |
54 | | - {(field) => ( |
55 | | - <div className="form__field"> |
56 | | - <label htmlFor="password" className="form__label"> |
57 | | - {t('LoginForm.Password')} |
58 | | - </label> |
59 | | - <div className="form__field__password"> |
| 33 | + {({ |
| 34 | + handleSubmit, |
| 35 | + submitError, |
| 36 | + submitting, |
| 37 | + modifiedSinceLastSubmit, |
| 38 | + form |
| 39 | + }) => { |
| 40 | + formRef.current = form; |
| 41 | + |
| 42 | + return ( |
| 43 | + <form className="form" onSubmit={handleSubmit}> |
| 44 | + <Field name="email"> |
| 45 | + {(field) => ( |
| 46 | + <div className="form__field"> |
| 47 | + <label htmlFor="email" className="form__label"> |
| 48 | + {t('LoginForm.UsernameOrEmail')} |
| 49 | + </label> |
60 | 50 | <input |
61 | 51 | className="form__input" |
62 | | - aria-label={t('LoginForm.PasswordARIA')} |
63 | | - type={showPassword ? 'text' : 'password'} |
64 | | - id="password" |
65 | | - autoComplete="current-password" |
| 52 | + aria-label={t('LoginForm.UsernameOrEmailARIA')} |
| 53 | + type="text" |
| 54 | + id="email" |
| 55 | + autoComplete="username" |
| 56 | + autoCapitalize="none" |
66 | 57 | {...field.input} |
67 | 58 | /> |
68 | | - <button |
69 | | - className="form__eye__icon" |
70 | | - type="button" |
71 | | - onClick={handleVisibility} |
72 | | - aria-hidden="true" |
73 | | - > |
74 | | - {showPassword ? ( |
75 | | - <AiOutlineEyeInvisible /> |
76 | | - ) : ( |
77 | | - <AiOutlineEye /> |
78 | | - )} |
79 | | - </button> |
| 59 | + {field.meta.touched && field.meta.error && ( |
| 60 | + <span className="form-error" aria-live="polite"> |
| 61 | + {field.meta.error} |
| 62 | + </span> |
| 63 | + )} |
| 64 | + </div> |
| 65 | + )} |
| 66 | + </Field> |
| 67 | + <Field name="password"> |
| 68 | + {(field) => ( |
| 69 | + <div className="form__field"> |
| 70 | + <label htmlFor="password" className="form__label"> |
| 71 | + {t('LoginForm.Password')} |
| 72 | + </label> |
| 73 | + <div className="form__field__password"> |
| 74 | + <input |
| 75 | + className="form__input" |
| 76 | + aria-label={t('LoginForm.PasswordARIA')} |
| 77 | + type={showPassword ? 'text' : 'password'} |
| 78 | + id="password" |
| 79 | + autoComplete="current-password" |
| 80 | + {...field.input} |
| 81 | + /> |
| 82 | + <button |
| 83 | + className="form__eye__icon" |
| 84 | + type="button" |
| 85 | + onClick={handleVisibility} |
| 86 | + aria-hidden="true" |
| 87 | + > |
| 88 | + {showPassword ? ( |
| 89 | + <AiOutlineEyeInvisible /> |
| 90 | + ) : ( |
| 91 | + <AiOutlineEye /> |
| 92 | + )} |
| 93 | + </button> |
| 94 | + </div> |
| 95 | + {field.meta.touched && field.meta.error && ( |
| 96 | + <span className="form-error" aria-live="polite"> |
| 97 | + {field.meta.error} |
| 98 | + </span> |
| 99 | + )} |
80 | 100 | </div> |
81 | | - {field.meta.touched && field.meta.error && ( |
82 | | - <span className="form-error" aria-live="polite"> |
83 | | - {field.meta.error} |
84 | | - </span> |
85 | | - )} |
86 | | - </div> |
| 101 | + )} |
| 102 | + </Field> |
| 103 | + {submitError && !modifiedSinceLastSubmit && ( |
| 104 | + <span className="form-error">{submitError}</span> |
87 | 105 | )} |
88 | | - </Field> |
89 | | - {submitError && !modifiedSinceLastSubmit && ( |
90 | | - <span className="form-error">{submitError}</span> |
91 | | - )} |
92 | | - <Button type="submit" disabled={submitting}> |
93 | | - {t('LoginForm.Submit')} |
94 | | - </Button> |
95 | | - </form> |
96 | | - )} |
| 106 | + <Button type="submit" disabled={submitting}> |
| 107 | + {t('LoginForm.Submit')} |
| 108 | + </Button> |
| 109 | + </form> |
| 110 | + ); |
| 111 | + }} |
97 | 112 | </Form> |
98 | 113 | ); |
99 | 114 | } |
|
0 commit comments