Skip to content

Commit a1aed4b

Browse files
authored
fix: mobile autofill from native suggestion (#428)
1 parent 325e175 commit a1aed4b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/index.tsx

+16-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ type InputProps = Required<
1212
| 'onKeyDown'
1313
| 'onPaste'
1414
| 'aria-label'
15-
| 'maxLength'
1615
| 'autoComplete'
1716
| 'style'
1817
| 'inputMode'
@@ -114,15 +113,29 @@ const OTPInput = ({
114113

115114
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
116115
const { nativeEvent } = event;
117-
if (!isInputValueValid(event.target.value)) {
118-
// @ts-expect-error - This was added previosly to handle and edge case
116+
const value = event.target.value;
117+
118+
if (!isInputValueValid(value)) {
119+
// Pasting from the native autofill suggestion on a mobile device can pass
120+
// the pasted string as one long input to one of the cells. This ensures
121+
// that we handle the full input and not just the first character.
122+
if (value.length === numInputs) {
123+
const hasInvalidInput = value.split('').some((cellInput) => !isInputValueValid(cellInput));
124+
if (!hasInvalidInput) {
125+
handleOTPChange(value.split(''));
126+
focusInput(numInputs - 1);
127+
}
128+
}
129+
130+
// @ts-expect-error - This was added previously to handle and edge case
119131
// for dealing with keyCode "229 Unidentified" on Android. Check if this is
120132
// still needed.
121133
if (nativeEvent.data === null && nativeEvent.inputType === 'deleteContentBackward') {
122134
event.preventDefault();
123135
changeCodeAtFocus('');
124136
focusInput(activeInput - 1);
125137
}
138+
126139
// Clear the input if it's not valid value because firefox allows
127140
// pasting non-numeric characters in a number type input
128141
event.target.value = '';
@@ -238,7 +251,6 @@ const OTPInput = ({
238251
onKeyDown: handleKeyDown,
239252
onPaste: handlePaste,
240253
autoComplete: 'off',
241-
maxLength: 1,
242254
'aria-label': `Please enter OTP character ${index + 1}`,
243255
style: Object.assign(
244256
!skipDefaultStyles ? ({ width: '1em', textAlign: 'center' } as const) : {},

0 commit comments

Comments
 (0)