Skip to content

Commit 253279a

Browse files
committed
fix(input-otp): handle autofill with all invalid chars
1 parent 4b7cb03 commit 253279a

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

core/src/components/input-otp/input-otp.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,32 @@ export class InputOTP implements ComponentInterface {
620620
.split('')
621621
.filter((char) => validKeyPattern.test(char))
622622
.slice(0, length);
623+
624+
// If there are no valid characters coming from the
625+
// autofill, all input refs have to be cleared after the
626+
// browser has finished the autofill behavior
627+
if (validChars.length === 0) {
628+
requestAnimationFrame(() => {
629+
this.inputRefs.forEach((input) => {
630+
input.value = '';
631+
});
632+
});
633+
}
634+
623635
for (let i = 0; i < length; i++) {
624636
this.inputValues[i] = validChars[i] || '';
625637
this.inputRefs[i].value = validChars[i] || '';
626638
}
627639
this.updateValue(event);
628-
// Focus the next empty input or the last one
640+
641+
// Focus the first empty input box or the last input box if all boxes
642+
// are filled after a small delay to ensure the input boxes have been
643+
// updated before moving the focus
629644
setTimeout(() => {
630645
const nextIndex = validChars.length < length ? validChars.length : length - 1;
631-
this.inputRefs[nextIndex].focus();
646+
this.inputRefs[nextIndex]?.focus();
632647
}, 20);
648+
633649
this.previousInputValues = [...this.inputValues];
634650
return;
635651
}

0 commit comments

Comments
 (0)