Skip to content

Commit

Permalink
fix import
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed Oct 24, 2019
1 parent 2202ddb commit 7364f3d
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 6,957 deletions.
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ __tests__/
.expo/
.github/
.vscode/
web-build/
web-build/
lib/assets/data/countries-image.json
19 changes: 10 additions & 9 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
PixelRatio,
Switch,
Button,
ScrollView
ScrollView,
Platform
} from 'react-native'
import CountryPicker from './src/'
import { CountryCode, Country } from './src/types'
Expand Down Expand Up @@ -96,11 +97,13 @@ export default function App() {
onValueChange={setWithCallingCodeButton}
/>
<Option title="With flag" value={withFlag} onValueChange={setWithFlag} />
<Option
title="With emoji"
value={withEmoji}
onValueChange={setWithEmoji}
/>
{Platform.OS !== 'web' && (
<Option
title="With emoji"
value={withEmoji}
onValueChange={setWithEmoji}
/>
)}
<Option
title="With filter"
value={withFilter}
Expand Down Expand Up @@ -133,7 +136,7 @@ export default function App() {
onValueChange={setWithFlagButton}
/>
<CountryPicker
theme={dark ? DARK_THEME : undefined}
theme={dark ? DARK_THEME : { flagSizeButton: 30, flagSize: 15 }}
{...{
countryCode,
withFilter,
Expand All @@ -155,8 +158,6 @@ export default function App() {
onOpen: () => setVisible(true)
}}
/>
{/* <CountryPicker countryCode="FR" /> */}

<Text style={styles.instructions}>Press on the flag to open modal</Text>
<Button
title={'Open modal from outside using visible props'}
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ export default function App() {
- `onSelect`(country: Country): void ([Country](https://github.com/xcarpentier/react-native-country-picker-modal/blob/master/src/types.ts#L263))
- `onOpen`(): void
- `onClose`(): void
- `closeButtonImage?`: [ImageSourcePropType](https://facebook.github.io/react-native/docs/image#props)
- `closeButtonStyle?`: StyleProp<ViewStyle>
- `closeButtonImageStyle?`: StyleProp<ImageStyle>

## Dark theme example

Expand Down
27 changes: 7 additions & 20 deletions __tests__/__snapshots__/CountryPicker.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,12 @@ Array [
}
}
>
<Text
allowFontScaling={false}
style={
Object {
"alignItems": "center",
"backgroundColor": "transparent",
"borderColor": "transparent",
"borderWidth": 0.5,
"fontSize": 30,
"justifyContent": "center",
}
}
>
<Text
allowFontScaling={false}
>
🇺🇸
</Text>
</Text>
<ActivityIndicator
animating={true}
color="#999999"
hidesWhenStopped={true}
size="small"
/>
</View>
</View>
</View>,
Expand Down Expand Up @@ -165,7 +152,7 @@ Array [
getItemCount={[Function]}
getItemLayout={[Function]}
horizontal={false}
initialNumToRender={12}
initialNumToRender={14}
keyExtractor={[Function]}
keyboardShouldPersistTaps="handled"
maxToRenderPerBatch={10}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"start:web": "yarn config:dev && expo start -w --dev",
"tsc": "node_modules/.bin/tsc --noEmit",
"build": "rm -rf lib/ && node_modules/.bin/tsc && cp -rf ./src/assets ./lib && yarn minify",
"minify": "yarn minify:image && yarn minify:emoji",
"minify:image": "./scripts/minify.js ./lib/assets/data/countries-image.json",
"minify": "yarn minify:emoji",
"minify:emoji": "./scripts/minify.js ./lib/assets/data/countries-emoji.json",
"config:dev": "json -I -f package.json -e 'this.main=\"node_modules/expo/AppEntry.js\"'",
"config:npm": "json -I -f package.json -e 'this.main=\"lib/index.js\"'",
Expand Down
4 changes: 2 additions & 2 deletions scripts/transform-world-countries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const isCca2 = process.argv.includes('--cca2')
const isRegion = process.argv.includes('--regions')
const isSubRegion = process.argv.includes('--subregions')

const getCountryNames = (common, translations) =>
const getCountryNameAsyncs = (common, translations) =>
Object.keys(translations)
.filter(k => k !== 'common')
.map(key => ({ [key]: translations[key].common }))
Expand Down Expand Up @@ -37,7 +37,7 @@ const newcountries = countries
region,
subregion,
flag: isEmoji ? `flag-${cca2.toLowerCase()}` : flags[cca2],
name: { common, ...getCountryNames(common, translations) }
name: { common, ...getCountryNameAsyncs(common, translations) }
}
})
)
Expand Down
36 changes: 18 additions & 18 deletions src/CountryContext.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import * as React from 'react'
import { TranslationLanguageCode } from './types'
import {
getEmojiFlag,
getImageFlag,
getCountryName,
getCountries,
getEmojiFlagAsync,
getImageFlagAsync,
getCountryNameAsync,
getCountriesAsync,
getLetters,
getCountryCallingCode,
getCountryCurrency,
getCountryCallingCodeAsync,
getCountryCurrencyAsync,
search
} from './CountryService'

export interface CountryContextParam {
translation?: TranslationLanguageCode
getCountryName: typeof getCountryName
getImageFlag: typeof getImageFlag
getEmojiFlag: typeof getEmojiFlag
getCountries: typeof getCountries
getCountryNameAsync: typeof getCountryNameAsync
getImageFlagAsync: typeof getImageFlagAsync
getEmojiFlagAsync: typeof getEmojiFlagAsync
getCountriesAsync: typeof getCountriesAsync
getLetters: typeof getLetters
getCountryCallingCode: typeof getCountryCallingCode
getCountryCurrency: typeof getCountryCurrency
getCountryCallingCodeAsync: typeof getCountryCallingCodeAsync
getCountryCurrencyAsync: typeof getCountryCurrencyAsync
search: typeof search
}
export const DEFAULT_COUNTRY_CONTEXT = {
translation: 'common' as TranslationLanguageCode,
getCountryName,
getImageFlag,
getEmojiFlag,
getCountries,
getCountryCallingCode,
getCountryCurrency,
getCountryNameAsync,
getImageFlagAsync,
getEmojiFlagAsync,
getCountriesAsync,
getCountryCallingCodeAsync,
getCountryCurrencyAsync,
search,
getLetters
}
Expand Down
19 changes: 14 additions & 5 deletions src/CountryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
TouchableOpacity,
ListRenderItemInfo,
PixelRatio,
FlatListProps
FlatListProps,
Dimensions
} from 'react-native'
import { useTheme } from './CountryTheme'
import { Country, Omit } from './types'
Expand Down Expand Up @@ -90,7 +91,7 @@ interface CountryItemProps {
onSelect(country: Country): void
}
const CountryItem = (props: CountryItemProps) => {
const { activeOpacity, itemHeight } = useTheme()
const { activeOpacity, itemHeight, flagSize } = useTheme()
const {
country,
onSelect,
Expand Down Expand Up @@ -118,7 +119,11 @@ const CountryItem = (props: CountryItemProps) => {
{...{ activeOpacity }}
>
<View style={[styles.itemCountry, { height: itemHeight }]}>
{withFlag && <Flag {...{ withEmoji, countryCode: country.cca2 }} />}
{withFlag && (
<Flag
{...{ withEmoji, countryCode: country.cca2, flagSize: flagSize! }}
/>
)}
<View style={styles.itemCountryName}>
<CountryText
allowFontScaling={false}
Expand Down Expand Up @@ -167,6 +172,8 @@ const ItemSeparatorComponent = () => {
)
}

const { height } = Dimensions.get('window')

export const CountryList = (props: CountryListProps) => {
const {
data,
Expand Down Expand Up @@ -212,14 +219,15 @@ export const CountryList = (props: CountryListProps) => {
scrollTo(letters[0], false)
}
}, [filterFocus])

const initialNumToRender = Math.round(height / (itemHeight || 1))
return (
<View style={[styles.container, { backgroundColor }]}>
<FlatList
onScrollToIndexFailed
ref={flatListRef}
testID="list-countries"
keyboardShouldPersistTaps="handled"
initialNumToRender={12}
automaticallyAdjustContentInsets={false}
scrollEventThrottle={1}
getItemLayout={(_data: any, index) => ({
Expand All @@ -238,7 +246,8 @@ export const CountryList = (props: CountryListProps) => {
data: search(filter, data),
keyExtractor,
onScrollToIndexFailed,
ItemSeparatorComponent
ItemSeparatorComponent,
initialNumToRender
}}
{...flatListProps}
/>
Expand Down
41 changes: 30 additions & 11 deletions src/CountryPicker.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { ReactNode, useState, useEffect } from 'react'
import { ModalProps, FlatListProps, StyleProp, ViewStyle } from 'react-native'
import {
ModalProps,
FlatListProps,
StyleProp,
ViewStyle,
ImageSourcePropType,
ImageStyle
} from 'react-native'
import { CountryModal } from './CountryModal'
import { HeaderModal } from './HeaderModal'
import { Country, CountryCode, FlagType, Region, Subregion } from './types'
Expand All @@ -21,17 +28,17 @@ const renderFlagButton = (
props.renderFlagButton ? (
props.renderFlagButton(props)
) : (
<FlagButton {...props} />
)
<FlagButton {...props} />
)

const renderFilter = (
props: CountryFilter['props'] & CountryPickerProps['renderCountryFilter']
): ReactNode =>
props.renderCountryFilter ? (
props.renderCountryFilter(props)
) : (
<CountryFilter {...props} />
)
<CountryFilter {...props} />
)

interface CountryPickerProps {
countryCode: CountryCode
Expand All @@ -55,6 +62,9 @@ interface CountryPickerProps {
withModal?: boolean
visible?: boolean
containerButtonStyle?: StyleProp<ViewStyle>
closeButtonImage?: ImageSourcePropType
closeButtonStyle?: StyleProp<ViewStyle>
closeButtonImageStyle?: StyleProp<ImageStyle>
renderFlagButton?(props: FlagButton['props']): ReactNode
renderCountryFilter?(props: CountryFilter['props']): ReactNode
onSelect(country: Country): void
Expand Down Expand Up @@ -88,15 +98,18 @@ export const CountryPicker = (props: CountryPickerProps) => {
withModal,
withFlagButton,
onClose: handleClose,
onOpen: handleOpen
onOpen: handleOpen,
closeButtonImage,
closeButtonStyle,
closeButtonImageStyle
} = props
const [state, setState] = useState<State>({
visible: props.visible || false,
countries: [],
filter: '',
filterFocus: false
})
const { translation, getCountries } = useContext()
const { translation, getCountriesAsync } = useContext()
const { visible, filter, countries, filterFocus } = state
const onOpen = () => {
setState({ ...state, visible: true })
Expand All @@ -119,7 +132,6 @@ export const CountryPicker = (props: CountryPickerProps) => {
}
const onFocus = () => setState({ ...state, filterFocus: true })
const onBlur = () => setState({ ...state, filterFocus: false })

const flagProp = {
withEmoji,
withCountryNameButton,
Expand All @@ -132,14 +144,15 @@ export const CountryPicker = (props: CountryPickerProps) => {
containerButtonStyle
}
useEffect(() => {
const countries = getCountries(
getCountriesAsync(
withEmoji ? FlagType.EMOJI : FlagType.FLAT,
translation,
region,
subregion,
countryCodes
)
setCountries(countries)
.then(setCountries)
.catch(console.error)
}, [translation, withEmoji])

return (
Expand All @@ -150,7 +163,13 @@ export const CountryPicker = (props: CountryPickerProps) => {
onRequestClose={onClose}
>
<HeaderModal
{...{ withFilter, withCloseButton, onClose }}
{...{
withFilter,
onClose,
closeButtonImage,
closeButtonImageStyle,
closeButtonStyle
}}
renderFilter={(props: CountryFilter['props']) =>
renderFilter({
...props,
Expand Down
Loading

0 comments on commit 7364f3d

Please sign in to comment.