Skip to content

Instantly share code, notes, and snippets.

@jonasgroendahl
Created August 10, 2020 13:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonasgroendahl/8d8059e548bb876b3dde3b8ef16b9b74 to your computer and use it in GitHub Desktop.
Save jonasgroendahl/8d8059e548bb876b3dde3b8ef16b9b74 to your computer and use it in GitHub Desktop.
Picker modal directly from the project
import React, {useState, useEffect} from 'react';
import {Modal, View, StyleSheet} from 'react-native';
import {Picker} from '@react-native-community/picker';
import Typography from './Typography';
import IconButton from './IconButton';
import Icon from './Icon';
import {useTranslation} from '../context/Language';
import ThemeColors from '../utils/Colors';
type Props = {
items: string[];
visible: boolean;
title: string;
onClose: () => void;
onSelect: (value: string) => void;
value: string;
};
const PickerModal: React.FC<Props> = ({
items,
visible,
onSelect,
onClose,
title,
value,
}) => {
const [pickerValue, setPickerValue] = useState<string>('');
const {placeholderAge, placeHolderGender, genderMale} = useTranslation();
useEffect(() => {
if (!value) {
const defaultPickerValue = title === 'age' ? '30' : genderMale;
setPickerValue(defaultPickerValue);
} else {
setPickerValue(value);
}
}, [visible, title, value, genderMale]);
const finalTitle = title === 'age' ? placeholderAge : placeHolderGender;
return (
<Modal animated transparent visible={visible} animationType="fade">
<View style={styles.container}>
<View style={styles.pickerContainer}>
<View style={styles.header}>
<IconButton onPress={onClose}>
<Icon name="close" />
</IconButton>
<Typography color="dark" style={styles.label}>
{finalTitle}
</Typography>
<IconButton
onPress={() => {
onSelect(pickerValue);
onClose();
}}>
<Icon name="check" />
</IconButton>
</View>
<Picker
selectedValue={pickerValue}
onValueChange={(itemValue, itemIndex) => setPickerValue(itemValue)}>
{items.map((item) => (
<Picker.Item value={item} label={item} />
))}
</Picker>
</View>
</View>
</Modal>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'flex-end',
backgroundColor: 'rgba(0,0,0,0.5)',
},
pickerContainer: {
height: 200,
width: '100%',
backgroundColor: 'white',
},
label: {
textTransform: 'capitalize',
},
header: {
justifyContent: 'space-between',
flexDirection: 'row',
alignItems: 'center',
backgroundColor: ThemeColors.white,
},
});
export default PickerModal;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment