import {countries, TCountries} from "countries-list"; import {Fragment, useState} from "react"; import {Combobox, Transition} from "@headlessui/react"; import {BsChevronExpand} from "react-icons/bs"; import countryCodes from "country-codes-list"; interface Props { value?: string; onChange?: (value: string) => void; disabled?: boolean; } const mapCountries = (codes: string[]) => { return codes.map((code) => ({ label: `${countryCodes.findOne("countryCode" as any, code).flag} ${countries[code as unknown as keyof TCountries].name} (+${ countries[code as unknown as keyof TCountries].phone })`, code, })); }; export default function CountrySelect({value, disabled = false, onChange}: Props) { const [query, setQuery] = useState(""); const filteredCountries = query === "" ? mapCountries(Object.keys(countries)) : mapCountries( Object.keys(countries).filter((x) => countries[x as unknown as keyof TCountries].name.toLowerCase().includes(query.toLowerCase()), ), ); return ( <>
setQuery(e.target.value)} displayValue={(code: string) => { const country = countries[code as unknown as keyof TCountries]; return `${countryCodes.findOne("countryCode" as any, code).flag} ${country.name} (+${country.phone})`; }} />
setQuery("")}> {filteredCountries.length === 0 && query !== "" ? (
Nothing found.
) : ( filteredCountries.map((country) => ( `relative cursor-default select-none py-2 pl-10 pr-4 ${ active ? "bg-mti-purple-light text-white" : "text-gray-900" }` }> {country.label} )) )}
); }