1- import React , { Component } from 'react' ;
2- import PropTypes from 'prop-types' ;
3- import { View , TextInput , StyleSheet , Dimensions } from 'react-native' ;
4- import _ from 'lodash' ;
5-
6- const { width : wWidth } = Dimensions . get ( 'window' ) ;
7-
8- export default class ConfirmationCodeInput extends Component {
9- static propTypes = {
10- codeLength : PropTypes . number ,
11- codeInputStyle : TextInput . propTypes . style ,
12- codeInputWrapperStyle : View . propTypes . style ,
13- onFulfill : PropTypes . func
14- } ;
15-
16- static defaultProps = {
17- codeLength : 5
18- } ;
19-
20- constructor ( props ) {
21- super ( props ) ;
22-
23- this . state = {
24- codeArr : new Array ( this . props . codeLength ) . fill ( '' ) ,
25- currentIndex : 0
26- } ;
27-
28- this . codeInputRefs = [ ] ;
29- }
30-
31- clear ( ) {
32- this . setState ( {
33- codeArr : new Array ( this . props . codeLength ) . fill ( '' ) ,
34- currentIndex : 0
35- } )
36- this . _setFocus ( 0 ) ;
37- }
38-
39- _setFocus ( index ) {
40- this . codeInputRefs [ index ] . focus ( ) ;
41- }
42-
43- _blur ( index ) {
44- this . codeInputRefs [ index ] . blur ( ) ;
45- }
46-
47- _onFocus ( index ) {
48- let newCodeArr = _ . clone ( this . state . codeArr ) ;
49- for ( const i in newCodeArr ) {
50- if ( i >= index ) {
51- newCodeArr [ i ] = '' ;
52- }
53- }
54-
55- this . setState ( {
56- codeArr : newCodeArr ,
57- currentIndex : index
58- } )
59- }
60-
61- _onInputCode ( character , index ) {
62- const { codeLength, onFulfill} = this . props ;
63- let newCodeArr = _ . clone ( this . state . codeArr ) ;
64- newCodeArr [ index ] = character ;
65-
66- if ( index == codeLength - 1 ) {
67- this . _blur ( this . state . currentIndex ) ;
68- onFulfill ( newCodeArr . join ( '' ) ) ;
69- } else {
70- this . _setFocus ( this . state . currentIndex + 1 ) ;
71- }
72-
73- this . setState ( prevState => {
74- return {
75- codeArr : newCodeArr ,
76- currentIndex : prevState . currentIndex + 1
77- } ;
78- } ) ;
79-
80- }
81-
82- render ( ) {
83- const {
84- codeLength,
85- codeInputStyle,
86- codeInputWrapperStyle
87- } = this . props ;
88-
89- let codeInputs = [ ] ;
90- for ( let i = 0 ; i < codeLength ; i ++ ) {
91- const id = i ;
92- let borderStyle = this . state . currentIndex == id ? { } : { borderColor : 'rgba(255, 255, 255, 0.1)' } ;
93- codeInputs . push (
94- < TextInput
95- autoFocus = { id == 0 }
96- key = { id }
97- ref = { ref => ( this . codeInputRefs [ id ] = ref ) }
98- onChangeText = { text => this . _onInputCode ( text , id ) }
99- onFocus = { ( ) => this . _onFocus ( id ) }
100- value = { this . state . codeArr [ id ] ? this . state . codeArr [ id ] . toString ( ) : '' }
101- style = { [ styles . codeInput , codeInputStyle , borderStyle ] }
102- returnKeyType = { 'done' }
103- autoCapitalize = "characters"
104- autoCorrect = { true }
105- { ...this . props }
106- maxLength = { 1 }
107- />
108- )
109- }
110-
111- return (
112- < View style = { [ styles . codeInputWrapper , codeInputWrapperStyle ] } >
113- { codeInputs }
114- </ View >
115- ) ;
116- }
117- }
118-
119- const styles = StyleSheet . create ( {
120- codeInputWrapper : {
121- width : '35%' ,
122- height : 40 ,
123- flexDirection : 'row' ,
124- justifyContent : 'space-around' ,
125- marginTop : 20
126- } ,
127- codeInput : {
128- backgroundColor : 'transparent' ,
129- textAlign : 'center' ,
130- flex : 1 ,
131- marginRight : 4 ,
132- borderBottomWidth : 1 ,
133- borderColor : '#fff'
134- }
135- } ) ;
1+ import ConfirmationCodeInput from './components/ConfirmationCodeInput' ;
2+ export default ConfirmationCodeInput ;
0 commit comments