Skip to content

Commit b3b973b

Browse files
authored
🚑 Fix paste not working for number inputs (#407)
* 🚑 Remove unnecessary condition from onKeyDown * 📝 Udpate documentation
1 parent 4309214 commit b3b973b

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ Do not override the following props on the input component that you return from
140140
- `onBlur`
141141
- `onKeyDown`
142142
- `onPaste`
143+
- `onInput`
143144
- `type`
145+
- `inputMode`
144146

145147
## Migrating from v2
146148

example/src/App.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function App() {
9595
<option value="text">text</option>
9696
<option value="number">number</option>
9797
<option value="password">password</option>
98+
<option value="tel">tel</option>
9899
</select>
99100
</div>
100101
<div className="side-bar__segment side-bar__segment--bottom">

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-otp-input",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "A fully customizable, one-time password input component for the web built with React",
55
"main": "lib/index.js",
66
"module": "lib/index.esm.js",

src/index.tsx

+13-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ type InputProps = Required<
1515
| 'maxLength'
1616
| 'autoComplete'
1717
| 'style'
18+
| 'inputMode'
19+
| 'onInput'
1820
> & {
1921
ref: React.RefCallback<HTMLInputElement>;
2022
placeholder: string | undefined;
@@ -101,8 +103,12 @@ const OTPInput = ({
101103
if (isInputValueValid(value)) {
102104
changeCodeAtFocus(value);
103105
focusInput(activeInput + 1);
104-
} else {
105-
const { nativeEvent } = event;
106+
}
107+
};
108+
109+
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
110+
const { nativeEvent } = event;
111+
if (!isInputValueValid(event.target.value)) {
106112
// @ts-expect-error - This was added previosly to handle and edge case
107113
// for dealing with keyCode "229 Unidentified" on Android. Check if this is
108114
// still needed.
@@ -111,6 +117,9 @@ const OTPInput = ({
111117
changeCodeAtFocus('');
112118
focusInput(activeInput - 1);
113119
}
120+
// Clear the input if it's not valid value because firefox allows
121+
// pasting non-numeric characters in a number type input
122+
event.target.value = '';
114123
}
115124
};
116125

@@ -151,8 +160,6 @@ const OTPInput = ({
151160
event.code === 'ArrowDown'
152161
) {
153162
event.preventDefault();
154-
} else if (isInputNum && !isInputValueValid(event.key)) {
155-
event.preventDefault();
156163
}
157164
};
158165

@@ -232,6 +239,8 @@ const OTPInput = ({
232239
),
233240
className: typeof inputStyle === 'string' ? inputStyle : undefined,
234241
type: inputType,
242+
inputMode: isInputNum ? 'numeric' : 'text',
243+
onInput: handleInputChange,
235244
},
236245
index
237246
)}

0 commit comments

Comments
 (0)