import React, { useState, ReactNode, RefObject, useRef, useEffect, } from 'react'; import { View, StyleSheet, Pressable, ScrollView, Text, Animated, } from 'react-native'; import { colors } from '../colors'; import Label from './Label'; import Modal from './Modal'; interface Item { label: string; value: string | number; } interface Props { items: Array; onSelection: (item: Item) => void; selectedValue: string | number; pickerIcon?: ReactNode; iconWrapperStyle?: object | object[]; asterik?: boolean; labelStyle?: object | object[]; asterikStyle?: object | object[]; label?: string | number; labelWrapperStyle?: object | object[]; placeholder?: string; selectedValueStyle?: object | object[]; buttonStyle?: object | object[]; itemLabelStyle?: object | object[]; floatingLabel?: boolean; type?: 'dropdown' | 'modal'; } export default function Picker(props: Props) { const [showPicker, setShowPicker] = useState(false); const [position, setPosition] = useState({ x: 0, y: 0, width: 0, height: 0 }); const [animatedBottom, setAnimatedBottom] = useState(new Animated.Value(0)); const [shouldAnimate, setShouldAnimate] = useState(true); const pickerRef: RefObject = useRef() as RefObject; const handlePress = () => { if (props.floatingLabel && shouldAnimate) Animated.timing(animatedBottom, { toValue: position.height - 10, useNativeDriver: false, duration: 300, }).start(() => setShouldAnimate(false)); if (!showPicker) pickerRef.current?.measureInWindow( (x: number, y: number, width: number, height: number) => { setPosition({ x, y, width, height }); } ); setShowPicker(!showPicker); }; useEffect(() => { if (props.floatingLabel && props.selectedValue) Animated.timing(animatedBottom, { toValue: position.height - 10, useNativeDriver: false, duration: 300, }).start(() => setShouldAnimate(false)); }, [shouldAnimate, position]); return ( <> {props.label && !props.floatingLabel && (