UNPKG

react-native-expo-modal-select

Version:
120 lines 4.57 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } import { useCallback, useMemo, useState } from 'react'; import { FlatList, Modal, Pressable, SafeAreaView, StyleSheet, Text, View } from 'react-native'; import React from 'react'; import EmptyIndicator from './EmptyIndicator'; import SelectItem from './Item'; const ModalSelect = ({ testID, placeholder, items, cancelTouchableText = 'cancel', emptyIndicatorText = 'Sorry, there is nothing to be shown here', closeModalText = 'Back', onChange, value, modalStyle, closeModalComponent, cancelModalComponent, pressableComponent, modalHeaderComponent, emptyIndicatorComponent, modalItemComponent }) => { const [modalVisibility, setModalVisibility] = useState(false); const [selectedItem, setSelectedItem] = useState(items.find(item => item.value === value)); const handleChangeSelectedItem = useCallback(item => { onChange(item.value); setSelectedItem(item); setModalVisibility(false); }, [onChange]); const handleCancel = () => { setSelectedItem(undefined); setModalVisibility(false); }; const modalStyles = useMemo(() => { return [styles.modal, modalStyle]; }, [modalStyle]); return /*#__PURE__*/React.createElement(View, { testID: testID }, pressableComponent ? pressableComponent({ text: selectedItem ? selectedItem.label : placeholder, onOpen: () => setModalVisibility(true) }) : /*#__PURE__*/React.createElement(Pressable, { onPress: () => setModalVisibility(true) }, /*#__PURE__*/React.createElement(Text, { testID: "text-placeholder" }, selectedItem ? selectedItem.label : placeholder)), /*#__PURE__*/React.createElement(Modal, { animationType: "slide", visible: modalVisibility, onRequestClose: () => setModalVisibility(false) }, /*#__PURE__*/React.createElement(View, { style: modalStyles }, /*#__PURE__*/React.createElement(SafeAreaView, null, modalHeaderComponent ? modalHeaderComponent({ onClose: () => setModalVisibility(false), onCancel: handleCancel, title: selectedItem ? selectedItem.label : placeholder }) : /*#__PURE__*/React.createElement(View, { style: styles.modalHeader }, /*#__PURE__*/React.createElement(View, { style: styles.modalTitleContainer }, /*#__PURE__*/React.createElement(Text, null, selectedItem ? selectedItem.label : placeholder, ' ')), closeModalComponent ? closeModalComponent({ onClose: () => setModalVisibility(false) }) : /*#__PURE__*/React.createElement(Pressable, { onPress: () => setModalVisibility(false) }, /*#__PURE__*/React.createElement(Text, { style: styles.closeModalText }, closeModalText)), cancelModalComponent ? cancelModalComponent({ onCancel: handleCancel, text: cancelTouchableText }) : /*#__PURE__*/React.createElement(Pressable, { onPress: handleCancel }, /*#__PURE__*/React.createElement(Text, { style: styles.cancelModalText }, cancelTouchableText)))), !items.length ? /*#__PURE__*/React.createElement(React.Fragment, null, emptyIndicatorComponent ? emptyIndicatorComponent({ text: emptyIndicatorText }) : /*#__PURE__*/React.createElement(EmptyIndicator, null, /*#__PURE__*/React.createElement(Text, null, emptyIndicatorText))) : /*#__PURE__*/React.createElement(FlatList, { data: items, renderItem: info => { const { item, index } = info; return /*#__PURE__*/React.createElement(React.Fragment, null, modalItemComponent ? modalItemComponent({ item, index, focused: (selectedItem === null || selectedItem === void 0 ? void 0 : selectedItem.value) === item.value, onSelect: () => handleChangeSelectedItem(item) }) : /*#__PURE__*/React.createElement(SelectItem, _extends({ key: item.value, onPress: () => handleChangeSelectedItem(item) }, item))); } })))); }; const styles = StyleSheet.create({ modal: { flex: 1 }, modalHeader: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', width: '100%', padding: 16 }, closeModalText: { fontSize: 16 }, cancelModalText: { fontSize: 16 }, modalTitleContainer: { position: 'absolute', alignItems: 'center', left: 0, right: 0 } }); export default ModalSelect; //# sourceMappingURL=ModalSelect.js.map