2023-02-25 19:15:05 +00:00
|
|
|
import { isSupportedCountry } from "libphonenumber-js";
|
2023-07-01 17:42:18 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2023-05-30 15:47:22 +00:00
|
|
|
import PhoneInput from "react-phone-input-2";
|
|
|
|
import "react-phone-input-2/lib/style.css";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
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-05-30 15:47:22 +00:00
|
|
|
export type PhoneInputProps = {
|
|
|
|
value?: string;
|
2023-03-02 18:15:28 +00:00
|
|
|
id?: string;
|
|
|
|
placeholder?: string;
|
|
|
|
required?: boolean;
|
|
|
|
className?: string;
|
|
|
|
name?: string;
|
2023-05-30 15:47:22 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
};
|
2022-03-03 09:57:59 +00:00
|
|
|
|
2023-05-30 15:47:22 +00:00
|
|
|
function BasePhoneInput({ name, className = "", onChange, ...rest }: PhoneInputProps) {
|
2023-07-01 17:42:18 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (!rest.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const formattedValue = rest.value.trim().replace(/^\+?/, "+");
|
|
|
|
onChange(formattedValue);
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
2023-02-25 19:15:05 +00:00
|
|
|
const defaultCountry = useDefaultCountry();
|
2022-04-06 12:37:06 +00:00
|
|
|
return (
|
2023-05-30 15:47:22 +00:00
|
|
|
<PhoneInput
|
2022-04-06 12:37:06 +00:00
|
|
|
{...rest}
|
2023-07-01 17:42:18 +00:00
|
|
|
country={rest.value ? undefined : defaultCountry}
|
2023-05-30 15:47:22 +00:00
|
|
|
enableSearch
|
|
|
|
disableSearchIcon
|
|
|
|
inputProps={{
|
|
|
|
name: name,
|
|
|
|
required: rest.required,
|
|
|
|
placeholder: rest.placeholder,
|
2022-09-02 21:16:36 +00:00
|
|
|
}}
|
2023-06-01 09:47:18 +00:00
|
|
|
onChange={(value) => {
|
|
|
|
onChange("+" + value);
|
|
|
|
}}
|
2023-05-30 15:47:22 +00:00
|
|
|
containerClass={classNames(
|
|
|
|
"hover:border-emphasis dark:focus:border-emphasis border-default !bg-default rounded-md border focus-within:outline-none focus-within:ring-2 focus-within:ring-brand-default disabled:cursor-not-allowed",
|
2023-04-10 16:12:15 +00:00
|
|
|
className
|
|
|
|
)}
|
2023-05-30 15:47:22 +00:00
|
|
|
inputClass="text-sm focus:ring-0 !bg-default text-default"
|
|
|
|
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
|
|
|
|
searchClass="!text-default !bg-default hover:!bg-emphasis"
|
|
|
|
dropdownClass="!text-default !bg-default"
|
|
|
|
inputStyle={{ width: "inherit", border: 0 }}
|
|
|
|
searchStyle={{
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "row",
|
|
|
|
alignItems: "center",
|
|
|
|
padding: "6px 12px",
|
|
|
|
gap: "8px",
|
|
|
|
width: "296px",
|
|
|
|
height: "28px",
|
|
|
|
marginLeft: "-4px",
|
|
|
|
}}
|
|
|
|
dropdownStyle={{ width: "max-content" }}
|
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-05-30 15:47:22 +00:00
|
|
|
const [defaultCountry, setDefaultCountry] = useState("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) => {
|
2023-06-22 08:18:05 +00:00
|
|
|
isSupportedCountry(data?.countryCode)
|
|
|
|
? setDefaultCountry(data.countryCode.toLowerCase())
|
|
|
|
: setDefaultCountry(navigator.language.split("-")[1]?.toLowerCase() || "us");
|
2023-03-08 12:33:42 +00:00
|
|
|
},
|
|
|
|
});
|
2023-02-25 19:15:05 +00:00
|
|
|
|
|
|
|
return defaultCountry;
|
|
|
|
};
|
|
|
|
|
2023-05-30 15:47:22 +00:00
|
|
|
export default BasePhoneInput;
|