Skip to content

Commit bd9a619

Browse files
committed
fix #261
1 parent bd15578 commit bd9a619

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Switch,
77
Button,
88
ScrollView,
9-
Platform,
109
} from 'react-native'
1110
import CountryPicker from './src/'
1211
import { CountryCode, Country } from './src/types'
@@ -54,7 +53,7 @@ const Option = ({ value, onValueChange, title }: OptionProps) => (
5453
)
5554

5655
export default function App() {
57-
const [countryCode, setCountryCode] = useState<CountryCode>('FR')
56+
const [countryCode, setCountryCode] = useState<CountryCode | undefined>()
5857
const [country, setCountry] = useState<Country>(null)
5958
const [withCountryNameButton, setWithCountryNameButton] = useState<boolean>(
6059
false,

src/CountryPicker.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const renderFilter = (
4141
)
4242

4343
interface CountryPickerProps {
44-
countryCode: CountryCode
44+
countryCode?: CountryCode
4545
region?: Region
4646
subregion?: Subregion
4747
countryCodes?: CountryCode[]
@@ -62,6 +62,7 @@ interface CountryPickerProps {
6262
withFlag?: boolean
6363
withModal?: boolean
6464
visible?: boolean
65+
placeholder?: string
6566
containerButtonStyle?: StyleProp<ViewStyle>
6667
closeButtonImage?: ImageSourcePropType
6768
closeButtonStyle?: StyleProp<ViewStyle>
@@ -104,6 +105,7 @@ export const CountryPicker = (props: CountryPickerProps) => {
104105
closeButtonStyle,
105106
closeButtonImageStyle,
106107
excludeCountries,
108+
placeholder,
107109
} = props
108110
const [state, setState] = useState<State>({
109111
visible: props.visible || false,
@@ -135,15 +137,16 @@ export const CountryPicker = (props: CountryPickerProps) => {
135137
const onFocus = () => setState({ ...state, filterFocus: true })
136138
const onBlur = () => setState({ ...state, filterFocus: false })
137139
const flagProp = {
140+
countryCode,
138141
withEmoji,
139142
withCountryNameButton,
140143
withCallingCodeButton,
141144
withCurrencyButton,
142145
withFlagButton,
143-
countryCode,
144146
renderFlagButton: renderButton,
145147
onOpen,
146148
containerButtonStyle,
149+
placeholder,
147150
}
148151

149152
useEffect(() => {
@@ -211,4 +214,5 @@ CountryPicker.defaultProps = {
211214
withModal: true,
212215
withAlphaFilter: false,
213216
withCallingCode: false,
217+
placeholder: 'Select Country',
214218
}

src/FlagButton.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type FlagWithSomethingProp = Pick<
4141
| 'withCurrencyButton'
4242
| 'withCallingCodeButton'
4343
| 'withFlagButton'
44+
| 'placeholder'
4445
> & { flagSize: number }
4546

4647
const FlagText = (props: TextProps & { children: ReactNode }) => (
@@ -56,6 +57,7 @@ const FlagWithSomething = memo(
5657
withCallingCodeButton,
5758
withFlagButton,
5859
flagSize,
60+
placeholder,
5961
}: FlagWithSomethingProp) => {
6062
const { translation, getCountryInfoAsync } = useContext()
6163
const [state, setState] = useState({
@@ -65,9 +67,11 @@ const FlagWithSomething = memo(
6567
})
6668
const { countryName, currency, callingCode } = state
6769
useEffect(() => {
68-
getCountryInfoAsync({ countryCode, translation })
69-
.then(setState)
70-
.catch(console.warn)
70+
if (countryCode) {
71+
getCountryInfoAsync({ countryCode, translation })
72+
.then(setState)
73+
.catch(console.warn)
74+
}
7175
}, [
7276
countryCode,
7377
withCountryNameButton,
@@ -77,9 +81,14 @@ const FlagWithSomething = memo(
7781

7882
return (
7983
<View style={styles.flagWithSomethingContainer}>
80-
<Flag
81-
{...{ withEmoji, countryCode, withFlagButton, flagSize: flagSize! }}
82-
/>
84+
{countryCode ? (
85+
<Flag
86+
{...{ withEmoji, countryCode, withFlagButton, flagSize: flagSize! }}
87+
/>
88+
) : (
89+
<FlagText>{placeholder}</FlagText>
90+
)}
91+
8392
{withCountryNameButton && countryName ? (
8493
<FlagText>{countryName + ' '}</FlagText>
8594
) : null}
@@ -101,7 +110,8 @@ interface FlagButtonProps {
101110
withCallingCodeButton?: boolean
102111
withFlagButton?: boolean
103112
containerButtonStyle?: StyleProp<ViewStyle>
104-
countryCode: CountryCode
113+
countryCode?: CountryCode
114+
placeholder: string
105115
onOpen?(): void
106116
}
107117

@@ -114,6 +124,7 @@ export const FlagButton = ({
114124
countryCode,
115125
containerButtonStyle,
116126
onOpen,
127+
placeholder,
117128
}: FlagButtonProps) => {
118129
const { flagSizeButton: flagSize } = useTheme()
119130
return (
@@ -134,6 +145,7 @@ export const FlagButton = ({
134145
withCurrencyButton,
135146
withFlagButton,
136147
flagSize: flagSize!,
148+
placeholder,
137149
}}
138150
/>
139151
</View>

0 commit comments

Comments
 (0)