diff --git a/index.d.ts b/index.d.ts index c4f41e5..0d52311 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,62 +1,72 @@ -import {ViewStyle, TextStyle} from 'react-native'; - +import React from "react"; +import { ViewStyle, TextStyle } from "react-native"; export interface CountryCodeProps { - /** - * Selected Country Dial Code - */ - selected: string, - /** - * Function to set the country - */ - setSelected: React.Dispatch>, - /** - * Function to set the country - */ - setCountryDetails?: React.Dispatch>, - /** - * State variable for storing the phone number - */ - phone?: string, - /** - * Function to set the phone number state variable - */ - setPhone?: React.Dispatch>, - /** - * Style the Country Code Container - */ - countryCodeContainerStyles?: ViewStyle, - /** - * Style the text inside Country Code - */ - countryCodeTextStyles?: TextStyle, - /** - * Phone Text Input Styles - */ - phoneStyles?: ViewStyle, - /** - * URL for the search Icon - */ - searchIcon?: string, - /** - * URL for the close Icon - */ - closeIcon?: string, - /** - * Search Input Container Styles - */ - searchStyles?: ViewStyle, - /** - * Search Input Text Styles - */ - searchTextStyles?: TextStyle, - /** - * Search Dropdown Container Styles - */ - dropdownStyles?: ViewStyle, - /** - * Search Dropdown Text Styles - */ - dropdownTextStyles?: TextStyle + /** + * Selected Country Dial Code + */ + selected: string; + /** + * Function to set the country + */ + setSelected: React.Dispatch>; + /** + * Function to set the country + */ + setCountryDetails: React.Dispatch>; + /** + * State variable for storing the phone number + */ + phone?: string; + /** + * Function to set the phone number state variable + */ + setPhone?: React.Dispatch>; + /** + * Style the Country Code Container + */ + countryCodeContainerStyles?: ViewStyle; + /** + * Style the text inside Country Code + */ + countryCodeTextStyles?: TextStyle; + /** + * Phone Text Input Styles + */ + phoneStyles?: ViewStyle; + /** + * URL for the search Icon + */ + searchIcon?: string; + /** + * URL for the close Icon + */ + closeIcon?: string; + /** + * Search Input Container Styles + */ + searchStyles?: ViewStyle; + /** + * Search Input Text Styles + */ + searchTextStyles?: TextStyle; + /** + * Search Dropdown Container Styles + */ + dropdownStyles?: ViewStyle; + /** + * Search Dropdown Text Styles + */ + dropdownTextStyles?: TextStyle; + /** + * Search Dropdown List Item Styles + */ + dropdownTextContainerStyles?: ViewStyle; +} -} \ No newline at end of file +export interface CountryCodeDropdownItemProps { + name: string; + dial_code: string; + code: string; + flag: string; +} diff --git a/index.tsx b/index.tsx index da34249..35361e9 100644 --- a/index.tsx +++ b/index.tsx @@ -1,270 +1,329 @@ -import React,{JSXElementConstructor} from 'react'; -import { View, Text, StyleSheet,FlatList, TouchableOpacity, Animated, TextInput, Image, ViewStyle, TextStyle } from 'react-native'; -import { countries, _getFlag } from './_inc/_lib/enhanced'; +import React, { JSXElementConstructor } from "react"; +import { + View, + Text, + StyleSheet, + FlatList, + TouchableOpacity, + Animated, + TextInput, + Image, + ViewStyle, + TextStyle, +} from "react-native"; +import { countries, _getFlag } from "./_inc/_lib/enhanced"; +const Countries: CountryCodeDropdownItemProps[] = countries; interface CountryCodeProps { - /** - * Selected Country Dial Code - */ - selected: string, - /** - * Function to set the country - */ - setSelected: React.Dispatch>, - /** - * Function to set the country - */ - setCountryDetails?: React.Dispatch>, - /** - * State variable for storing the phone number - */ - phone?: string, - /** - * Function to set the phone number state variable - */ - setPhone?: React.Dispatch>, - /** - * Style the Country Code Container - */ - countryCodeContainerStyles?: ViewStyle, - /** - * Style the text inside Country Code - */ - countryCodeTextStyles?: TextStyle, - /** - * Phone Text Input Styles - */ - phoneStyles?: ViewStyle, - /** - * URL for the search Icon - */ - searchIcon?: string, - /** - * URL for the close Icon - */ - closeIcon?: string, - /** - * Search Input Container Styles - */ - searchStyles?: ViewStyle, - /** - * Search Input Text Styles - */ - searchTextStyles?: TextStyle, - /** + /** + * Selected Country Dial Code + */ + selected: string; + /** + * Function to set the country + */ + setSelected: React.Dispatch>; + /** + * Function to set the country + */ + setCountryDetails: React.Dispatch< + React.SetStateAction + >; + /** + * State variable for storing the phone number + */ + phone?: string; + /** + * Function to set the phone number state variable + */ + setPhone?: React.Dispatch>; + /** + * Style the Country Code Container + */ + countryCodeContainerStyles?: ViewStyle; + /** + * Style the text inside Country Code + */ + countryCodeTextStyles?: TextStyle; + /** + * Phone Text Input Styles + */ + phoneStyles?: ViewStyle; + /** + * URL for the search Icon + */ + searchIcon?: string; + /** + * URL for the close Icon + */ + closeIcon?: string; + /** + * Search Input Container Styles + */ + searchStyles?: ViewStyle; + /** + * Search Input Text Styles + */ + searchTextStyles?: TextStyle; + /** /** * Search Dropdown Container Styles */ - dropdownStyles?: ViewStyle, - /** - * Search Dropdown Text Styles - */ - dropdownTextStyles?: TextStyle - + dropdownStyles?: ViewStyle; + /** + * Search Dropdown Text Styles + */ + dropdownTextStyles?: TextStyle; + /** + * Search Dropdown List Item Styles + */ + dropdownTextContainerStyles?: ViewStyle; } +interface CountryCodeDropdownItemProps { + name: string; + dial_code: string; + code: string; + flag: string; +} const CountryCodeDropdownPicker: React.FC = ({ - selected, - setSelected, - setCountryDetails= () => {}, - phone, - setPhone, - countryCodeContainerStyles={}, - countryCodeTextStyles={}, - phoneStyles={}, - searchIcon, - closeIcon, - searchStyles={}, - searchTextStyles={}, - dropdownStyles={}, - dropdownTextStyles={}, + selected, + setSelected, + setCountryDetails = (details: CountryCodeDropdownItemProps) => {}, + phone, + setPhone, + countryCodeContainerStyles = {}, + countryCodeTextStyles = {}, + phoneStyles = {}, + searchIcon, + closeIcon, + searchStyles = {}, + searchTextStyles = {}, + dropdownStyles = {}, + dropdownTextStyles = {}, + dropdownTextContainerStyles = {}, }) => { + const [_selected, _setSelected] = React.useState(false); + const [_search, _setSearch] = React.useState(""); + const [_countries, _setCountries] = React.useState(Countries); - const [_selected, _setSelected] = React.useState(false); - const [_search, _setSearch] = React.useState(''); - const [_countries, _setCountries] = React.useState(countries); - - const slideAnim = React.useRef(new Animated.Value(0)).current; + const slideAnim = React.useRef(new Animated.Value(0)).current; + const _static = { + search: searchIcon ?? require("./_inc/images/search.png"), + close: closeIcon ?? require("./_inc/images/close.png"), + }; - const _static = { - search: searchIcon ?? require('./_inc/images/search.png'), - close: closeIcon ?? require('./_inc/images/close.png') - }; + const slideDown = () => { + _setSelected(true); + Animated.timing(slideAnim, { + toValue: 235, + duration: 1200, + useNativeDriver: false, + }).start(); + }; + const slideUp = () => { + Animated.timing(slideAnim, { + toValue: 0, + duration: 300, + useNativeDriver: false, + }).start(() => _setSelected(false)); + }; + function _searchCountry(country: string) { + _setSearch(country); + let c = Countries.filter((item) => { + return item.name.includes(country); + }); + _setCountries(c); + } - const slideDown = () => { - _setSelected(true); - Animated.timing(slideAnim, { - toValue: 235, - duration: 1200, - useNativeDriver: false - }).start(); - }; - - const slideUp = () => { - Animated.timing(slideAnim, { - toValue: 0, - duration: 300, - useNativeDriver: false - }).start(() => _setSelected(false)); - }; - - function _searchCountry(country) { - _setSearch(country); - let c = countries.filter((item) => { return item.name.includes(country) }) - _setCountries(c); - } - - - - const RenderBtn = () => { - - if(!_selected){ - return( - - {_setCountries(countries); slideDown()} }> - - {_getFlag(selected)} - {selected} - - - { - (phone != undefined && setPhone != undefined) - ? - - : - <> - } - - - - ) - }else{ - return( - - - - - - - slideUp()} style={{marginHorizontal: 10}}> - - + const RenderBtn = () => { + if (!_selected) { + return ( + + { + _setCountries(Countries); + slideDown(); + }} + > + + + {_getFlag(selected)} + + {selected} + + + {phone != undefined && setPhone != undefined && ( + + )} + + ); + } else { + return ( + + + {_static.search && ( + + )} + + - - ) - } + slideUp()} + style={{ marginHorizontal: 10 }} + > + {_static.close && ( + + )} + + + ); } + }; + const renderCountryItem = ({ + item, + }: { + item: CountryCodeDropdownItemProps; + }) => { + return ( + { + setSelected(item.dial_code); + setCountryDetails(item); + slideUp(); + }} + > + {item?.flag} + + {item?.name} + + + ); + }; - const renderCountryItem = ({item}) => { - return( - {setSelected(item.dial_code); setCountryDetails(item); slideUp();} }> - { item?.flag } - { item?.name } - - ) - } - - - return( - - { RenderBtn() } + return ( + + {RenderBtn()} - { - (_selected) - ? - - item.code} - ListEmptyComponent={ No Result Found } - /> - - : - <> + {_selected && ( + + item.code} + ListEmptyComponent={ + + No Result Found + } - - - ) -} - + /> + + )} + + ); +}; export default CountryCodeDropdownPicker; const styles = StyleSheet.create({ - row: { - flexDirection: 'row', - alignItems: 'center' - }, - container: { - width: '100%', - }, - selectedContainer: { - padding: 10, - flexDirection: 'row', - minWidth: '20%', - alignItems: 'center', - justifyContent: 'space-between', - borderWidth: 1, - borderColor: '#dddddd', - borderRadius: 8, - backgroundColor: 'white' - }, - valuesContainer: { - borderWidth: 1, - borderColor: '#dddddd', - borderRadius: 8, - maxHeight: 235, - backgroundColor: 'white', - marginTop: 8 - }, - countryContainer: { - flexDirection: 'row', - paddingHorizontal: 15, - paddingVertical: 13, - borderBottomWidth: 1, - borderColor: '#dedede', - alignItems: 'center' - }, - countryFlag: { - marginRight: 8, - color: 'black' - }, - countryText: { - fontWeight: 'bold' - }, - inputBoxContainer: { - width: '100%', - borderWidth: 1, - borderColor: '#dddddd', - borderRadius: 8, - flexDirection: 'row', - justifyContent: 'space-between', - alignItems: 'center' - }, - icon: { - width: 10, - height: 10 - } -}); \ No newline at end of file + row: { + flexDirection: "row", + alignItems: "center", + }, + container: { + width: "100%", + }, + selectedContainer: { + padding: 10, + flexDirection: "row", + minWidth: "20%", + alignItems: "center", + justifyContent: "space-between", + borderWidth: 1, + borderColor: "#dddddd", + borderRadius: 8, + backgroundColor: "white", + }, + valuesContainer: { + borderWidth: 1, + borderColor: "#dddddd", + borderRadius: 8, + maxHeight: 235, + backgroundColor: "white", + marginTop: 8, + }, + countryContainer: { + flexDirection: "row", + paddingHorizontal: 15, + paddingVertical: 13, + borderBottomWidth: 1, + borderColor: "#dedede", + alignItems: "center", + }, + countryFlag: { + marginRight: 8, + color: "black", + }, + countryText: { + fontWeight: "bold", + }, + inputBoxContainer: { + width: "100%", + borderWidth: 1, + borderColor: "#dddddd", + borderRadius: 8, + flexDirection: "row", + justifyContent: "space-between", + alignItems: "center", + }, + icon: { + width: 10, + height: 10, + }, +}); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9b781f7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,13 @@ +{ + "name": "react-native-dropdown-country-picker", + "version": "1.0.11", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "react-native-dropdown-country-picker", + "version": "1.0.11", + "license": "MIT" + } + } +}