Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into main
  • Loading branch information
Ritik Banger committed Nov 30, 2022
2 parents bcb48b6 + e1ca609 commit 5efb0dd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# react18-input-otp

[![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)
[![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)

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

Expand Down
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
"component"
],
"peerDependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0"
"react": "16.2.0 - 18",
"react-dom": "16.2.0 - 18"
},
"typings": "typings/index.d.ts",
"devDependencies": {
Expand Down
20 changes: 16 additions & 4 deletions src/lib/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,26 @@ class OtpInput extends Component {
}
};

//If the OTP value at focused input exists, it gets changed; else, the previous input is focused.
focusPrevInputAndChangeCode = (value) => {
const { activeInput } = this.state;
const otp = this.getOtpValue();
if (otp[activeInput] != value[0]) {
otp[activeInput] = value[0];
if (otp[activeInput + 1] != value[0]) {
this.focusInput(otp.length - 1);
}
} else {
this.focusInput(activeInput - 1);
}
this.handleOtpChange(otp);
};

// Handle cases of backspace, delete, left arrow, right arrow, space
handleOnKeyDown = (e) => {
if (e.keyCode === BACKSPACE || e.key === 'Backspace') {
e.preventDefault();
this.changeCodeAtFocus('');
this.focusPrevInput();
this.focusPrevInputAndChangeCode('');
} else if (e.keyCode === DELETE || e.key === 'Delete') {
e.preventDefault();
this.changeCodeAtFocus('');
Expand All @@ -268,8 +282,6 @@ class OtpInput extends Component {
e.preventDefault();
} else if (e.keyCode === ENTER || e.key === 'Enter') {
this.props.onSubmit();
} else {
this.changeCodeAtFocus('');
}
};

Expand Down

0 comments on commit 5efb0dd

Please sign in to comment.