Skip to content
This repository was archived by the owner on Nov 3, 2024. It is now read-only.

fix: autocomplete issue for mobile devices. #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 28 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import React, {
useRef,
useEffect,
useImperativeHandle,
forwardRef
forwardRef,
ChangeEvent
} from 'react';

const allowedCharactersValues = ['alpha', 'numeric', 'alphanumeric'] as const;
Expand Down Expand Up @@ -183,6 +184,31 @@ const AuthCode = forwardRef<AuthCodeRef, AuthCodeProps>(
e.preventDefault();
};

const handleOnInput = (event: ChangeEvent<HTMLInputElement>) => {
if (!event.target.value) return;
const {
target: { value },
} = event;
let currentInput = 0;

if (event.target.value && event.target.value.length === length) {
for (let i = 0; i < value.length; i++) {
const pastedCharacter = value.charAt(i);
if (pastedCharacter.match(inputProps.pattern)) {
inputsRef.current[currentInput].value = pastedCharacter;
if (inputsRef.current[currentInput].nextElementSibling !== null) {
(
inputsRef.current[currentInput]
.nextElementSibling as HTMLInputElement
).focus();
currentInput++;
}
}
}
sendResult();
}
};

const inputs = [];
for (let i = 0; i < length; i++) {
inputs.push(
Expand All @@ -192,12 +218,12 @@ const AuthCode = forwardRef<AuthCodeRef, AuthCodeProps>(
onKeyDown={handleOnKeyDown}
onFocus={handleOnFocus}
onPaste={handleOnPaste}
onInput={handleOnInput}
{...inputProps}
type={isPassword ? 'password' : inputProps.type}
ref={(el: HTMLInputElement) => {
inputsRef.current[i] = el;
}}
maxLength={1}
className={inputClassName}
autoComplete={i === 0 ? 'one-time-code' : 'off'}
aria-label={
Expand Down