Skip to content

Commit 84c0e6a

Browse files
shashwat2003naveenvignesh5
authored andcommitted
Fixed backspace bug and more..
1 parent 31eab21 commit 84c0e6a

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

index.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,29 @@ class OTPTextView extends Component {
9090

9191
onKeyPress = (e, i) => {
9292
const val = this.state.otpText[i] || "";
93-
94-
if (e.nativeEvent.key === "Backspace" && i !== 0 && val.length === 1) {
95-
this.inputs[i - 1].focus();
93+
const { handleTextChange, inputCellLength } = this.props;
94+
const { otpText } = this.state;
95+
if (e.nativeEvent.key === "Backspace" && i !== 0) {
96+
if (val.length === 0) {
97+
if (otpText[i - 1].length === inputCellLength) {
98+
this.setState(
99+
(prevState) => {
100+
let { otpText } = prevState;
101+
otpText[i - 1] = otpText[i - 1]
102+
.split("")
103+
.splice(0, otpText[i - 1].length - 1)
104+
.join("");
105+
return {
106+
otpText,
107+
};
108+
},
109+
() => {
110+
handleTextChange(this.state.otpText.join(""));
111+
this.inputs[i - 1].focus();
112+
}
113+
);
114+
}
115+
}
96116
}
97117
};
98118

0 commit comments

Comments
 (0)