Skip to content

Commit

Permalink
🚑 Fix paste not working for number inputs (#407)
Browse files Browse the repository at this point in the history
* 🚑 Remove unnecessary condition from onKeyDown

* 📝 Udpate documentation
  • Loading branch information
prateek3255 authored Jul 22, 2023
1 parent 4309214 commit b3b973b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ Do not override the following props on the input component that you return from
- `onBlur`
- `onKeyDown`
- `onPaste`
- `onInput`
- `type`
- `inputMode`

## Migrating from v2

Expand Down
1 change: 1 addition & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function App() {
<option value="text">text</option>
<option value="number">number</option>
<option value="password">password</option>
<option value="tel">tel</option>
</select>
</div>
<div className="side-bar__segment side-bar__segment--bottom">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-otp-input",
"version": "3.0.2",
"version": "3.0.3",
"description": "A fully customizable, one-time password input component for the web built with React",
"main": "lib/index.js",
"module": "lib/index.esm.js",
Expand Down
17 changes: 13 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type InputProps = Required<
| 'maxLength'
| 'autoComplete'
| 'style'
| 'inputMode'
| 'onInput'
> & {
ref: React.RefCallback<HTMLInputElement>;
placeholder: string | undefined;
Expand Down Expand Up @@ -101,8 +103,12 @@ const OTPInput = ({
if (isInputValueValid(value)) {
changeCodeAtFocus(value);
focusInput(activeInput + 1);
} else {
const { nativeEvent } = event;
}
};

const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { nativeEvent } = event;
if (!isInputValueValid(event.target.value)) {
// @ts-expect-error - This was added previosly to handle and edge case
// for dealing with keyCode "229 Unidentified" on Android. Check if this is
// still needed.
Expand All @@ -111,6 +117,9 @@ const OTPInput = ({
changeCodeAtFocus('');
focusInput(activeInput - 1);
}
// Clear the input if it's not valid value because firefox allows
// pasting non-numeric characters in a number type input
event.target.value = '';
}
};

Expand Down Expand Up @@ -151,8 +160,6 @@ const OTPInput = ({
event.code === 'ArrowDown'
) {
event.preventDefault();
} else if (isInputNum && !isInputValueValid(event.key)) {
event.preventDefault();
}
};

Expand Down Expand Up @@ -232,6 +239,8 @@ const OTPInput = ({
),
className: typeof inputStyle === 'string' ? inputStyle : undefined,
type: inputType,
inputMode: isInputNum ? 'numeric' : 'text',
onInput: handleInputChange,
},
index
)}
Expand Down

0 comments on commit b3b973b

Please # to comment.