/* eslint-disable @next/next/no-img-element */ import countries from "countries-phone-masks"; import { CountryCode, getCountries, getCountryCallingCode, } from "libphonenumber-js"; import { SyntheticEvent, useRef, useState } from "react"; import { useOnClickOutside } from "usehooks-ts"; import ChevronDownIcon from "@/components/icons/ChevronDownIcon"; import ChevronUpIcon from "@/components/icons/ChevronUpIcon"; export function SelectPhoneCode({ currentPhoneCodeAndCountry: [currentPhoneCode, currentCountry], onClick, }: { currentPhoneCodeAndCountry: [string, CountryCode]; onClick: ([phoneCode, country]: [string, CountryCode]) => void; }) { const [open, setOpen] = useState(false); const ref = useRef(null); useOnClickOutside(ref, () => setOpen(false)); function handleExpand(e: SyntheticEvent) { e.preventDefault(); setOpen((prev) => !prev); } function pickPhoneCode(phoneCode: string, country: CountryCode) { onClick([phoneCode, country as CountryCode]); setOpen(false); } return (
{open && (
{getCountries() .map((country) => [`+${getCountryCallingCode(country)}`, country]) .filter( ([phonecode, country]) => phonecode !== currentPhoneCode || country !== currentCountry ) .map(([phoneCode, country]) => (
pickPhoneCode(phoneCode, country as CountryCode)} > c.iso === country)?.flag || ""} alt={country} className="!relative w-4 sm:w-6" width={16} height={8} />

{phoneCode}

))}
)}
); }