Skip to content

Commit 3fef597

Browse files
committed
♻️ Focus activeInput on paste
Additionally, fix #190
1 parent 91c62df commit 3fef597

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/lib/index.jsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,16 @@ class OtpInput extends Component<Props, State> {
224224
// Handle pasted OTP
225225
handleOnPaste = (e: Object) => {
226226
e.preventDefault();
227-
const { numInputs } = this.props;
228-
var { activeInput } = this.state;
227+
228+
const { activeInput } = this.state;
229+
const { numInputs, isDisabled } = this.props;
230+
231+
if (isDisabled) {
232+
return;
233+
}
234+
229235
const otp = this.getOtpValue();
236+
let nextActiveInput = activeInput;
230237

231238
// Get pastedData in an array of max size (num of inputs - current position)
232239
const pastedData = e.clipboardData
@@ -238,14 +245,14 @@ class OtpInput extends Component<Props, State> {
238245
for (let pos = 0; pos < numInputs; ++pos) {
239246
if (pos >= activeInput && pastedData.length > 0) {
240247
otp[pos] = pastedData.shift();
241-
activeInput++;
248+
nextActiveInput++;
242249
}
243250
}
244251

245-
this.setState({ activeInput });
246-
this.focusInput(activeInput);
247-
248-
this.handleOtpChange(otp);
252+
this.setState({ activeInput: nextActiveInput }, () => {
253+
this.focusInput(activeInput);
254+
this.handleOtpChange(otp);
255+
});
249256
};
250257

251258
handleOnChange = (e: Object) => {

0 commit comments

Comments
 (0)