Skip to content

Commit

Permalink
fix type
Browse files Browse the repository at this point in the history
  • Loading branch information
xcarpentier committed Oct 8, 2019
1 parent 33aad8e commit 0a2222e
Show file tree
Hide file tree
Showing 11 changed files with 1,051 additions and 1,006 deletions.
28 changes: 23 additions & 5 deletions App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { useState } from 'react'
import { View, Text, StyleSheet, PixelRatio, Switch } from 'react-native'
import {
View,
Text,
StyleSheet,
PixelRatio,
Switch,
Button
} from 'react-native'
import CountryPicker from './src/'
import { CountryCode, Country } from './src/types'
import { Row } from './src/Row'
Expand All @@ -22,8 +29,8 @@ const styles = StyleSheet.create({
marginBottom: 5
},
data: {
padding: 15,
marginTop: 10,
padding: 10,
marginTop: 7,
backgroundColor: '#ddd',
borderColor: '#888',
borderWidth: 1 / PixelRatio.get(),
Expand Down Expand Up @@ -53,10 +60,12 @@ export default function App() {
const [withAlphaFilter, setWithAlphaFilter] = useState<boolean>(false)
const [withCallingCode, setWithCallingCode] = useState<boolean>(false)
const [withCurrency, setWithCurrency] = useState<boolean>(false)
const [visible, setVisible] = useState<boolean>(false)
const onSelect = (country: Country) => {
setCountryCode(country.cca2)
setCountry(country)
}
const switchVisible = () => setVisible(!visible)
return (
<View style={styles.container}>
<Text style={styles.welcome}>Welcome to Country Picker !</Text>
Expand Down Expand Up @@ -101,12 +110,21 @@ export default function App() {
withCallingCode,
withCurrency,
withEmoji,
onSelect
onSelect,
modalProps: {
visible
},
onClose: () => setVisible(false),
onOpen: () => setVisible(true)
}}
/>
<Text style={styles.instructions}>Press on the flag to open modal</Text>
<Button
title={'Open modal from outside using visible props'}
onPress={() => switchVisible()}
/>
{country !== null && (
<Text style={styles.data}>{JSON.stringify(country, null, 2)}</Text>
<Text style={styles.data}>{JSON.stringify(country, null, 0)}</Text>
)}
</View>
)
Expand Down
2 changes: 1 addition & 1 deletion data/countries-emoji.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion data/countries.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-country-picker-modal",
"version": "1.2.0",
"version": "1.3.0",
"description": "react-native country picker",
"main": "node_modules/expo/AppEntry.js",
"types": "lib/index.d.ts",
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 @@ -21,8 +21,8 @@ const getCountryNames = (common, translations) =>
const newcountries = countries
.map(({ cca2, currency, callingCode, name: { common }, translations }) => ({
[cca2]: {
currency: currency[0],
callingCode: callingCode[0],
currency,
callingCode,
flag: isEmoji ? `flag-${cca2.toLowerCase()}` : flags[cca2],
name: { common, ...getCountryNames(common, translations) }
}
Expand Down
4 changes: 2 additions & 2 deletions src/CountryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ const CountryItem = (props: CountryItemProps) => {
} = props
const extraContent: string[] = []
if (withCallingCode && country.callingCode) {
extraContent.push(`+${country.callingCode}`)
extraContent.push(`+${country.callingCode.join('|')}`)
}
if (withCurrency && country.currency) {
extraContent.push(country.currency)
extraContent.push(country.currency.join('|'))
}
return (
<TouchableOpacity
Expand Down
22 changes: 18 additions & 4 deletions src/CountryPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ interface CountryPickerProps {
renderFlagButton?(props: FlagButton['props']): ReactNode
renderCountryFilter?(props: CountryFilter['props']): ReactNode
onSelect(country: Country): void
onOpen?(): void
onClose?(): void
}

export const CountryPicker = (props: CountryPickerProps) => {
Expand All @@ -67,7 +69,9 @@ export const CountryPicker = (props: CountryPickerProps) => {
withCallingCode,
withCurrency,
withFlag,
withModal
withModal,
onClose: handleClose,
onOpen: handleOpen
} = props
const [state, setState] = useState<State>({
visible: props.visible || false,
Expand All @@ -76,8 +80,18 @@ export const CountryPicker = (props: CountryPickerProps) => {
})
const { translation, getCountries } = useContext()
const { visible, filter, countries } = state
const onOpen = () => setState({ ...state, visible: true })
const onClose = () => setState({ ...state, filter: '', visible: false })
const onOpen = () => {
setState({ ...state, visible: true })
if (handleOpen) {
handleOpen()
}
}
const onClose = () => {
setState({ ...state, filter: '', visible: false })
if (handleClose) {
handleClose()
}
}
const setFilter = (filter: string) => setState({ ...state, filter })
const setCountries = (countries: Country[]) =>
setState({ ...state, countries })
Expand All @@ -104,7 +118,7 @@ export const CountryPicker = (props: CountryPickerProps) => {
<>
{renderFlagButton(flagProp)}
<CountryModal
{...{ ...modalProps, visible, withModal }}
{...{ visible, withModal, ...modalProps }}
onRequestClose={onClose}
>
<HeaderModal
Expand Down
Loading

0 comments on commit 0a2222e

Please sign in to comment.