fix(client): focus password after invalid login
- Defer focus until submission finishes and the password field is cleared.
This commit is contained in:
1 parent
92141a25fb
commit
8676d6281c
1 file changed
+9
-1
@@ -14,6 +14,7 @@ export function LoginForm({ expired = false, signedOut = false, onSubmit }: Logi
|
||||
const { t } = useTranslation();
|
||||
const titleRef = useRef<HTMLHeadingElement>(null);
|
||||
const formRef = useRef<HTMLFormElement>(null);
|
||||
const focusPasswordAfterInvalidCredentialsRef = useRef(false);
|
||||
const [username, setUsername] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [usernameError, setUsernameError] = useState('');
|
||||
@@ -25,6 +26,13 @@ export function LoginForm({ expired = false, signedOut = false, onSubmit }: Logi
|
||||
titleRef.current?.focus();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!submitting && focusPasswordAfterInvalidCredentialsRef.current) {
|
||||
focusPasswordAfterInvalidCredentialsRef.current = false;
|
||||
formRef.current?.querySelector<HTMLInputElement>('input[name="password"]')?.focus();
|
||||
}
|
||||
}, [submitting]);
|
||||
|
||||
function focusInput(name: string) {
|
||||
formRef.current?.querySelector<HTMLInputElement>(`input[name="${name}"]`)?.focus();
|
||||
}
|
||||
@@ -49,7 +57,7 @@ export function LoginForm({ expired = false, signedOut = false, onSubmit }: Logi
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
setFormError(t('auth.invalid_credentials'));
|
||||
setPassword('');
|
||||
focusInput('password');
|
||||
focusPasswordAfterInvalidCredentialsRef.current = true;
|
||||
} else {
|
||||
setFormError(t('auth.sign_in_failed'));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user