taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 4bf1b24fba1731d01ba25153beef6d53e0bbd103
parent c58cf2ae54417fe5d5cab9075ff3d88e52f3796b
Author: Florian Dold <dold@taler.net>
Date:   Fri, 13 Nov 2026 01:06:16 +0100

merchant web UI: focus authentication fields and code steps

Focus the field each authentication step asks for. Use the challenge ID
to focus a new code prompt without remounting it or reacting to
countdowns.

Issue: https://bugs.taler.net/n/12003
Issue: https://bugs.taler.net/n/12023
Assisted-by: LLM

Diffstat:
Mpackages/taler-merchant-webui/src/screens/ForgotPasswordScreen.tsx | 12++++++++++--
Mpackages/taler-merchant-webui/src/screens/SelfProvisionScreen.tsx | 29++++++++++++++++++++++++++++-
Mpackages/taler-merchant-webui/src/screens/SignInScreen.tsx | 20+++++++++++++++++++-
Mpackages/taler-merchant-webui/src/screens/UnauthorizedScreen.tsx | 6++++++
Mpackages/taler-merchant-webui/src/ui/TanCodeInputGroup.tsx | 7+++++++
Apackages/taler-merchant-webui/src/utils/focus.ts | 25+++++++++++++++++++++++++
6 files changed, 95 insertions(+), 4 deletions(-)

diff --git a/packages/taler-merchant-webui/src/screens/ForgotPasswordScreen.tsx b/packages/taler-merchant-webui/src/screens/ForgotPasswordScreen.tsx @@ -14,9 +14,10 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { useFocusScope, firstEditableField } from "@gnu-taler/web-util/browser"; import { ActionFooter } from "../ui/FormActionFooter.js"; import type { VNode } from "preact"; -import { useState } from "preact/hooks"; +import { useRef, useState } from "preact/hooks"; import { TalerMerchantApi } from "@gnu-taler/taler-util"; import { TalerLogo } from "../ui/TalerLogo.js"; import { CopyErrorButton } from "../ui/CopyErrorButton.js"; @@ -26,6 +27,7 @@ import { } from "../stores/session.js"; import { useTranslation } from "../context/translation.js"; import { formatErrorMessage } from "../utils/errors.js"; + import { RawLayout } from "../ui/RawLayout.js"; import { PasswordInput } from "../ui/PasswordInput.js"; @@ -64,6 +66,12 @@ export function ForgotPasswordScreen({ const [errorMsg, setErrorMsg] = useState<string>(""); const [rawError, setRawError] = useState<unknown>(null); const [isSubmitting, setIsSubmitting] = useState<boolean>(false); + // Start on the account, or on the new password when the account is known. + const form = useRef<HTMLFormElement>(null); + useFocusScope({ + root: () => form.current, + initialFocus: () => firstEditableField(form.current, true), + }); const handleSubmit = async (e: Event) => { e.preventDefault(); @@ -135,7 +143,7 @@ export function ForgotPasswordScreen({ } > <div class="bg-white py-6 px-4 shadow-sm border border-gray-200 sm:rounded-lg sm:px-8 space-y-5"> - <form onSubmit={handleSubmit} class="space-y-4"> + <form ref={form} onSubmit={handleSubmit} class="space-y-4"> <div> <label htmlFor="reset-account" diff --git a/packages/taler-merchant-webui/src/screens/SelfProvisionScreen.tsx b/packages/taler-merchant-webui/src/screens/SelfProvisionScreen.tsx @@ -14,6 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { useFocusScope, firstEditableField } from "@gnu-taler/web-util/browser"; import { ActionFooter } from "../ui/FormActionFooter.js"; import type { VNode } from "preact"; import { useState, useEffect, useRef } from "preact/hooks"; @@ -32,6 +33,7 @@ import { type LoginTokenInfo } from "../stores/session.js"; import { presetMerchantBaseUrl } from "../stores/webuiConfig.js"; import { sanitizeTanCode, formatTanDigits } from "../utils/tan.js"; import { TanCodeInputGroup } from "../ui/TanCodeInputGroup.js"; +import { checkedRadio } from "../utils/focus.js"; import { PasswordInput } from "../ui/PasswordInput.js"; import type { ApiFailure } from "../api/failure.js"; import { normalizeUiFailure } from "../utils/errors.js"; @@ -277,6 +279,27 @@ export function SelfProvisionScreen({ (challenge) => challenge.challenge_id === challengeRound.activeChallengeId, ); + // Each step swaps the form's fields, so put the cursor on the one it asks + // for. Coming back to edit the address the code went to lands on that + // address; otherwise on the first field still empty. The code steps' inputs + // focus themselves. + const form = useRef<HTMLFormElement>(null); + const returnFocusId = useRef<string | undefined>(undefined); + useFocusScope({ + root: () => form.current, + activationKey: step, + initialFocus: () => + step === "form" + ? ((returnFocusId.current + ? form.current?.querySelector<HTMLElement>( + `[id="${returnFocusId.current}"]`, + ) + : null) ?? firstEditableField(form.current, true)) + : step === "select_channel" + ? checkedRadio(form.current) + : form.current?.querySelector<HTMLElement>('input[id$="-code"]'), + }); + const sendChallenge = async (challengeId: string): Promise<void> => { try { setResendCooldown( @@ -701,7 +724,7 @@ export function SelfProvisionScreen({ </div> )} - <form onSubmit={handleSubmit} class="space-y-4"> + <form ref={form} onSubmit={handleSubmit} class="space-y-4"> {step === "form" && ( <> <div> @@ -952,12 +975,14 @@ export function SelfProvisionScreen({ </div> <TanCodeInputGroup + activationKey={activeChallenge?.challenge_id} id="signup-email-code" value={emailCode} onChange={setEmailCode} resendCooldown={resendCooldown} onResend={handleResendTanCodes} onBack={() => { + returnFocusId.current = "signup-email"; setStep("form"); setErrorMsg(""); }} @@ -992,12 +1017,14 @@ export function SelfProvisionScreen({ </div> <TanCodeInputGroup + activationKey={activeChallenge?.challenge_id} id="signup-sms-code" value={smsCode} onChange={setSmsCode} resendCooldown={resendCooldown} onResend={handleResendTanCodes} onBack={() => { + returnFocusId.current = "signup-phone"; setStep("form"); setErrorMsg(""); }} diff --git a/packages/taler-merchant-webui/src/screens/SignInScreen.tsx b/packages/taler-merchant-webui/src/screens/SignInScreen.tsx @@ -14,6 +14,7 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { useFocusScope, firstEditableField } from "@gnu-taler/web-util/browser"; import { ActionFooter } from "../ui/FormActionFooter.js"; import type { VNode } from "preact"; import { useState, useEffect, useRef } from "preact/hooks"; @@ -28,6 +29,7 @@ import { CopyErrorButton } from "../ui/CopyErrorButton.js"; import { FooterControls } from "../ui/FooterControls.js"; import { useTranslation } from "../context/translation.js"; import { formatErrorMessage } from "../utils/errors.js"; +import { checkedRadio } from "../utils/focus.js"; import { currentAccount, lastMerchantAccountStore, @@ -224,6 +226,21 @@ export function SignInScreen({ const maxAttemptsReached = attemptsCount >= 5; + // Each step swaps the form's fields, so put the cursor on the one it asks + // for: the account (or, when it is remembered, the password), or the chosen + // verification method. The code step's input focuses itself. + const form = useRef<HTMLFormElement>(null); + useFocusScope({ + root: () => form.current, + activationKey: step, + initialFocus: () => + step === "credentials" + ? firstEditableField(form.current, true) + : step === "channel_select" + ? checkedRadio(form.current) + : form.current?.querySelector<HTMLElement>('input[id$="-code"]'), + }); + /** * Send the code when the screen opens already asking for one. * @@ -547,7 +564,7 @@ export function SignInScreen({ </div> )} - <form onSubmit={handleSubmit} class="space-y-5"> + <form ref={form} onSubmit={handleSubmit} class="space-y-5"> {step === "credentials" && ( <> <div> @@ -671,6 +688,7 @@ export function SignInScreen({ </div> <TanCodeInputGroup + activationKey={activeChallengeId} id="signin-2fa-code" label={t`Authentication code`} value={mfaCode} diff --git a/packages/taler-merchant-webui/src/screens/UnauthorizedScreen.tsx b/packages/taler-merchant-webui/src/screens/UnauthorizedScreen.tsx @@ -14,7 +14,9 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { useFocusScope } from "@gnu-taler/web-util/browser"; import type { VNode } from "preact"; +import { useRef } from "preact/hooks"; import { TalerLogo } from "../ui/TalerLogo.js"; import { useTranslation } from "../context/translation.js"; import { RawLayout } from "../ui/RawLayout.js"; @@ -44,6 +46,9 @@ export function UnauthorizedScreen({ "Your session token was rejected by the server (HTTP 401 Unauthorized)." ? t`Your session token was rejected by the server (HTTP 401 Unauthorized).` : errorDetail; + // Signing in again is the only thing to do here. + const signInAgain = useRef<HTMLButtonElement>(null); + useFocusScope({ initialFocus: () => signInAgain.current }); return ( <RawLayout> @@ -87,6 +92,7 @@ export function UnauthorizedScreen({ <div class="pt-2"> <button + ref={signInAgain} type="button" onClick={() => { if (onSignInAgain) { diff --git a/packages/taler-merchant-webui/src/ui/TanCodeInputGroup.tsx b/packages/taler-merchant-webui/src/ui/TanCodeInputGroup.tsx @@ -14,12 +14,15 @@ GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> */ +import { useFocusScope } from "@gnu-taler/web-util/browser"; import type { VNode } from "preact"; +import { useRef } from "preact/hooks"; import { useTranslation } from "../context/translation.js"; import { formatTanDigits } from "../utils/tan.js"; export interface TanCodeInputGroupProps { id: string; + activationKey?: string; label?: string; value: string; onChange: (val: string) => void; @@ -32,6 +35,7 @@ export interface TanCodeInputGroupProps { export function TanCodeInputGroup({ id, + activationKey, label, value, onChange, @@ -42,6 +46,8 @@ export function TanCodeInputGroup({ backLabel, }: TanCodeInputGroupProps): VNode { const { t } = useTranslation(); + const input = useRef<HTMLInputElement>(null); + useFocusScope({ activationKey, initialFocus: () => input.current }); return ( <div> @@ -52,6 +58,7 @@ export function TanCodeInputGroup({ {label || t`Verification code`} </label> <input + ref={input} id={id} type="text" value={value} diff --git a/packages/taler-merchant-webui/src/utils/focus.ts b/packages/taler-merchant-webui/src/utils/focus.ts @@ -0,0 +1,25 @@ +/* + This file is part of GNU Taler + (C) 2026 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +export function checkedRadio( + root: ParentNode | null | undefined, +): HTMLElement | null | undefined { + return ( + root?.querySelector<HTMLElement>( + 'input[type="radio"]:checked:not(:disabled)', + ) ?? root?.querySelector<HTMLElement>('input[type="radio"]:not(:disabled)') + ); +}