feat: auto submit totp code when it is filled

This commit is contained in:
Stavros
2026-01-30 20:26:47 +02:00
parent 6431afb7d1
commit 252ba10f48
2 changed files with 10 additions and 4 deletions

View File

@@ -14,11 +14,10 @@ import z from "zod";
interface Props { interface Props {
formId: string; formId: string;
onSubmit: (code: TotpSchema) => void; onSubmit: (code: TotpSchema) => void;
loading?: boolean;
} }
export const TotpForm = (props: Props) => { export const TotpForm = (props: Props) => {
const { formId, onSubmit, loading } = props; const { formId, onSubmit } = props;
const { t } = useTranslation(); const { t } = useTranslation();
z.config({ z.config({
@@ -30,6 +29,14 @@ export const TotpForm = (props: Props) => {
resolver: zodResolver(totpSchema), resolver: zodResolver(totpSchema),
}); });
const handleChange = (value: string) => {
form.setValue("code", value, { shouldDirty: true, shouldValidate: true });
if (value.length === 6) {
onSubmit({ code: value });
}
};
return ( return (
<Form {...form}> <Form {...form}>
<form id={formId} onSubmit={form.handleSubmit(onSubmit)}> <form id={formId} onSubmit={form.handleSubmit(onSubmit)}>
@@ -41,10 +48,10 @@ export const TotpForm = (props: Props) => {
<FormControl> <FormControl>
<InputOTP <InputOTP
maxLength={6} maxLength={6}
disabled={loading}
{...field} {...field}
autoComplete="one-time-code" autoComplete="one-time-code"
autoFocus autoFocus
onChange={handleChange}
> >
<InputOTPGroup> <InputOTPGroup>
<InputOTPSlot index={0} /> <InputOTPSlot index={0} />

View File

@@ -70,7 +70,6 @@ export const TotpPage = () => {
<TotpForm <TotpForm
formId={formId} formId={formId}
onSubmit={(values) => totpMutation.mutate(values)} onSubmit={(values) => totpMutation.mutate(values)}
loading={totpMutation.isPending}
/> />
</CardContent> </CardContent>
<CardFooter className="flex flex-col items-stretch"> <CardFooter className="flex flex-col items-stretch">