import countries from 'countries-phone-masks'; import { CountryCode, getCountries, getCountryCallingCode, } from 'libphonenumber-js'; import { useRef, useState } from 'react'; import { useOnClickOutside } from 'usehooks-ts'; import { ChevronUpIcon } from './icons/ChevronUpIcon'; import { ChevronDownIcon } from './icons/ChevronDownIcon'; 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)); return (
{open && (
{getCountries() .map(country => [`+${getCountryCallingCode(country)}`, country]) .filter( ([phonecode, country]) => phonecode !== currentPhoneCode || country !== currentCountry, ) .map(([phoneCode, country]) => (
{ onClick([phoneCode, country as CountryCode]); setOpen(false); }} > c.iso === country)?.flag} alt="" className="sm:w-6 w-4" />

{phoneCode}

))}
)}
); }