Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes form reinitialization on language change (#3229) #3249

157 changes: 92 additions & 65 deletions client/modules/User/components/LoginForm.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Form, Field } from 'react-final-form';
import { useDispatch } from 'react-redux';
Expand All @@ -8,92 +8,119 @@ import { validateLogin } from '../../../utils/reduxFormUtils';
import { validateAndLoginUser } from '../actions';

function LoginForm() {
const { t } = useTranslation();
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);
};
useEffect(() => {
const form = formRef.current;
if (!form) return;

const { values } = form.getState(); // store current form touched values
form.reset();

// Restore prev form values and trigger validation
Object.keys(values).forEach((field) => {
if (values[field]) {
// Only reapply touched values
form.change(field, values[field]);
}
});
}, [i18n.language]);

return (
<Form
fields={['email', 'password']}
validate={validateLogin}
onSubmit={onSubmit}
>
{({ handleSubmit, submitError, submitting, modifiedSinceLastSubmit }) => (
<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">
{({
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.PasswordARIA')}
type={showPassword ? 'text' : 'password'}
id="password"
autoComplete="current-password"
aria-label={t('LoginForm.UsernameOrEmailARIA')}
type="text"
id="email"
autoComplete="username"
autoCapitalize="none"
{...field.input}
/>
<button
className="form__eye__icon"
type="button"
onClick={handleVisibility}
aria-hidden="true"
>
{showPassword ? (
<AiOutlineEyeInvisible />
) : (
<AiOutlineEye />
)}
</button>
{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.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>
)}
</Field>
{submitError && !modifiedSinceLastSubmit && (
<span className="form-error">{submitError}</span>
)}
<Button type="submit" disabled={submitting}>
{t('LoginForm.Submit')}
</Button>
</form>
)}
<Button type="submit" disabled={submitting}>
{t('LoginForm.Submit')}
</Button>
</form>
);
}}
</Form>
);
}
Expand Down
Loading
Loading