Files
zitadel/apps/login/src/components/username-form.tsx
T
e25220bfab feat(login): add autoFocus to input components (#11566)
<!--
Please inform yourself about the contribution guidelines on submitting a
PR here:
https://github.com/zitadel/zitadel/blob/main/CONTRIBUTING.md#submit-a-pull-request-pr.
Take note of how PR/commit titles should be written and replace the
template texts in the sections below. Don't remove any of the sections.
It is important that the commit history clearly shows what is changed
and why.
Important: By submitting a contribution you agree to the terms from our
Licensing Policy as described here:
https://github.com/zitadel/zitadel/blob/main/LICENSING.md#community-contributions.
-->

# Which Problems Are Solved

The first input field on login screens is not automatically focused,
requiring users to manually click before typing. This creates
unnecessary friction during the login flow, particularly for
keyboard-oriented users and accessibility scenarios.


# How the Problems Are Solved

- Added the `autoFocus` prop to the first `TextInput` in all 12 login
form components.
- For forms with conditionally rendered first inputs
(`set-password-form` and `register-form-idp-incomplete`), `autoFocus` is
set dynamically so the correct field receives focus depending on which
input is visible.

# Additional Changes

None

# Additional Context

- Closes #11565

Co-authored-by: Max Peintner <[email protected]>
2026-02-19 09:28:48 +00:00

157 lines
4.7 KiB
TypeScript

"use client";
import { sendLoginname } from "@/lib/server/loginname";
import { handleServerActionResponse } from "@/lib/client";
import { LoginSettings } from "@zitadel/proto/zitadel/settings/v2/login_settings_pb";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { useForm } from "react-hook-form";
import { Alert } from "./alert";
import { BackButton } from "./back-button";
import { Button, ButtonVariants } from "./button";
import { TextInput } from "./input";
import { Spinner } from "./spinner";
import { Translated } from "./translated";
import { useTranslations } from "next-intl";
import { AutoSubmitForm } from "./auto-submit-form";
type Inputs = {
loginName: string;
};
type Props = {
loginName: string | undefined;
requestId: string | undefined;
loginSettings: LoginSettings | undefined;
organization?: string;
defaultOrganization?: string;
suffix?: string;
submit: boolean;
allowRegister: boolean;
};
export function UsernameForm({
loginName,
requestId,
organization,
defaultOrganization,
suffix,
loginSettings,
submit,
allowRegister,
}: Props) {
const { register, handleSubmit, formState } = useForm<Inputs>({
mode: "onChange",
defaultValues: {
loginName: loginName ? loginName : "",
},
});
const t = useTranslations("loginname");
const router = useRouter();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string>("");
const [samlData, setSamlData] = useState<{ url: string; fields: Record<string, string> } | null>(null);
async function submitLoginName(values: Inputs, organization?: string) {
setLoading(true);
try {
const res = await sendLoginname({
loginName: values.loginName,
organization,
defaultOrganization,
requestId,
suffix,
ignoreUnknownUsernames: loginSettings?.ignoreUnknownUsernames,
});
handleServerActionResponse(res, router, setSamlData, setError);
return res;
} catch {
setError(t("errors.internalError"));
} finally {
setLoading(false);
}
}
useEffect(() => {
if (submit && loginName) {
// When we navigate to this page, we always want to be redirected if submit is true and the parameters are valid.
submitLoginName({ loginName }, organization);
}
}, []);
let inputLabel = t("labels.loginname");
if (loginSettings?.disableLoginWithEmail && loginSettings?.disableLoginWithPhone) {
inputLabel = t("labels.username");
} else if (loginSettings?.disableLoginWithEmail) {
inputLabel = t("labels.usernameOrPhoneNumber");
} else if (loginSettings?.disableLoginWithPhone) {
inputLabel = t("labels.usernameOrEmail");
}
return (
<>
{samlData && <AutoSubmitForm url={samlData.url} fields={samlData.fields} />}
<form className="w-full">
<div className="">
<TextInput
type="text"
autoComplete="username"
autoFocus
{...register("loginName", { required: t("required.loginName") })}
label={inputLabel}
data-testid="username-text-input"
suffix={suffix}
/>
{allowRegister && (
<button
className="text-sm transition-all hover:text-primary-light-500 dark:hover:text-primary-dark-500"
onClick={() => {
const registerParams = new URLSearchParams();
if (organization) {
registerParams.append("organization", organization);
}
if (requestId) {
registerParams.append("requestId", requestId);
}
router.push("/register?" + registerParams);
}}
type="button"
disabled={loading}
data-testid="register-button"
>
<Translated i18nKey="register" namespace="loginname" />
</button>
)}
</div>
{error && (
<div className="py-4" data-testid="error">
<Alert>{error}</Alert>
</div>
)}
<div className="mt-4 flex w-full flex-row items-center">
<BackButton data-testid="back-button" />
<span className="flex-grow"></span>
<Button
data-testid="submit-button"
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
disabled={loading || !formState.isValid}
onClick={handleSubmit((e) => submitLoginName(e, organization))}
>
{loading && <Spinner className="mr-2 h-5 w-5" />}
<Translated i18nKey="submit" namespace="loginname" />
</Button>
</div>
</form>
</>
);
}