TextInputComponent.js
1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import React, {useState, useEffect} from 'react';
import {View, TextInput, Text, StyleSheet} from 'react-native';
const TextInputComponent = (props) => {
// 데이터
const {onChangeText, value, secureTextEntry, placeholder, rules, inputError, style1, style2} = props;
const [error, setError] = useState(null);
// 렌더링
useEffect(() => {
if (rules) {
let i = 0;
while (true) {
if (rules[i](value) !== true) {
setError(rules[i](value));
break;
} else {
if (i === rules.length - 1) {
setError(null);
break;
} else {
i++;
}
}
}
}
}, [value]);
return (
<>
<View style={{...styles.input, ...style1}}>
<TextInput style={{...styles.inputText}}
onChangeText={onChangeText}
value={value}
secureTextEntry={secureTextEntry}
placeholder={placeholder}/>
</View>
<Text style={{...styles.inputError, ...style2}}>{inputError ? inputError : error}</Text>
</>
)
};
const styles = StyleSheet.create({
input: {
borderRadius: 3,
backgroundColor: '#ebebeb',
},
inputText: {
fontSize: 16,
color: '#373737',
},
inputError: {
marginBottom: 5,
fontSize: 14,
color: '#9b0500',
}
});
export default TextInputComponent;