Skip to content

Commit

Permalink
Add preferred countries options in country picker
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelsevla authored and xcarpentier committed Jul 3, 2020
1 parent 7a58ca3 commit 0049fe4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export default function App() {
- `closeButtonStyle?`: StyleProp<ViewStyle>
- `closeButtonImageStyle?`: StyleProp<ImageStyle>
- `disableNativeModal?`: boolean (you have to wrap your all app with CountryModalProvider)
- `preferredCountries`: [CountryCode](https://github.com/xcarpentier/react-native-country-picker-modal/blob/master/src/types.ts#L254) (`withAlphaFilter` must be false)

## Dark theme example

Expand Down
4 changes: 4 additions & 0 deletions src/CountryPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface CountryPickerProps {
subregion?: Subregion
countryCodes?: CountryCode[]
excludeCountries?: CountryCode[]
preferredCountries?: CountryCode[]
modalProps?: ModalProps
filterProps?: CountryFilterProps
flatListProps?: FlatListProps<Country>
Expand Down Expand Up @@ -108,6 +109,7 @@ export const CountryPicker = (props: CountryPickerProps) => {
closeButtonImageStyle,
excludeCountries,
placeholder,
preferredCountries
} = props
const [state, setState] = useState<State>({
visible: props.visible || false,
Expand Down Expand Up @@ -166,6 +168,8 @@ export const CountryPicker = (props: CountryPickerProps) => {
subregion,
countryCodes,
excludeCountries,
preferredCountries,
withAlphaFilter
)
.then(setCountries)
.catch(console.warn)
Expand Down
37 changes: 32 additions & 5 deletions src/CountryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,18 @@ export const getCountriesAsync = async (
subregion?: Subregion,
countryCodes?: CountryCode[],
excludeCountries?: CountryCode[],
preferredCountries?: CountryCode[],
withAlphaFilter?: boolean
): Promise<Country[]> => {
const countriesRaw = await loadDataAsync(flagType)
if (!countriesRaw) {
return []
}
const countries = CountryCodeList.filter(isCountryPresent(countriesRaw))

if (preferredCountries && !withAlphaFilter) {
const newCountryCodeList = [...preferredCountries, ...CountryCodeList.filter(code => !preferredCountries.includes(code))]

const countries = newCountryCodeList.filter(isCountryPresent(countriesRaw))
.map((cca2: CountryCode) => ({
cca2,
...{
Expand All @@ -148,11 +154,32 @@ export const getCountriesAsync = async (
.filter(isSubregion(subregion))
.filter(isIncluded(countryCodes))
.filter(isExcluded(excludeCountries))
.sort((country1: Country, country2: Country) =>
(country1.name as string).localeCompare(country2.name as string),
)

return countries

} else {
const countries = CountryCodeList.filter(isCountryPresent(countriesRaw))
.map((cca2: CountryCode) => ({
cca2,
...{
...countriesRaw[cca2],
name:
(countriesRaw[cca2].name as TranslationLanguageCodeMap)[
translation
] ||
(countriesRaw[cca2].name as TranslationLanguageCodeMap)['common'],
},
}))
.filter(isRegion(region))
.filter(isSubregion(subregion))
.filter(isIncluded(countryCodes))
.filter(isExcluded(excludeCountries))
.sort((country1: Country, country2: Country) =>
(country1.name as string).localeCompare(country2.name as string),
)

return countries
return countries
}
}

const DEFAULT_FUSE_OPTION = {
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ interface Props {
subregion?: Subregion
countryCodes?: CountryCode[]
excludeCountries?: CountryCode[]
preferredCountries?: CountryCode[]
theme?: Theme
translation?: TranslationLanguageCode
modalProps?: ModalProps
Expand Down

0 comments on commit 0049fe4

Please sign in to comment.