Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/components/auth/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,25 @@ export function OtpInput({ onComplete, isLoading = false, error, onResend }: Otp
}, [digits]);

const handleChange = (index: number, e: ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
const value = e.target.value.replace(/\D/g, "");

// Only allow single digit
if (value.length > 1) return;
// iOS/Android one-time-code AutoFill inserts the whole code into one field
if (value.length >= OTP_LENGTH) {
setDigits(value.slice(-OTP_LENGTH).split(""));
inputRefs.current[OTP_LENGTH - 1]?.focus();
return;
}

// Only allow numbers
if (value && !/^\d$/.test(value)) return;
// Typing into a filled field: keep the newest digit
if (e.target.value && !value) return;
const digit = value.slice(-1);
Comment thread
pullfrog[bot] marked this conversation as resolved.

const newDigits = [...digits];
newDigits[index] = value;
newDigits[index] = digit;
setDigits(newDigits);

// Auto-advance to next input
if (value && index < OTP_LENGTH - 1) {
if (digit && index < OTP_LENGTH - 1) {
inputRefs.current[index + 1]?.focus();
}
};
Expand Down Expand Up @@ -151,10 +156,12 @@ export function OtpInput({ onComplete, isLoading = false, error, onResend }: Otp
}}
type="text"
inputMode="numeric"
maxLength={1}
autoComplete="one-time-code"
value={digit}
onChange={(e) => handleChange(index, e)}
onKeyDown={(e) => handleKeyDown(index, e)}
// Select existing digit so typing or AutoFill replaces it regardless of caret position
onFocus={(e) => e.target.select()}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are reports that select() doesn't take effect in iOS Safari, and iOS is the platform this PR targets (example). If it doesn't work, a tap on a filled box falls back to the old caret behavior. The usual workaround is setSelectionRange. Worth confirming during the on-device test in the test plan.

Suggested change
onFocus={(e) => e.target.select()}
onFocus={(e) => e.target.setSelectionRange(0, e.target.value.length)}

onPaste={handlePaste}
disabled={isLoading}
className="w-12 h-14 text-center text-2xl font-semibold text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-amber-500 focus:border-amber-500 disabled:opacity-50 disabled:cursor-not-allowed"
Expand Down
Loading