import React from 'react'; import { View, Text, StyleSheet, Image, TouchableOpacity, ScrollView, Animated, TextInput, ViewStyle, ActivityIndicator, Pressable} from 'react-native'; import { MultipleSelectListProps } from '..'; type L1Keys = { key?: any; value?: any; disabled?: boolean | undefined } const MultipleSelectList: React.FC = ({ fontFamily, setSelected, placeholder, boxStyles, inputStyles, dropdownStyles, dropdownItemStyles, dropdownTextStyles, maxHeight, data, searchicon = false, arrowicon = false, closeicon = false, checkicon = false, search = true, searchPlaceholder = "search", searchPlaceholderColor = "white", onSelect = () => {}, label, notFoundText = "No data found", disabledItemStyles, disabledTextStyles, disabledCheckBoxStyles, labelStyles, badgeStyles, badgeTextStyles, checkBoxStyles, save = 'key', dropdownShown = false, defaultOption = [], labelHeadingColor = 'initial', showSelected = true, customUserInput = false, setData, setDefaultOption, isApiSearch = false, loader = false, setSearchValue = () => {}, loaderSize, loaderColor }) => { const oldOption = React.useRef(null) const [_firstRender,_setFirstRender] = React.useState(true); const [dropdown, setDropdown] = React.useState(dropdownShown); const [selectedval, setSelectedVal] = React.useState(defaultOption); const [height,setHeight] = React.useState(350) const animatedvalue = React.useRef(new Animated.Value(0)).current; const [filtereddata,setFilteredData] = React.useState(data); const [customUserInputData, setCustomUserInputData] = React.useState("") const [counter, setCounter] = React.useState(0); const slidedown = () => { setDropdown(true) Animated.timing(animatedvalue,{ toValue:height, duration:500, useNativeDriver:false, }).start() } const slideup = () => { Animated.timing(animatedvalue,{ toValue:0, duration:500, useNativeDriver:false, }).start(() => setDropdown(false)) } React.useEffect( () => { if(maxHeight) setHeight(maxHeight) },[maxHeight]) React.useEffect(() => { setFilteredData(data); },[data]) // const setCustom = () => { // if(customUserInputData.length > 0){ // const unfilteredData = data.map((item: any,index) => ( item.value )); // console.log('unfilteredData',unfilteredData) // const customInputData = [customUserInputData].concat(unfilteredData) // const customInputDataFormatted = customInputData.map((item,index) => ({ // key: index + 1, // value: item // })) // console.log(customInputDataFormatted) // setData(customInputDataFormatted) // setDefaultOption((prev) => [...prev, customUserInputData]) // } // } // React.useEffect(() => { // setCustom(); // },[counter]) React.useEffect(() => { if(_firstRender){ _setFirstRender(false); return; } onSelect() },[selectedval]) React.useEffect(() => { if(!_firstRender){ if(dropdownShown) slidedown(); else slideup(); } },[dropdownShown]) return( { (dropdown && search) ? { (!searchicon) ? : searchicon } { if(isApiSearch) { setSearchValue(val) } let result = data.filter((item: L1Keys) => { val.toLowerCase(); let row = item.value.toLowerCase() return row.search(val.toLowerCase()) > -1; }); setFilteredData(result) if(result.length === 0 && customUserInput === true){ setCustomUserInputData(val) } }} style={[{padding:0,height:20,flex:1,fontFamily},inputStyles]} /> { slideup() setFilteredData(data) isApiSearch ? setSearchValue("") : null // setTimeout(() => setFilteredData(data), 800) }} > { (!closeicon) ? : closeicon } : (selectedval?.length > 0 ) ? { if(!dropdown){ slidedown() }else{ slideup() } }} > { label } { selectedval?.map((item,index) => { return ( {item} ) }) } : { if(!dropdown){ slidedown() }else{ slideup() } }}> { (selectedval == "") ? (placeholder) ? placeholder : 'Select option' : selectedval } { (!arrowicon) ? : arrowicon } } { (dropdown) ? { (isApiSearch && loader) ? : null } { (filtereddata.length >= 1) ? filtereddata.map((item: L1Keys,index: number) => { let key = item.key ?? item.value ?? item; let value = item.value ?? item; let disabled = item.disabled ?? false; if(disabled){ return( { (selectedval?.includes(value)) ? (!checkicon) ? : checkicon : null } {value} ) }else{ return( { let existing = selectedval?.indexOf(value) // console.log(existing); if(existing != -1 && existing != undefined){ let sv = [...selectedval]; sv.splice(existing,1) setSelectedVal(sv); setSelected((val: any) => { let temp = [...val]; temp.splice(existing,1) return temp; }); // onSelect() }else{ if(save === 'value'){ setSelected((val: any) => { let temp = [...new Set([...val,value])]; return temp; }) }else{ setSelected((val: any) => { let temp = [...new Set([...val,key])]; return temp; }) } setSelectedVal((val: any )=> { let temp = [...new Set([...val,value])]; return temp; }) // onSelect() } }}> { (selectedval?.includes(value)) ? (!checkicon) ? : checkicon : null } {value} ) } }) : customUserInput === true ? ( { const unfilteredData = data.map((item: any,index) => ( item.value )); const customInputData = [customUserInputData].concat(unfilteredData) const customInputDataFormatted = customInputData.map((item,index) => ({ key: index + 1, value: item })) console.log(customInputDataFormatted) setData(customInputDataFormatted) setDefaultOption((prev) => [...prev, customUserInputData]) setSelected([...selectedval, customUserInputData]) setSelectedVal([...selectedval, customUserInputData]) setCustomUserInputData(""); slideup() setTimeout(() => setFilteredData(customInputDataFormatted), 800) }}> add "{customUserInputData}" ) : ( { setSelected(undefined) setSelectedVal("") slideup() setTimeout(() => setFilteredData(data), 800) }}> {notFoundText} ) } {showSelected && (selectedval?.length > 0) ? Selected { selectedval?.map((item,index) => { return ( {item} ) }) } : null } : null } ) } export default MultipleSelectList; const styles = StyleSheet.create({ wrapper:{ borderWidth:1,borderRadius:10,borderColor:'gray',paddingHorizontal:20,paddingVertical:12,flexDirection:'row',justifyContent:'space-between',marginBottom:10 }, dropdown:{ borderWidth:1,borderRadius:10,borderColor:'gray',overflow:'hidden'}, option:{ paddingHorizontal:20,paddingVertical:8,flexDirection:'row',alignItems:'center'}, disabledoption:{ paddingHorizontal:20,paddingVertical:8,flexDirection:'row',alignItems:'center', backgroundColor:'whitesmoke'} })