import { FlashList } from '@shopify/flash-list'; import { forwardRef, RefAttributes, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { Dimensions, ScrollViewProps, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { NativeViewGestureHandlerProps, ScrollView } from 'react-native-gesture-handler'; import Animated, { FadeIn, FadeOut, useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'; import type { DrowdownProps, FCCWD } from '../../types'; import { defaultTheme } from '../../core/theme/theme'; import { defaultTypography } from '../../core/typography/typography'; import Button from '../Button/Button'; import FeatherIcon from '../Icons/Feather'; import IoniconsIcon from '../Icons/Ionicons'; import OcticonsIcon from '../Icons/Octicons'; const windowsHeight = Dimensions.get('window').height; let dataWithID: Array; // eslint-disable-next-line no-undef const GScrollView = forwardRef((props: JSX.IntrinsicAttributes & ScrollViewProps & NativeViewGestureHandlerProps & RefAttributes, ref) => ); const MultipleDropdown: FCCWD = ( { theme, typography, left, right, data, displayedRowValue, displayedButtonValue, listContainerStyle, defaultValue = [], displayLength = 4, buttonTitle, rowStyle, buttonStyle, buttonTextStyle, selectall = false, onSelect, onComplete, rowTextStyle, containerStyle, testID }, ) => { const [visible, setVisible] = useState(false); const [selectedObjects, setSelectedObjects] = useState(defaultValue || []); const [cord, setCord] = useState({ x: 0, y: 0, height: 0, width: 0 }); const openAnimation = useSharedValue(0); const dropdown = useRef(null); const dropdownAnimation = useAnimatedStyle(() => ({ transform: [{ rotate: `${openAnimation.value * 180}deg` }], }), []); const rightElement = useMemo(() => (typeof right === 'function' ? right(visible) : right), [visible, right]); const leftElement = useMemo(() => (typeof left === 'function' ? left(visible) : left), [visible, left]); const toggleCheckBox = (value: string | { [key: string]: any; }) => { const selectedObjectsTemp = [...selectedObjects]; const valueIndex = selectedObjectsTemp.indexOf(value); if (valueIndex > -1) { selectedObjectsTemp.splice(valueIndex, 1); } else { selectedObjectsTemp.push(value); } onSelect?.(selectedObjectsTemp); setSelectedObjects(selectedObjectsTemp); }; const isItemSelected = (item: string | { [key: string]: any; }) => { const result = selectedObjects?.find((x: any) => (x ? displayedButtonValue(x) === displayedButtonValue(item) : false)); return result; }; const isObjectSelected = !!selectedObjects?.length; useLayoutEffect(() => { dropdown?.current?.measure((x, y, width, height, pageX, pageY) => { setCord({ width, height, x: pageX, y: pageY }); }); }, [visible]); useEffect(() => { openAnimation.value = withSpring(visible ? 1 : 0); }, [visible]); useEffect(() => { // @ts-expect-error dataWithID = data?.map((x: (string | { [key: string]: any })): (string | { keyID: number, [key: string]: any }) => { x.keyID = Math.random(); return (x); }); }, []); return ( { setVisible(!visible); }} style={[Style.button, buttonStyle, { backgroundColor: visible ? theme?.primary5 : theme?.darkWhite }]} > {leftElement} {!isObjectSelected ? (buttonTitle || 'Please Select') : selectedObjects.length <= displayLength ? `${selectedObjects.map(item => displayedButtonValue(item))}` : `${selectedObjects?.length} Selected`} {rightElement || ( )} {visible && ( item.keyID || displayedRowValue(item)} estimatedItemSize={38} renderItem={({ item, index }:any) => { const isSelected = isItemSelected(item || {}); return ( { setSelectedObjects(item); toggleCheckBox(item); }} style={[ Style.row, { backgroundColor: theme?.darkWhite, }, index === data?.length || 0 - 1 ? { borderBottomLeftRadius: 5, borderBottomRightRadius: 5 } : null, rowStyle, ]} > {isSelected && ( )} {displayedRowValue(item)} ); }} /> {selectall && ( { data?.length === selectedObjects.length ? setSelectedObjects([]) : setSelectedObjects(data); }} style={{ flexDirection: 'row', justifyContent: 'flex-end' }} > Select All )}