|
1 | 1 | import React, { useState } from 'react' |
2 | | -import { |
3 | | - Text, |
4 | | - StyleSheet, |
5 | | - PixelRatio, |
6 | | - Switch, |
7 | | - Button, |
8 | | - Pressable, |
9 | | - ScrollView, |
10 | | - TextInput, |
11 | | - View |
12 | | -} from 'react-native' |
| 2 | +import { Text, StyleSheet, PixelRatio, Switch, Button, Pressable, ScrollView, TextInput, View } from 'react-native' |
13 | 3 | import CountryPicker, { CountryModalProvider, TranslationLanguageCodeList } from './src/' |
14 | 4 | import { CountryCode, Country } from './src/types' |
15 | 5 | import { Row } from './src/Row' |
16 | 6 | import { DARK_THEME } from './src/CountryTheme' |
17 | 7 |
|
18 | 8 | const styles = StyleSheet.create({ |
19 | | - mainBox: { |
20 | | - paddingVertical: 10, |
21 | | - justifyContent: 'space-between', |
22 | | - alignItems: 'center', |
23 | | - flex: 1, |
24 | | - }, |
25 | | - container: { |
26 | | - paddingVertical: 10, |
27 | | - justifyContent: 'space-between', |
28 | | - alignItems: 'center', |
29 | | - }, |
30 | | - welcome: { |
31 | | - fontSize: 17, |
32 | | - textAlign: 'center', |
33 | | - marginHorizontal: 5, |
34 | | - marginVertical: 10 |
35 | | - }, |
36 | | - heading: { |
37 | | - fontSize: 17, |
38 | | - textAlign: 'center', |
39 | | - marginHorizontal: 5, |
40 | | - marginVertical: 10 |
41 | | - }, |
42 | | - instructions: { |
43 | | - fontSize: 10, |
44 | | - textAlign: 'center', |
45 | | - color: '#888', |
46 | | - marginBottom: 5, |
47 | | - paddingHorizontal: 15 |
48 | | - }, |
49 | | - data: { |
50 | | - // maxWidth: 250, |
51 | | - padding: 10, |
52 | | - marginTop: 7, |
53 | | - backgroundColor: '#ddd', |
54 | | - borderColor: '#888', |
55 | | - borderWidth: 1 / PixelRatio.get(), |
56 | | - color: '#777', |
57 | | - }, |
58 | | - button: { |
59 | | - paddingVertical: 5, |
60 | | - paddingHorizontal: 10, |
61 | | - backgroundColor: '#cfd4e7', |
62 | | - marginHorizontal: 4 |
63 | | - }, |
| 9 | + mainBox: { paddingVertical: 10, justifyContent: 'space-between', alignItems: 'center', flex: 1, }, |
| 10 | + container: { paddingVertical: 10, justifyContent: 'space-between', alignItems: 'center', }, |
| 11 | + button: { paddingVertical: 5, paddingHorizontal: 10, marginHorizontal: 4, backgroundColor: '#cfd4e7' }, |
| 12 | + countryPickerBox: { maxHeight: '30%', paddingVertical: 5, paddingHorizontal: 10, backgroundColor: '#eee' }, |
| 13 | + optionsBox: { maxHeight: '30%', marginVertical: 10, borderColor: '#abc', borderWidth: 1 / PixelRatio.get() }, |
| 14 | + dataBox: { flex: 1, width: '90%', marginHorizontal: 5, borderWidth: 1 / PixelRatio.get() }, |
| 15 | + textEntryBox: { flex: 1, padding: 5, marginLeft: 20, flexDirection: 'row', justifyContent: 'flex-start' }, |
| 16 | + buttonsBox: { flexDirection: 'row', marginTop: 2 }, |
64 | 17 | // |
65 | | - textEntryLabel: { flex: 2, width: "25%" }, |
66 | | - textEntryBox: { flex: 1, flexDirection: 'row', justifyContent: 'flex-start', padding: 5, marginLeft: 20 }, |
67 | | - textEntryInput: { flex: 3, marginLeft: 2 }, |
| 18 | + welcome: { fontSize: 17, textAlign: 'center', marginHorizontal: 5, marginVertical: 10, }, |
| 19 | + heading: { fontSize: 17, textAlign: 'center', marginHorizontal: 5, marginVertical: 10, }, |
| 20 | + instructions: { fontSize: 10, textAlign: 'center', color: '#888', marginBottom: 5, paddingHorizontal: 15, }, |
| 21 | + data: { padding: 10, marginTop: 7, backgroundColor: '#ddd', borderColor: '#888', borderWidth: 1 / PixelRatio.get(), color: '#777' }, |
68 | 22 | // |
69 | | - countryPickerBox: { paddingVertical: 5, paddingHorizontal: 10, backgroundColor: '#eee', maxHeight: "30%" }, |
70 | | - optionsBox: { maxHeight: "30%", marginVertical: 10, borderColor: '#abc', borderWidth: 1 }, |
71 | | - dataBox: { flex: 1, width: "90%", marginHorizontal: 5, borderWidth: 1 }, |
72 | | - buttonsBox: { flexDirection: 'row', marginTop: 2 } |
| 23 | + textEntryLabel: { flex: 2, width: '25%' }, |
| 24 | + textEntryInput: { flex: 3, marginLeft: 2 }, |
73 | 25 | // |
74 | 26 | }) |
75 | 27 |
|
@@ -105,156 +57,123 @@ export default function App() { |
105 | 57 | const [fontScaling, setFontScaling] = useState<boolean>(true) |
106 | 58 | const [disableNativeModal, setDisableNativeModal] = useState<boolean>(false) |
107 | 59 |
|
108 | | - const [preferredCountriesStr, setPreferredCountriesStr] = useState<string>('UA') |
109 | | - const [excludeCountriesStr, setExcludeCountriesStr] = useState<string>('RU') |
| 60 | + const [preferredCountriesStr, setPreferredCountriesStr] = useState<string>('UA') |
| 61 | + const [excludeCountriesStr, setExcludeCountriesStr] = useState<string>('RU') |
110 | 62 | const [translation, setTranslation] = useState<string>(null) |
111 | 63 |
|
112 | | - const resetCountry = () => { setCountry(null); setCountryCode(null) } |
| 64 | + const resetCountry = () => { |
| 65 | + setCountry(null) |
| 66 | + setCountryCode(null) |
| 67 | + } |
113 | 68 | const onSelect = (country: Country) => { |
114 | 69 | setCountryCode(country.cca2) |
115 | 70 | setCountry(country) |
116 | 71 | } |
117 | 72 | const switchVisible = () => setVisible(!visible) |
118 | 73 |
|
119 | | - const excludeCountries = excludeCountriesStr.toUpperCase().trim().split(/\W+/) |
| 74 | + const excludeCountries = excludeCountriesStr.toUpperCase().trim().split(/\W+/) |
120 | 75 | const preferredCountries = preferredCountriesStr.toUpperCase().trim().split(/\W+/) |
121 | 76 |
|
122 | 77 | return ( |
123 | 78 | <CountryModalProvider> |
124 | | - <View style={styles.mainBox}> |
125 | | - <Text style={styles.welcome}>Welcome to Country Picker !</Text> |
126 | | - <Text style={styles.instructions}>Press on the flag/placeholder to open modal:</Text> |
127 | | - |
128 | | - <View style={styles.countryPickerBox}> |
129 | | - <CountryPicker |
130 | | - theme={dark ? DARK_THEME : {}} |
131 | | - {...{ |
132 | | - allowFontScaling: fontScaling, |
133 | | - countryCode, |
134 | | - withFilter, |
135 | | - excludeCountries, |
136 | | - withFlag, |
137 | | - withCurrencyButton, |
138 | | - withCallingCodeButton, |
139 | | - withCountryNameButton, |
140 | | - withLetterScroller, |
141 | | - withCallingCode, |
142 | | - withCurrency, |
143 | | - withEmoji, |
144 | | - withModal, |
145 | | - withFlagButton, |
146 | | - onSelect, |
147 | | - disableNativeModal, |
148 | | - preferredCountries, |
149 | | - translation, |
150 | | - modalProps: { |
151 | | - visible, |
152 | | - }, |
153 | | - onClose: () => setVisible(false), |
154 | | - onOpen: () => setVisible(true), |
155 | | - placeholder: '(-country-)', |
156 | | - }} |
157 | | - /> |
158 | | - </View> |
| 79 | + <View style={styles.mainBox}> |
| 80 | + <Text style={styles.welcome}>Welcome to Country Picker !</Text> |
| 81 | + <Text style={styles.instructions}>Press on the flag/placeholder to open modal:</Text> |
159 | 82 |
|
160 | | - <View style={styles.optionsBox}> |
161 | | - <ScrollView> |
162 | | - <Text style={styles.heading}>Options:</Text> |
163 | | - <View style={styles.textEntryBox}> |
164 | | - <Text style={styles.textEntryLabel}>Preferred Countries: </Text> |
165 | | - <TextInput style={styles.textEntryInput}value={preferredCountriesStr} onChangeText={setPreferredCountriesStr} /> |
| 83 | + <View style={styles.countryPickerBox}> |
| 84 | + <CountryPicker |
| 85 | + theme={dark ? DARK_THEME : {}} |
| 86 | + {...{ |
| 87 | + allowFontScaling: fontScaling, |
| 88 | + countryCode, |
| 89 | + withFilter, |
| 90 | + excludeCountries, |
| 91 | + withFlag, |
| 92 | + withCurrencyButton, |
| 93 | + withCallingCodeButton, |
| 94 | + withCountryNameButton, |
| 95 | + withLetterScroller, |
| 96 | + withCallingCode, |
| 97 | + withCurrency, |
| 98 | + withEmoji, |
| 99 | + withModal, |
| 100 | + withFlagButton, |
| 101 | + onSelect, |
| 102 | + disableNativeModal, |
| 103 | + preferredCountries, |
| 104 | + translation, |
| 105 | + modalProps: { |
| 106 | + visible, |
| 107 | + }, |
| 108 | + onClose: () => setVisible(false), |
| 109 | + onOpen: () => setVisible(true), |
| 110 | + placeholder: '(-country-)', |
| 111 | + }} |
| 112 | + /> |
166 | 113 | </View> |
167 | | - <View style={styles.textEntryBox}> |
168 | | - <Text style={styles.textEntryLabel}>Exclude Countries: </Text> |
169 | | - <TextInput style={styles.textEntryInput}value={excludeCountriesStr} onChangeText={setExcludeCountriesStr} /> |
170 | | - </View> |
171 | | - <View style={styles.textEntryBox}> |
172 | | - <Text style={styles.textEntryLabel}>Translation:</Text> |
173 | | - <TextInput style={styles.textEntryInput} autoCorrect={false} value={translation} onChangeText={setTranslation} /> |
| 114 | + |
| 115 | + <View style={styles.optionsBox}> |
| 116 | + <ScrollView> |
| 117 | + <Text style={styles.heading}>Options:</Text> |
| 118 | + <View style={styles.textEntryBox}> |
| 119 | + <Text style={styles.textEntryLabel}>Preferred Countries: </Text> |
| 120 | + <TextInput |
| 121 | + style={styles.textEntryInput} |
| 122 | + value={preferredCountriesStr} |
| 123 | + onChangeText={setPreferredCountriesStr} |
| 124 | + autoCapitalize='characters' |
| 125 | + /> |
| 126 | + </View> |
| 127 | + <View style={styles.textEntryBox}> |
| 128 | + <Text style={styles.textEntryLabel}>Exclude Countries: </Text> |
| 129 | + <TextInput |
| 130 | + style={styles.textEntryInput} |
| 131 | + value={excludeCountriesStr} |
| 132 | + onChangeText={setExcludeCountriesStr} |
| 133 | + autoCapitalize='characters' |
| 134 | + /> |
| 135 | + </View> |
| 136 | + <View style={styles.textEntryBox}> |
| 137 | + <Text style={styles.textEntryLabel}>Translation:</Text> |
| 138 | + <TextInput style={styles.textEntryInput} autoCorrect={false} value={translation} onChangeText={setTranslation} autoCapitalize='none' /> |
| 139 | + </View> |
| 140 | + <Text style={styles.instructions}>{TranslationLanguageCodeList.join(' / ')}</Text> |
| 141 | + <Text style={styles.instructions}> |
| 142 | + ex: {excludeCountries.join('|')} pr: {preferredCountries.join('|')} |
| 143 | + </Text> |
| 144 | + <Option title='Show country name on button' value={withCountryNameButton} onValueChange={setWithCountryNameButton} /> |
| 145 | + <Option title='Show currency on button' value={withCurrencyButton} onValueChange={setWithCurrencyButton} /> |
| 146 | + <Option title='Show calling code on button' value={withCallingCodeButton} onValueChange={setWithCallingCodeButton} /> |
| 147 | + <Option title='Show flag on button' value={withFlagButton} onValueChange={setWithFlagButton} /> |
| 148 | + <Option title='With font scaling' value={fontScaling} onValueChange={setFontScaling} /> |
| 149 | + <Option title='Use emoji (not image) flags' value={withEmoji} onValueChange={setWithEmoji} /> |
| 150 | + <Option title='Provide type-to-filter entry' value={withFilter} onValueChange={setWithFilter} /> |
| 151 | + <Option title='Show flag in picker' value={withFlag} onValueChange={setWithFlag} /> |
| 152 | + <Option title='Show calling code in picker' value={withCallingCode} onValueChange={setWithCallingCode} /> |
| 153 | + <Option title='Show currency in picker' value={withCurrency} onValueChange={setWithCurrency} /> |
| 154 | + <Option title='Show letter scroller on right' value={withLetterScroller} onValueChange={setWithLetterScroller} /> |
| 155 | + <Option title='Without native modal' value={disableNativeModal} onValueChange={setDisableNativeModal} /> |
| 156 | + <Option title='With modal' value={withModal} onValueChange={setWithModal} /> |
| 157 | + <Option title='With dark theme' value={dark} onValueChange={setDark} /> |
| 158 | + </ScrollView> |
174 | 159 | </View> |
175 | | - <Text style={styles.instructions}>One of: {TranslationLanguageCodeList.join(' / ')}</Text> |
176 | | - <Text style={styles.instructions}>ex: {excludeCountries.join('|')} pr: {preferredCountries.join('|')}</Text> |
177 | | - <Option |
178 | | - title='Show country name on button' |
179 | | - value={withCountryNameButton} |
180 | | - onValueChange={setWithCountryNameButton} |
181 | | - /> |
182 | | - <Option |
183 | | - title='Show currency on button' |
184 | | - value={withCurrencyButton} |
185 | | - onValueChange={setWithCurrencyButton} |
186 | | - /> |
187 | | - <Option |
188 | | - title='Show calling code on button' |
189 | | - value={withCallingCodeButton} |
190 | | - onValueChange={setWithCallingCodeButton} |
191 | | - /> |
192 | | - <Option |
193 | | - title='Show flag on button' |
194 | | - value={withFlagButton} |
195 | | - onValueChange={setWithFlagButton} |
196 | | - /> |
197 | | - <Option |
198 | | - title='With font scaling' |
199 | | - value={fontScaling} |
200 | | - onValueChange={setFontScaling} |
201 | | - /> |
202 | | - <Option |
203 | | - title='Use emoji (not image) flags' |
204 | | - value={withEmoji} |
205 | | - onValueChange={setWithEmoji} |
206 | | - /> |
207 | | - <Option |
208 | | - title='Provide type-to-filter entry' |
209 | | - value={withFilter} |
210 | | - onValueChange={setWithFilter} |
211 | | - /> |
212 | | - <Option |
213 | | - title='Show flag in picker' |
214 | | - value={withFlag} |
215 | | - onValueChange={setWithFlag} |
216 | | - /> |
217 | | - <Option |
218 | | - title='Show calling code in picker' |
219 | | - value={withCallingCode} |
220 | | - onValueChange={setWithCallingCode} |
221 | | - /> |
222 | | - <Option |
223 | | - title='Show currency in picker' |
224 | | - value={withCurrency} |
225 | | - onValueChange={setWithCurrency} |
226 | | - /> |
227 | | - <Option |
228 | | - title='Show letter scroller on right' |
229 | | - value={withLetterScroller} |
230 | | - onValueChange={setWithLetterScroller} |
231 | | - /> |
232 | | - <Option |
233 | | - title='Without native modal' |
234 | | - value={disableNativeModal} |
235 | | - onValueChange={setDisableNativeModal} |
236 | | - /> |
237 | | - <Option |
238 | | - title='With modal' |
239 | | - value={withModal} |
240 | | - onValueChange={setWithModal} |
241 | | - /> |
242 | | - <Option title='With dark theme' value={dark} onValueChange={setDark} /> |
243 | | - </ScrollView> |
244 | | - </View> |
245 | 160 |
|
246 | | - <View style={styles.dataBox}> |
247 | | - <ScrollView> |
248 | | - <Text style={styles.heading}>Result:</Text> |
249 | | - <Text style={styles.data}>{JSON.stringify(country, null, 2)}</Text> |
250 | | - </ScrollView> |
251 | | - </View> |
| 161 | + <View style={styles.dataBox}> |
| 162 | + <ScrollView> |
| 163 | + <Text style={styles.heading}>Result:</Text> |
| 164 | + <Text style={styles.data}>{JSON.stringify(country, null, 2)}</Text> |
| 165 | + </ScrollView> |
| 166 | + </View> |
252 | 167 |
|
253 | | - <View style={styles.buttonsBox}> |
254 | | - <Pressable onPress={resetCountry} style={styles.button}><Text>Reset Country</Text></Pressable> |
255 | | - <Pressable onPress={switchVisible} style={styles.button}><Text>Open Modal Picker</Text></Pressable> |
256 | | - </View> |
257 | | - </View> |
| 168 | + <View style={styles.buttonsBox}> |
| 169 | + <Pressable onPress={resetCountry} style={styles.button}> |
| 170 | + <Text>Reset Country</Text> |
| 171 | + </Pressable> |
| 172 | + <Pressable onPress={switchVisible} style={styles.button}> |
| 173 | + <Text>Open Modal Picker</Text> |
| 174 | + </Pressable> |
| 175 | + </View> |
| 176 | + </View> |
258 | 177 | </CountryModalProvider> |
259 | 178 | ) |
260 | 179 | } |
0 commit comments