Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: country picker modal with close button #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export default function App() {
// For showing picker just put show state to show prop
<CountryPicker
show={show}
closeModal={() => setShow(false)}
// when picker button press you will get the country object with dial code
pickerButtonOnPress={(item) => {
setCountryCode(item.dial_code);
setShow(false);
}}
/>
</View>
Expand Down Expand Up @@ -120,10 +120,10 @@ export default function App() {
// For showing picker just put show state to show prop
<CountryPicker
show={show}
closeModal={() => setShow(false)}
// when picker button press you will get the country object with dial code
pickerButtonOnPress={(item) => {
setCountryCode(item.dial_code);
setShow(false);
}}
ListHeaderComponent={ListHeaderComponent}
popularCountries={['en', 'ua', 'pl']}
Expand Down Expand Up @@ -178,6 +178,7 @@ Below are the props you can pass to the React Component.
| Prop | Type | Default | Example | Description |
| -------------------------- | --------- | ------- | ------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| show | boolean | | | This prop using for displaying the modal. Put your show state here. |
| closeModal | function | | closeModal={() => setShow(false)} | Function used to close the modal. |
| pickerButtonOnPress | function | | (country) => setCode(country.dial_code) | Put your function/functions here for getting country data from picker. |
| inputPlaceholder | string | | inputPlaceholder={'Your placeholder'} | If you need a custom placeholder for your input you may need this prop. |
| searchMessage | string | | searchMessage={'Some search message here'} | If you want to customize search message just use this prop. |
Expand All @@ -193,6 +194,7 @@ Below are the props you can pass to the React Component.
| showOnly | array | | showOnly={['UA', 'EN']} | This prop allow you to configure which countries you want to show. |
| popularCountries | array | | popularCountries={['UA', 'EN']} | This prop allow you to send popular countries array to your ListHeaderComponent. |
| ListHeaderComponent | JSX.Element | | ListHeaderComponent={ListHeaderComponent} | This prop allow you to create header component to show popular countries on top of list! Check example section with ListHeaderComponent |
| showCloseButton | boolean | true | buttonCloseModal={true} | show or not show the close button when the modal is open |

:grey_exclamation: Also you can use all other FlatList and TextInput props if you need. :grey_exclamation:

Expand All @@ -201,6 +203,7 @@ Below are the props you can pass to the React Component.
```JS
<CountryPicker
show={show}
closeModal={() => setShow(false)}
lang={'cz'}
style={{
// Styles for whole modal [View]
Expand Down Expand Up @@ -252,7 +255,6 @@ Below are the props you can pass to the React Component.
}}
pickerButtonOnPress={(item) => {
setCountryCode(item.dial_code);
setShow(false);
}}
/>
```
Expand Down
44 changes: 42 additions & 2 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
Keyboard,
ViewStyle,
Modal,
TextStyle
TextStyle,
TouchableOpacity
} from 'react-native';
import { CountryItem, ItemTemplateProps, Style, ListHeaderComponentProps } from "./types/Types";
import { useKeyboardStatus } from "./helpers/useKeyboardStatus";
Expand Down Expand Up @@ -55,12 +56,14 @@ interface Props {
show: boolean,
enableModalAvoiding?: boolean,
disableBackdrop?: boolean,
closeButton?: boolean,

onBackdropPress?: (...args: any) => any,
pickerButtonOnPress: (item: CountryItem) => any,
itemTemplate?: (props: ItemTemplateProps) => JSX.Element,
ListHeaderComponent?: (props: ListHeaderComponentProps) => JSX.Element,
onRequestClose?: (...args: any) => any,
closeModal?: (...args: any) => any,

lang: string,
inputPlaceholder?: string,
Expand Down Expand Up @@ -89,6 +92,8 @@ export const CountryPicker = ({
showOnly,
ListHeaderComponent,
itemTemplate: ItemTemplate = CountryButton,
closeModal: closeCountryPicker,
showCloseButton,
...rest
}: Props) => {
// ToDo refactor exclude and showOnly props to objects
Expand Down Expand Up @@ -207,6 +212,7 @@ export const CountryPicker = ({
onPress={() => {
Keyboard.dismiss();
typeof pickerButtonOnPress === 'function' && pickerButtonOnPress(item);
typeof closeCountryPicker === 'function' && closeCountryPicker()
}}
/>
);
Expand Down Expand Up @@ -261,6 +267,14 @@ export const CountryPicker = ({
},
]}
>
{showCloseButton === false ? null :
<TouchableOpacity
style={[styles.closeButton, style?.closeButton]}
onPress={()=> closeCountryPicker ? closeCountryPicker() : null}
>
<Text style={[styles.closeButtonText, style?.closeButtonText]}>&times;</Text>
</TouchableOpacity>
}
<View
style={{
flexDirection: 'row',
Expand Down Expand Up @@ -310,6 +324,7 @@ export const CountryPicker = ({
onPress={(item: CountryItem) => {
Keyboard.dismiss();
typeof pickerButtonOnPress === 'function' && pickerButtonOnPress(item);
typeof closeCountryPicker === 'function' && closeCountryPicker()
}}
/>
: null
Expand Down Expand Up @@ -437,7 +452,7 @@ export const CountryList = ({
};


type StyleKeys = 'container' | 'modal' | 'modalInner' | 'searchBar' | 'countryMessage' | 'line';
type StyleKeys = 'container' | 'modal' | 'modalInner' | 'searchBar' | 'countryMessage' | 'line' | 'closeButton' | 'closeButtonText';

const styles: { [key in StyleKeys]: ViewStyle } = {
container: {
Expand Down Expand Up @@ -494,4 +509,29 @@ const styles: { [key in StyleKeys]: ViewStyle } = {
alignSelf: 'center',
marginVertical: 5,
},
closeButton: {
backgroundColor: '#ee6e73',
shadowColor: '#000',
shadowOffset: {
width: 0,
height: 2,
},
shadowOpacity: 0.25,
shadowRadius: 3.84,
elevation: 5,
width: 60,
height: 60,
borderRadius: 100,
position: 'absolute',
bottom: 10,
alignSelf: 'center',
justifyContent: 'center',
alignItem: 'center',
zIndex: 999
},
closeButtonText:{
textAlign: 'center',
color: '#0A0A0A',
fontSize: 32
}
};
4 changes: 3 additions & 1 deletion types/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ export interface Style {
countryButtonStyles?: ViewStyle,
flag?: TextStyle,
dialCode?: TextStyle,
countryName?: TextStyle
countryName?: TextStyle,
closeButton?: ViewStyle,
closeButtonText?: TextStyle
}