import { useEffect, useRef, useState } from "react"; import { AnimatePresence, motion } from "framer-motion"; interface Country { code: string; iso: string; label: string; } const COUNTRIES: Country[] = [ { code: "+351", iso: "pt", label: "Portugal" }, { code: "+34", iso: "es", label: "Spain" }, { code: "+33", iso: "fr", label: "France" }, { code: "+44", iso: "gb", label: "United Kingdom" }, { code: "+1", iso: "us", label: "United States" }, { code: "+55", iso: "br", label: "Brazil" }, ]; const flagUrl = (iso: string) => `https://flagcdn.com/${iso}.svg`; interface Props { value: string; onChange: (code: string) => void; } const PhoneCodeSelect = ({ value, onChange }: Props) => { const [open, setOpen] = useState(false); const containerRef = useRef(null); const active = COUNTRIES.find((c) => c.code === value) ?? COUNTRIES[0]; useEffect(() => { if (!open) return; const handlePointerDown = (e: MouseEvent) => { if ( containerRef.current && !containerRef.current.contains(e.target as Node) ) { setOpen(false); } }; document.addEventListener("mousedown", handlePointerDown); return () => document.removeEventListener("mousedown", handlePointerDown); }, [open]); return (
setOpen((o) => !o)} whileTap={{ scale: 0.95 }} transition={{ type: "spring", stiffness: 500, damping: 30 }} aria-label="Country code" > {active.label} {active.code} {open && ( {COUNTRIES.map((country) => ( ))} )}
); }; export default PhoneCodeSelect;