@@ -34,9 +34,9 @@ const ReactInputVerificationCode = ({
34
34
35
35
const isCodeRegex = new RegExp ( `^[0-9]{${ length } }$` ) ;
36
36
37
- const getItem = ( index : number ) => itemsRef [ index ] . current ! ;
38
- const focusItem = ( index : number ) : void => getItem ( index ) . focus ( ) ;
39
- const blurItem = ( index : number ) : void => getItem ( index ) . blur ( ) ;
37
+ const getItem = ( index : number ) => itemsRef [ index ] ? .current ;
38
+ const focusItem = ( index : number ) : void => getItem ( index ) ? .focus ( ) ;
39
+ const blurItem = ( index : number ) : void => getItem ( index ) ? .blur ( ) ;
40
40
41
41
const onItemFocus = ( index : number ) => ( ) => {
42
42
setActiveIndex ( index ) ;
@@ -48,6 +48,9 @@ const ReactInputVerificationCode = ({
48
48
const nextIndex = activeIndex + 1 ;
49
49
const prevIndex = activeIndex - 1 ;
50
50
51
+ const codeInput = codeInputRef . current ;
52
+ const currentItem = getItem ( activeIndex ) ;
53
+
51
54
const isLast = nextIndex === length ;
52
55
const isDeleting =
53
56
keyCode === KEY_CODE . DELETE || keyCode === KEY_CODE . BACKSPACE ;
@@ -75,7 +78,7 @@ const ReactInputVerificationCode = ({
75
78
76
79
// reset the current value
77
80
// and set the new one
78
- if ( codeInputRef . current ) codeInputRef . current . value = '' ;
81
+ if ( codeInput ) codeInput . value = '' ;
79
82
newValue [ activeIndex ] = key ;
80
83
setValue ( newValue ) ;
81
84
@@ -85,8 +88,9 @@ const ReactInputVerificationCode = ({
85
88
return ;
86
89
}
87
90
88
- if ( codeInputRef . current ) codeInputRef . current . blur ( ) ;
89
- getItem ( activeIndex ) . blur ( ) ;
91
+ if ( codeInput ) codeInput . blur ( ) ;
92
+ if ( currentItem ) currentItem . blur ( ) ;
93
+
90
94
setActiveIndex ( - 1 ) ;
91
95
} ;
92
96
0 commit comments