import React, { useEffect, useState } from "react"; import { FlatList, Modal, SafeAreaView, StyleSheet, Text, View, } from "react-native"; import Buttons from "./components/button"; import { CheckBox } from "./components/checkbox"; import SearchBar from "./components/search-bar"; import { PickerProps } from "./utils/props-type"; import { COLORS } from "./utils/values"; const MultipleSelection: React.FC = ({ show, type, enableSearch = true, isCapsTitle = false, searchPlaceholder = "Search here", searchPlaceholderTextColor, pickerTitle, emptyTitle = "No Record(s) Found", data, value, rowTitleKey, rowUniqueKey, extraTitleSymbol = "", extraTitleKey = "", onDone, onClose, pickerColor, }) => { const [selectedData, setSelectedData] = useState([]); const [searchData, setSearchData] = useState([]); const [searchText, setSearchText] = useState(""); const [allSelected, setAllSelected] = useState(false); const listRef = React.useRef(null); useEffect(() => { if (value !== "" && show && type === "single") { setSelectedData(value); const index = data?.findIndex( (item) => item[rowUniqueKey] == value[rowUniqueKey] ); if (show && index > -1) { listRef.current?.scrollToIndex({ index: index, animated: true, viewPosition: 0.5, }); } } else if ( value !== "" && show && type === "multiple" && value?.length > 0 ) { setSelectedData( data?.filter((e) => value?.some((item) => item[rowUniqueKey] === e[rowUniqueKey]) ) ); } if (selectedData?.length === data?.length) { setAllSelected(true); } else { setAllSelected(false); } return () => { setSearchText(""); setSearchData([]); }; }, [show, value]); useEffect(() => { if (selectedData?.length === data?.length) { setAllSelected(true); } else { setAllSelected(false); } }, [selectedData]); return React.useMemo(() => { const onChangeText = (txt: string) => { setSearchData( data.filter((item) => { const mainTitle = item[rowTitleKey]?.toString().toLowerCase() || ""; const extraTitle = extraTitleKey ? item[extraTitleKey]?.toString().toLowerCase() || "" : ""; const searchText = txt.toLowerCase(); return ( mainTitle.includes(searchText) || extraTitle.includes(searchText) ); if (extraTitleKey) { return ( item[rowTitleKey] ?.toString() ?.toLowerCase() ?.includes(txt?.toLowerCase()) || item[extraTitleKey] ?.toString() ?.toLowerCase() ?.includes(txt?.toLowerCase()) ); } else { return item[rowTitleKey] ?.toString() ?.toLowerCase() ?.includes(txt?.toLowerCase()); } }) ); setSearchText(txt); }; const onDonePress = () => { if (selectedData?.length <= 0) { setSelectedData([]); onClosed(); } else { onDone(selectedData); setSelectedData([]); } }; const onClosed = () => { setSelectedData([]); onClose(); }; const onSelectAllPress = () => { if (!allSelected) { setSelectedData([...data]); setAllSelected(true); } else { setSelectedData([]); setAllSelected(false); } }; const onItemPress = (item: any, isAdded: boolean) => { let allData = [...selectedData]; if (type === "single") { setSelectedData([item]); } else { if (isAdded) { allData = allData.filter( (el) => el[rowUniqueKey] !== item[rowUniqueKey] ); } else { allData.push(item); } setSelectedData(allData); if (allData?.length === data?.length) { setAllSelected(true); } else { setAllSelected(false); } } }; const showListData = searchText === "" ? data : searchData; return ( {pickerTitle} {enableSearch && ( setSearchText("")} /> )} {type === "multiple" && showListData?.length > 0 && ( <> )} { return ( {emptyTitle} ); }} renderItem={({ item }) => { const active = selectedData?.some( (e) => e[rowUniqueKey] === item[rowUniqueKey] ); return ( onItemPress(item, active)} isChecked={active} pickerColor={pickerColor} isCapsTitle={isCapsTitle} title={`${item[rowTitleKey]}${ extraTitleSymbol && extraTitleSymbol }${extraTitleKey && item[extraTitleKey]}`} /> ); }} // snapToInterval={SIZE.S_54} decelerationRate="normal" viewabilityConfig={{ itemVisiblePercentThreshold: 50, // Adjust as needed }} onScrollToIndexFailed={(info) => { if (type === "single") { const wait = new Promise((resolve) => setTimeout(resolve, 500) ); wait.then(() => { listRef.current?.scrollToIndex({ index: info.index, animated: true, viewPosition: 0.5, }); }); } }} keyExtractor={(item) => item[rowUniqueKey]} /> ); }, [ show, data, type, pickerTitle, rowUniqueKey, rowTitleKey, extraTitleKey, extraTitleSymbol, onClose, onDone, searchText, searchData, selectedData, allSelected, ]); }; const styles = StyleSheet.create({ emptyView: { flex: 1, justifyContent: "center", alignItems: "center", marginTop: 110, }, emptyTitleText: { fontSize: 20, fontWeight: "500", textAlign: "center", color: COLORS.TITLE, }, listView: { marginBottom: 110 }, devider: { height: 1, backgroundColor: COLORS.DEVIDER, }, buttonsRow: { flexDirection: "row", padding: 10, justifyContent: "center", alignItems: "center", backgroundColor: COLORS.WHITE, }, pickerTitleText: { fontSize: 20, fontWeight: "bold", textAlign: "center", color: COLORS.PRIMARY, flex: 1, }, safearea: { flex: 1, }, bottomSafearea: { backgroundColor: COLORS.WHITE, flex: 0, }, container: { flex: 1, backgroundColor: COLORS.RGBA, justifyContent: "flex-end", }, innerContainer: { backgroundColor: COLORS.WHITE, paddingVertical: 15, paddingHorizontal: 5, maxHeight: "80%", borderTopLeftRadius: 10, borderTopRightRadius: 10, }, }); export default MultipleSelection;