Advertisement
Guest User

App Input

a guest
Jan 22nd, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. import React from 'react'
  2. import {
  3. View,
  4. Text,
  5. Modal,
  6. FlatList,
  7. StyleSheet,
  8. SafeAreaView,
  9. Keyboard,
  10. KeyboardAvoidingView,
  11. TouchableWithoutFeedback,
  12. TouchableOpacity
  13. } from 'react-native'
  14. // native base imports
  15. import {
  16. Container,
  17. Item,
  18. Input,
  19. Icon
  20. } from 'native-base'
  21.  
  22. import data from './Countries'
  23.  
  24. // Default render of country flag
  25. const defaultFlag = data.filter(
  26. obj => obj.name === 'United Kingdom'
  27. )[0].flag
  28.  
  29. export default class App extends React.Component {
  30. state = {
  31. flag: defaultFlag,
  32. modalVisible: false,
  33. phoneNumber: '',
  34. }
  35. onChangeText(key, value) {
  36. this.setState({
  37. [key]: value
  38. })
  39. }
  40. showModal() {
  41. this.setState({ modalVisible: true })
  42. }
  43. hideModal() {
  44. this.setState({ modalVisible: false })
  45. // Refocus on the Input field after selecting the country code
  46. this.refs.PhoneInput._root.focus()
  47. }
  48.  
  49. async getCountry(country) {
  50. const countryData = await data
  51. try {
  52. const countryCode = await countryData.filter(
  53. obj => obj.name === country
  54. )[0].dial_code
  55. const countryFlag = await countryData.filter(
  56. obj => obj.name === country
  57. )[0].flag
  58. // Set data from user choice of country
  59. this.setState({ phoneNumber: countryCode, flag: countryFlag })
  60. await this.hideModal()
  61. }
  62. catch (err) {
  63. console.log(err)
  64. }
  65. }
  66.  
  67. render() {
  68. let { flag } = this.state
  69. const countryData = data
  70. return (
  71. <SafeAreaView style={styles.container}>
  72. <KeyboardAvoidingView style={styles.container} behavior='padding' enabled>
  73. <TouchableWithoutFeedback style={styles.container} onPress={Keyboard.dismiss}>
  74. <View style={styles.container}>
  75. <Container style={styles.infoContainer}>
  76. {/* Phone input with native-base */}
  77. {/* phone section */}
  78. <Item rounded style={styles.itemStyle}>
  79. <Icon
  80. active
  81. name='call'
  82. style={styles.iconStyle}
  83. />
  84. {/* country flag */}
  85. <View><Text style={{fontSize: 40}}>{flag}</Text></View>
  86. {/* open modal */}
  87. <Icon
  88. active
  89. name='md-arrow-dropdown'
  90. style={[styles.iconStyle, { marginLeft: 5 }]}
  91. onPress={() => this.showModal()}
  92. />
  93. <Input
  94. style={styles.input}
  95. placeholder='+44766554433'
  96. placeholderTextColor='#adb4bc'
  97. keyboardType={'phone-pad'}
  98. returnKeyType='done'
  99. autoCapitalize='none'
  100. autoCorrect={false}
  101. secureTextEntry={false}
  102. ref='PhoneInput'
  103. value={this.state.phoneNumber}
  104. onChangeText={(val) => {
  105. if (this.state.phoneNumber===''){
  106. // render UK phone code by default when Modal is not open
  107. this.onChangeText('phoneNumber', defaultCode + val)
  108. } else {
  109. // render country code based on users choice with Modal
  110. this.onChangeText('phoneNumber', val)
  111. }}
  112. }
  113. />
  114. {/* Modal for country code and flag */}
  115. <Modal
  116. animationType="slide" // fade
  117. transparent={false}
  118. visible={this.state.modalVisible}>
  119. <View style={{ flex: 1 }}>
  120. <View style={{ flex: 10, paddingTop: 80, backgroundColor: '#5059ae' }}>
  121. <FlatList
  122. data={countryData}
  123. keyExtractor={(item, index) => index.toString()}
  124. renderItem={
  125. ({ item }) =>
  126. <TouchableWithoutFeedback
  127. onPress={() => this.getCountry(item.name)}>
  128. <View
  129. style={
  130. [
  131. styles.countryStyle,
  132. {
  133. flexDirection: 'row',
  134. alignItems: 'center',
  135. justifyContent: 'space-between'
  136. }
  137. ]
  138. }>
  139. <Text style={{fontSize: 45}}>
  140. {item.flag}
  141. </Text>
  142. <Text style={{fontSize: 20, color: '#fff'}}>
  143. {item.name} ({item.dial_code})
  144. </Text>
  145. </View>
  146. </TouchableWithoutFeedback>
  147. }
  148. />
  149. </View>
  150. <TouchableOpacity
  151. onPress={() => this.hideModal()}
  152. style={styles.closeButtonStyle}>
  153. <Text style={styles.textStyle}>
  154. Close
  155. </Text>
  156. </TouchableOpacity>
  157. </View>
  158. </Modal>
  159. </Item>
  160. </Container>
  161. </View>
  162. </TouchableWithoutFeedback>
  163. </KeyboardAvoidingView>
  164. </SafeAreaView>
  165. )
  166. }
  167. }
  168.  
  169. const styles = StyleSheet.create({
  170. container: {
  171. flex: 1,
  172. backgroundColor: '#5059ae',
  173. justifyContent: 'center',
  174. flexDirection: 'column'
  175. },
  176. input: {
  177. flex: 1,
  178. fontSize: 17,
  179. fontWeight: 'bold',
  180. color: '#fff',
  181. },
  182. infoContainer: {
  183. flexDirection: 'row',
  184. justifyContent: 'center',
  185. alignItems: 'center',
  186. paddingHorizontal: 30,
  187. backgroundColor: '#5059ae',
  188. },
  189. itemStyle: {
  190. marginBottom: 10,
  191. },
  192. iconStyle: {
  193. color: '#fff',
  194. fontSize: 28,
  195. marginLeft: 15
  196. },
  197. buttonStyle: {
  198. alignItems: 'center',
  199. backgroundColor: '#b44666',
  200. padding: 14,
  201. marginBottom: 10,
  202. borderRadius: 3,
  203. },
  204. buttonText: {
  205. fontSize: 18,
  206. fontWeight: 'bold',
  207. color: "#fff",
  208. },
  209. textStyle: {
  210. padding: 5,
  211. fontSize: 20,
  212. color: '#fff',
  213. fontWeight: 'bold'
  214. },
  215. countryStyle: {
  216. flex: 1,
  217. backgroundColor: '#5059ae',
  218. borderTopColor: '#211f',
  219. borderTopWidth: 1,
  220. padding: 12,
  221. },
  222. closeButtonStyle: {
  223. flex: 1,
  224. padding: 12,
  225. alignItems: 'center',
  226. backgroundColor: '#b44666',
  227. }
  228. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement