File tree 2 files changed +15
-16
lines changed
2 files changed +15
-16
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 { getInputArrayFromProps , getValueFromProps , uuidv4 } from './utils' ;
11
11
12
12
const BACKSPACE_KEY = 8 ;
13
13
const LEFT_ARROW_KEY = 37 ;
@@ -32,23 +32,11 @@ class ReactCodeInput extends Component {
32
32
constructor ( props ) {
33
33
super ( props ) ;
34
34
35
- const { fields, forceUppercase } = props ;
36
- let value = props . value || '' ;
37
-
38
- if ( forceUppercase ) {
39
- value = value . toUpperCase ( ) ;
40
- }
41
-
42
35
this . state = {
43
- value ,
44
- input : [ ] ,
36
+ input : getInputArrayFromProps ( props ) ,
37
+ value : getValueFromProps ( props ) ,
45
38
} ;
46
39
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
40
this . textInput = [ ] ;
53
41
54
42
this . uuid = uuidv4 ( ) ;
Original file line number Diff line number Diff line change @@ -3,4 +3,15 @@ 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 getValueFromProps = ( { forceUppercase, value } ) => {
9
+ value = value == null ? '' : value ;
10
+ return forceUppercase ? value . toUpperCase ( ) : value ;
11
+ } ;
12
+
13
+ export const getInputArrayFromProps = ( props ) => {
14
+ const fields = Math . min ( 32 , props . fields ) ;
15
+ const value = getValueFromProps ( props ) ;
16
+ return Array . from ( Array ( fields ) ) . map ( ( _ , index ) => value [ index ] || '' ) ;
17
+ } ;
You can’t perform that action at this time.
0 commit comments