UNPKG

react-native-custom-dropdown-select

Version:

A simple and customizable React Native component for select with dropdown and customizabe style

107 lines (96 loc) 2.87 kB
import React, { useRef, useState } from 'react'; import { FlatList, StyleSheet, Text, TouchableOpacity, Modal, View, } from 'react-native'; import Icon from 'react-native-vector-icons/FontAwesome'; const Dropdown = (props) => { const { label, data, onSelect, icon, iconStyle, disabled } = props; const DropdownButton = useRef(); const [visible, setVisible] = useState(false); const [selected, setSelected] = useState(undefined); const [dropdownTop, setDropdownTop] = useState(0); const toggleDropdown = () => { visible ? setVisible(false) : openDropdown(); }; const openDropdown = () => { DropdownButton.current.measure((_fx, _fy, _w, h, _px, py) => { setDropdownTop(py - h); }); setVisible(true); }; const onItemPress = (item) => { onSelect(item); setVisible(false); }; const renderItem = ({ item }) => ( <TouchableOpacity style={styles.item} onPress={() => onItemPress(item)}> <Text>{item.label ? item.label : item}</Text> </TouchableOpacity> ); const renderDropdown = () => { return ( <Modal visible={visible} transparent animationType="none"> <TouchableOpacity style={styles.overlay} onPress={() => setVisible(false)} > <View style={[styles.dropdown, { top: dropdownTop }]}> <FlatList data={data} renderItem={renderItem} keyExtractor={(item, index) => index.toString()} /> </View> </TouchableOpacity> </Modal> ); }; return ( <TouchableOpacity ref={DropdownButton} style={styles.button} onPress={toggleDropdown} > {!disabled && renderDropdown()} <Text style={styles.buttonText}> {(!!selected && selected.label) || label} </Text> <Icon style={iconStyle} size={15} name={icon} /> </TouchableOpacity> ); }; const styles = StyleSheet.create({ button: { flexDirection: 'row', zIndex: 1, }, buttonText: { flex: 1, textAlign: 'center', }, dropdown: { // position: 'absolute', backgroundColor: '#fff', width: '100%', shadowColor: '#000000', shadowRadius: 4, shadowOffset: { height: 4, width: 0 }, shadowOpacity: 0.5, }, overlay: { // width: '80%', // height: '80%', margin: '10%' }, item: { paddingHorizontal: 10, paddingVertical: 10, borderBottomWidth: 1, }, }); export default Dropdown;