2023-02-25 19:15:05 +00:00
|
|
|
import { isSupportedCountry } from "libphonenumber-js";
|
2023-03-08 12:33:42 +00:00
|
|
|
import { useState } from "react";
|
2023-03-02 18:15:28 +00:00
|
|
|
import BasePhoneInput from "react-phone-number-input";
|
|
|
|
import type { Props, Country } from "react-phone-number-input";
|
2021-09-22 19:52:38 +00:00
|
|
|
import "react-phone-number-input/style.css";
|
|
|
|
|
2023-04-10 16:12:15 +00:00
|
|
|
import { classNames } from "@calcom/lib";
|
2023-03-08 12:33:42 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
export type PhoneInputProps = Props<{
|
|
|
|
value: string;
|
|
|
|
id?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
required?: boolean;
|
|
|
|
className?: string;
|
|
|
|
name?: string;
|
|
|
|
}>;
|
2022-03-03 09:57:59 +00:00
|
|
|
|
2023-03-02 18:15:28 +00:00
|
|
|
function PhoneInput({ name, className = "", onChange, ...rest }: PhoneInputProps) {
|
2023-02-25 19:15:05 +00:00
|
|
|
const defaultCountry = useDefaultCountry();
|
2023-03-02 18:15:28 +00:00
|
|
|
|
2022-04-06 12:37:06 +00:00
|
|
|
return (
|
|
|
|
<BasePhoneInput
|
|
|
|
{...rest}
|
2022-07-26 01:37:32 +00:00
|
|
|
international
|
2023-02-25 19:15:05 +00:00
|
|
|
defaultCountry={defaultCountry}
|
2022-04-06 12:37:06 +00:00
|
|
|
name={name}
|
2022-12-15 21:54:40 +00:00
|
|
|
onChange={onChange}
|
2023-04-05 18:14:46 +00:00
|
|
|
countrySelectProps={{ className: "text-emphasis" }}
|
2022-09-02 21:16:36 +00:00
|
|
|
numberInputProps={{
|
2023-04-10 16:12:15 +00:00
|
|
|
className: "border-0 text-sm focus:ring-0 bg-default text-default",
|
2022-09-02 21:16:36 +00:00
|
|
|
}}
|
2023-04-10 16:12:15 +00:00
|
|
|
className={classNames(
|
|
|
|
"hover:border-emphasis border-default bg-default rounded-md border py-px pl-3 focus-within:border-neutral-300 focus-within:outline-none focus-within:ring-2 focus-within:ring-neutral-800 focus-within:ring-offset-1 disabled:cursor-not-allowed",
|
|
|
|
className
|
|
|
|
)}
|
2022-04-06 12:37:06 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2021-08-25 10:18:37 +00:00
|
|
|
|
2023-02-25 19:15:05 +00:00
|
|
|
const useDefaultCountry = () => {
|
2023-03-02 18:15:28 +00:00
|
|
|
const [defaultCountry, setDefaultCountry] = useState<Country>("US");
|
2023-03-09 10:38:57 +00:00
|
|
|
trpc.viewer.public.countryCode.useQuery(undefined, {
|
|
|
|
refetchOnWindowFocus: false,
|
|
|
|
refetchOnReconnect: false,
|
|
|
|
retry: false,
|
2023-03-08 12:33:42 +00:00
|
|
|
onSuccess: (data) => {
|
|
|
|
if (isSupportedCountry(data?.countryCode)) {
|
|
|
|
setDefaultCountry(data.countryCode as Country);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2023-02-25 19:15:05 +00:00
|
|
|
|
|
|
|
return defaultCountry;
|
|
|
|
};
|
|
|
|
|
2021-08-25 10:18:37 +00:00
|
|
|
export default PhoneInput;
|