1
1
import React , {
2
2
ChangeEvent ,
3
- ClipboardEvent ,
4
3
createRef ,
5
4
InputHTMLAttributes ,
6
5
KeyboardEvent ,
@@ -139,6 +138,8 @@ const ReactInputVerificationCode = ({
139
138
140
139
const onInputChange = useCallback (
141
140
( event : ChangeEvent < HTMLInputElement > , index : number ) => {
141
+ event . preventDefault ( ) ;
142
+
142
143
const eventValue = event . target . value ;
143
144
console . log ( '-------' ) ;
144
145
console . log ( 'RIVC:onInputChange' , {
@@ -149,9 +150,9 @@ const ReactInputVerificationCode = ({
149
150
} ) ;
150
151
151
152
/**
152
- * otp code
153
+ * otp code or pasted value
153
154
*/
154
- if ( eventValue . length > 2 ) {
155
+ if ( eventValue . length > 1 ) {
155
156
console . log ( 'RIVC: isOtp' , true ) ;
156
157
console . log ( 'RIVC: fillValues(eventValue)' , fillValues ( eventValue ) ) ;
157
158
@@ -166,6 +167,7 @@ const ReactInputVerificationCode = ({
166
167
return ;
167
168
}
168
169
170
+ focusInput ( eventValue . length ) ;
169
171
return ;
170
172
}
171
173
@@ -243,32 +245,6 @@ const ReactInputVerificationCode = ({
243
245
[ focusInput , focusedIndex , setValue , values ]
244
246
) ;
245
247
246
- const onInputPaste = useCallback (
247
- ( event : ClipboardEvent < HTMLInputElement > , index : number ) => {
248
- event . preventDefault ( ) ;
249
-
250
- const pastedValue = event . clipboardData . getData ( 'text' ) ;
251
- const nextValues = pastedValue . slice ( 0 , length ) ;
252
-
253
- if ( ! validate ( nextValues ) ) {
254
- return ;
255
- }
256
-
257
- setValues ( fillValues ( nextValues ) ) ;
258
-
259
- const isCompleted = nextValues . length === length ;
260
-
261
- if ( isCompleted ) {
262
- onCompleted ( nextValues ) ;
263
- blurInput ( index ) ;
264
- return ;
265
- }
266
-
267
- focusInput ( nextValues . length ) ;
268
- } ,
269
- [ blurInput , fillValues , focusInput , length , onCompleted , validate ]
270
- ) ;
271
-
272
248
/**
273
249
* autoFocus
274
250
*/
@@ -282,14 +258,13 @@ const ReactInputVerificationCode = ({
282
258
< div className = 'ReactInputVerificationCode-container' >
283
259
{ inputsRefs . map ( ( ref , i ) => (
284
260
< input
285
- autoComplete = { focusedIndex === i ? 'one-time-code' : 'off' }
261
+ autoComplete = 'one-time-code'
286
262
className = 'ReactInputVerificationCode-item'
287
263
inputMode = { type === 'number' ? 'numeric' : 'text' }
288
264
key = { i }
289
265
onChange = { ( event ) => onInputChange ( event , i ) }
290
266
onFocus = { ( ) => onInputFocus ( i ) }
291
267
onKeyDown = { ( event ) => onInputKeyDown ( event , i ) }
292
- onPaste = { ( event ) => onInputPaste ( event , i ) }
293
268
placeholder = { placeholder }
294
269
ref = { ref }
295
270
value = { values [ i ] }
0 commit comments