|
1 | | -import React, {useRef, useState} from 'react'; |
2 | | -import {View, TextInput, StyleSheet, KeyboardTypeOptions} from 'react-native'; |
| 1 | +import React from 'react'; |
| 2 | +import {View, TextInput, StyleSheet} from 'react-native'; |
3 | 3 |
|
4 | 4 | interface props { |
5 | 5 | label?: string; |
6 | 6 | placeholder?: string; |
7 | | - keyboardType?: KeyboardTypeOptions; |
8 | | - maxLength?: number; |
9 | | - onChangeText?: (text: string) => void; |
10 | | - value?: any; |
| 7 | + onChangeText: (codes: any[]) => void; |
| 8 | + value: any; |
11 | 9 | inputStyle?: any; |
12 | 10 | labelStyle?: any; |
13 | 11 | onBlur?: () => void; |
14 | 12 | } |
15 | 13 |
|
16 | | -function CodeInput({onChangeText, inputStyle}: props) { |
17 | | - const [code, setCode] = useState(['', '', '', '', '', '']); |
| 14 | +function CodeInput({value, onChangeText, inputStyle}: props) { |
| 15 | + const handleCodeChange = (text, index) => { |
| 16 | + if (/^\d*$/.test(text) && text.length <= 1) { |
| 17 | + const newCode = [...value]; |
| 18 | + newCode[index] = text; |
| 19 | + onChangeText(newCode); |
18 | 20 |
|
19 | | - const handleCodeChange = (text, index) => { |
20 | | - if (/^\d*$/.test(text) && text.length <= 1) { |
21 | | - const newCode = [...code]; |
22 | | - newCode[index] = text; |
23 | | - setCode(newCode); |
24 | | - |
25 | | - if (text !== '') { |
26 | | - focusNextInput(index + 1); |
27 | | - } else{ |
28 | | - focusPreviousInput(index - 1); |
29 | | - } |
| 21 | + if (text !== '') { |
| 22 | + focusNextInput(index + 1); |
| 23 | + } else { |
| 24 | + focusPreviousInput(index - 1); |
30 | 25 | } |
31 | | - }; |
| 26 | + } |
| 27 | + }; |
32 | 28 |
|
33 | | - const focusNextInput = nextIndex => { |
34 | | - if (nextIndex < 6) { |
35 | | - this[`inputRef${nextIndex}`].focus(); |
36 | | - } |
37 | | - }; |
| 29 | + const focusNextInput = nextIndex => { |
| 30 | + if (nextIndex < 6) { |
| 31 | + this[`inputRef${nextIndex}`].focus(); |
| 32 | + } |
| 33 | + }; |
| 34 | + |
| 35 | + const focusPreviousInput = prevIndex => { |
| 36 | + if (prevIndex >= 0) { |
| 37 | + this[`inputRef${prevIndex}`].focus(); |
| 38 | + } |
| 39 | + }; |
38 | 40 |
|
39 | | - const focusPreviousInput = prevIndex => { |
40 | | - if (prevIndex >= 0) { |
41 | | - this[`inputRef${prevIndex}`].focus(); |
42 | | - } |
43 | | - }; |
44 | | - |
45 | 41 | return ( |
46 | 42 | <View style={styles.container}> |
47 | | - {code.map((value, index) => ( |
| 43 | + {value.map((value, index) => ( |
48 | 44 | <TextInput |
49 | 45 | key={index} |
50 | 46 | style={styles.input} |
|
0 commit comments