File tree Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Expand file tree Collapse file tree 1 file changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -90,9 +90,29 @@ class OTPTextView extends Component {
90
90
91
91
onKeyPress = ( e , i ) => {
92
92
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
+ }
96
116
}
97
117
} ;
98
118
You can’t perform that action at this time.
0 commit comments