import { isSupportedCountry } from "libphonenumber-js";
import { useEffect, useState } from "react";
import BasePhoneInput from "react-phone-number-input";
import type { Props, Country } from "react-phone-number-input";
import "react-phone-number-input/style.css";
export type PhoneInputProps = Props<{
value: string;
id?: string;
placeholder?: string;
required?: boolean;
className?: string;
name?: string;
}>;
function PhoneInput({ name, className = "", onChange, ...rest }: PhoneInputProps) {
const defaultCountry = useDefaultCountry();
return (
);
}
const useDefaultCountry = () => {
const [defaultCountry, setDefaultCountry] = useState("US");
useEffect(() => {
fetch("/api/countrycode")
.then((res) => res.json())
.then((res) => {
if (isSupportedCountry(res.countryCode)) setDefaultCountry(res.countryCode);
})
.catch((err) => {
console.log(err);
});
}, []);
return defaultCountry;
};
export default PhoneInput;