Skip to content

fix: enhance OTP input handling for iOS and improve focus behavior #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions src/components/single-otp-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ const handleOnKeyDown = (event: KeyboardEvent) => {
// Only allow characters 0-9, DEL, Backspace, Enter, Right and Left Arrows, and Pasting
const keyEvent = event || window.event;
const charCode = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
// Allow any input on iOS
if (/iPhone|iPad|iPod/.test(navigator.userAgent)) {
emit("on-keydown", event);
return;
}

if (
isCodeNumeric(charCode) ||
(props.inputType === "letter-numeric" && isCodeLetter(charCode)) ||
Expand Down
10 changes: 8 additions & 2 deletions src/components/vue3-otp-input.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ const handleOnFocus = (index: number) => {
activeInput.value = index;
};
const handleOnBlur = () => {
activeInput.value = -1;
// Don't reset activeInput if we're at the last input
if (activeInput.value !== props.numInputs - 1) {
activeInput.value = -1;
}
};

// Helper to return OTP from input
Expand Down Expand Up @@ -137,7 +140,10 @@ const handleOnPaste = (event: any) => {

const handleOnChange = (value: number | string) => {
changeCodeAtFocus(value);
focusNextInput();
// Only move to next input if we're not at the last input
if (activeInput.value < props.numInputs - 1) {
focusNextInput();
}
};
const clearInput = () => {
if (otp.value.length > 0) {
Expand Down