File tree 2 files changed +10
-10
lines changed
2 files changed +10
-10
lines changed Original file line number Diff line number Diff line change 7
7
import React , { Component } from 'react' ;
8
8
import PropTypes from 'prop-types' ;
9
9
import classNames from 'classnames' ;
10
- import { uuidv4 } from './utils' ;
10
+ import { getInputArray , uuidv4 } from './utils' ;
11
11
12
12
const BACKSPACE_KEY = 8 ;
13
13
const LEFT_ARROW_KEY = 37 ;
@@ -32,23 +32,17 @@ class ReactCodeInput extends Component {
32
32
constructor ( props ) {
33
33
super ( props ) ;
34
34
35
- const { fields, forceUppercase } = props ;
36
35
let value = props . value || '' ;
37
36
38
- if ( forceUppercase ) {
37
+ if ( props . forceUppercase ) {
39
38
value = value . toUpperCase ( ) ;
40
39
}
41
40
42
41
this . state = {
43
42
value,
44
- input : [ ] ,
43
+ input : getInputArray ( props ) ,
45
44
} ;
46
45
47
- for ( let i = 0 ; i < Number ( fields ) && i < 32 ; i += 1 ) {
48
- const value = this . state . value [ i ] || '' ;
49
- this . state . input . push ( value ) ;
50
- }
51
-
52
46
this . textInput = [ ] ;
53
47
54
48
this . uuid = uuidv4 ( ) ;
Original file line number Diff line number Diff line change @@ -3,4 +3,10 @@ export const uuidv4 = () => {
3
3
let r = Math . random ( ) * 16 | 0 , v = c === 'x' ? r : ( r & 0x3 | 0x8 ) ;
4
4
return v . toString ( 16 ) ;
5
5
} ) ;
6
- } ;
6
+ } ;
7
+
8
+ export const getInputArray = ( { fields, value } ) => {
9
+ fields = Math . min ( 32 , fields ) ;
10
+ value = value || '' ;
11
+ return Array . from ( Array ( fields ) ) . map ( ( _ , index ) => value [ index ] || '' ) ;
12
+ } ;
You can’t perform that action at this time.
0 commit comments