-
-
Notifications
You must be signed in to change notification settings - Fork 225
/
Copy pathApp.tsx
120 lines (117 loc) · 3.39 KB
/
App.tsx
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, {useState, useRef} from 'react';
import {
SafeAreaView,
StyleSheet,
View,
StatusBar,
TouchableOpacity,
Text,
} from 'react-native';
import PhoneInput from 'react-native-phone-number-input';
import {Colors} from 'react-native/Libraries/NewAppScreen';
const App = () => {
const [value, setValue] = useState('');
const [countryCode, setCountryCode] = useState('');
const [formattedValue, setFormattedValue] = useState('');
const [valid, setValid] = useState(false);
const [disabled, setDisabled] = useState(false);
const [showMessage, setShowMessage] = useState(false);
const phoneInput = useRef<PhoneInput>(null);
return (
<>
<StatusBar barStyle="dark-content" />
<View style={styles.container}>
<SafeAreaView style={styles.wrapper}>
{showMessage && (
<View style={styles.message}>
<Text>Country Code : {countryCode}</Text>
<Text>Value : {value}</Text>
<Text>Formatted Value : {formattedValue}</Text>
<Text>Valid : {valid ? 'true' : 'false'}</Text>
</View>
)}
<PhoneInput
ref={phoneInput}
defaultValue={value}
defaultCode="IN"
layout="first"
onChangeText={(text) => {
setValue(text);
}}
onChangeFormattedText={(text) => {
setFormattedValue(text);
setCountryCode(phoneInput.current?.getCountryCode() || '');
}}
countryPickerProps={{withAlphaFilter:true}}
disabled={disabled}
withDarkTheme
withShadow
autoFocus
/>
<TouchableOpacity
style={styles.button}
onPress={() => {
const checkValid = phoneInput.current?.isValidNumber(value);
setShowMessage(true);
setValid(checkValid ? checkValid : false);
setCountryCode(phoneInput.current?.getCountryCode() || '');
let getNumberAfterPossiblyEliminatingZero = phoneInput.current?.getNumberAfterPossiblyEliminatingZero();
console.log(getNumberAfterPossiblyEliminatingZero);
}}>
<Text style={styles.buttonText}>Check</Text>
</TouchableOpacity>
<TouchableOpacity
style={[styles.button, disabled ? {} : styles.redColor]}
onPress={() => {
setDisabled(!disabled);
}}>
<Text style={styles.buttonText}>{disabled ? 'Activate' : 'Disable'}</Text>
</TouchableOpacity>
</SafeAreaView>
</View>
</>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: Colors.lighter,
},
wrapper: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
button: {
marginTop: 20,
height: 50,
width: 300,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#7CDB8A',
shadowColor: 'rgba(0,0,0,0.4)',
shadowOffset: {
width: 1,
height: 5,
},
shadowOpacity: 0.34,
shadowRadius: 6.27,
elevation: 10,
},
buttonText:{
color: 'white',
fontSize: 14,
},
redColor: {
backgroundColor: '#F57777'
},
message: {
borderWidth: 1,
borderRadius: 5,
padding: 20,
marginBottom: 20,
justifyContent: 'center',
alignItems: 'flex-start',
},
});
export default App;