Skip to content

Commit 5efb0dd

Browse files
author
Ritik Banger
committed
Merge branch 'main' of https://github.com/ritikbanger/react18-input-otp into main
2 parents bcb48b6 + e1ca609 commit 5efb0dd

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react18-input-otp
22

3-
[![npm version](https://badge.fury.io/js/react18-input-otp.svg)](https://badge.fury.io/js/react18-input-otp) [![npm](https://img.shields.io/npm/dw/react18-input-otp.svg?logo=npm)](https://www.npmjs.com/package/react18-input-otp) [![npm](https://img.shields.io/bundlephobia/minzip/react18-input-otp)](https://www.npmjs.com/package/react18-input-otp)
3+
[![npm version](https://badge.fury.io/js/react18-input-otp.svg)](https://badge.fury.io/js/react18-input-otp) [![npm](https://img.shields.io/npm/dm/react18-input-otp.svg?logo=npm)](https://www.npmjs.com/package/react18-input-otp) [![npm](https://img.shields.io/bundlephobia/minzip/react18-input-otp)](https://www.npmjs.com/package/react18-input-otp)
44

55
<!-- ALL-CONTRIBUTORS-BADGE:END -->
66

lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"component"
2525
],
2626
"peerDependencies": {
27-
"react": "^18.1.0",
28-
"react-dom": "^18.1.0"
27+
"react": "16.2.0 - 18",
28+
"react-dom": "16.2.0 - 18"
2929
},
3030
"typings": "typings/index.d.ts",
3131
"devDependencies": {

src/lib/index.jsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,26 @@ class OtpInput extends Component {
249249
}
250250
};
251251

252+
//If the OTP value at focused input exists, it gets changed; else, the previous input is focused.
253+
focusPrevInputAndChangeCode = (value) => {
254+
const { activeInput } = this.state;
255+
const otp = this.getOtpValue();
256+
if (otp[activeInput] != value[0]) {
257+
otp[activeInput] = value[0];
258+
if (otp[activeInput + 1] != value[0]) {
259+
this.focusInput(otp.length - 1);
260+
}
261+
} else {
262+
this.focusInput(activeInput - 1);
263+
}
264+
this.handleOtpChange(otp);
265+
};
266+
252267
// Handle cases of backspace, delete, left arrow, right arrow, space
253268
handleOnKeyDown = (e) => {
254269
if (e.keyCode === BACKSPACE || e.key === 'Backspace') {
255270
e.preventDefault();
256-
this.changeCodeAtFocus('');
257-
this.focusPrevInput();
271+
this.focusPrevInputAndChangeCode('');
258272
} else if (e.keyCode === DELETE || e.key === 'Delete') {
259273
e.preventDefault();
260274
this.changeCodeAtFocus('');
@@ -268,8 +282,6 @@ class OtpInput extends Component {
268282
e.preventDefault();
269283
} else if (e.keyCode === ENTER || e.key === 'Enter') {
270284
this.props.onSubmit();
271-
} else {
272-
this.changeCodeAtFocus('');
273285
}
274286
};
275287

0 commit comments

Comments
 (0)