/* eslint-disable no-unsafe-optional-chaining */ import React, { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'; import { Dimensions, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import Animated, { FadeIn, FadeOut, useAnimatedStyle, useSharedValue, withSpring } from 'react-native-reanimated'; import { ScrollView } from 'react-native-gesture-handler'; import type { DrowdownProps, FCCWD } from '../../types'; import FeatherIcon from '../Icons/Feather'; const windowsHeight = Dimensions.get('window').height; const Dropdown: FCCWD = ( { left, right, theme, typography, data, displayedRowValue, displayedButtonValue, listContainerStyle, defaultValue = {}, buttonTitle, rowStyle, buttonStyle, disabled, buttonTextStyle, onSelect, rowTextStyle, containerStyle, iconStyle, autoPosition = true, testID, buttonBackgrounColor = { focusBackground: theme?.primary5, defaultBackground: theme?.darkWhite } }, ) => { const [visible, setVisible] = useState(false); const [selectedObject, setSelectedObject] = useState(defaultValue); const [cord, setCord] = useState({ x: 0, y: 0, height: 0, width: 0 }); const openAnimation = useSharedValue(0); const dropdown = useRef(null); const rightElement = useMemo(() => (typeof right === 'function' ? right(visible) : right), [visible, right]); const leftElement = useMemo(() => (typeof left === 'function' ? left(visible) : left), [visible, left]); const dropdownAnimation = useAnimatedStyle(() => ({ transform: [{ rotate: `${openAnimation.value * 180}deg` }], }), []); const isObjectSelected = (Object.keys(selectedObject).length === 0); const isSelectedObject = (value: any) => displayedButtonValue(selectedObject) === displayedButtonValue(value); 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]); return ( setCord(event.nativeEvent.layout)} onPress={() => { setVisible(!visible); }} style={[Style.button, buttonStyle, { backgroundColor: visible ? buttonBackgrounColor.focusBackground : buttonBackgrounColor.defaultBackground }]} > {leftElement} {isObjectSelected ? (buttonTitle || 'Please Select') : displayedButtonValue(selectedObject)} {rightElement || ( )} {visible && cord.x >= 0 && cord.y >= 0 && ( {data?.map((value, index) => ( { setSelectedObject(value); setVisible(false); onSelect?.(value); }} style={[ Style.row, { backgroundColor: isSelectedObject(value) ? theme?.primary15 : theme?.darkWhite, }, index === data.length - 1 ? { borderBottomLeftRadius: 5, borderBottomRightRadius: 5 } : null, rowStyle, ]} > {displayedRowValue(value)} ))} )} ); }; export default Dropdown; export const Style = StyleSheet.create({ button: { height: 38, width: '100%', flexDirection: 'row', alignItems: 'center', borderRadius: 5, }, listContainer: { zIndex: 100, padding: 10, height: 36 * 4.5, width: '100%', position: 'absolute', borderTopWidth: 0, shadowColor: '#000', shadowOffset: { width: 0, height: 2, }, shadowOpacity: 0.37, elevation: 4, borderRadius: 5, }, row: { height: 38, borderRadius: 3, borderWidth: 0, justifyContent: 'center', }, rightIcon: { borderRadius: 3, paddingVertical: 2, paddingHorizontal: 6, marginRight: 12, }, });