Files
calendar/packages/features/components/phone-input/PhoneInput.tsx
T
1d25265c56 feat: workflows v3 UI (#22772)
* early UI refresher of workflows using v3 designs. needs tons of testing, might have broken

* Delete .claude/settings.local.json

* fix: bunch of design fixes, merge conflict fixes

* fix: e2e and type-check

* fix: await button click

* review fixes

* code rabbit fixes

* fix styles in variables dropdown

* fix text truncate

* fix: unit tests

* fix: unit tests

* chore: add missing i18n

* review fixes

* review fixes

* fix testing info message

* move timeSectionText up

* fix which team does this apply to info message

* disable set up agent button with read only

* fix: cal.ai breaking on mobile screen

---------

Co-authored-by: Amit Sharma <74371312+Amit91848@users.noreply.github.com>
Co-authored-by: Udit Takkar <udit222001@gmail.com>
Co-authored-by: CarinaWolli <wollencarina@gmail.com>
2025-09-11 17:01:42 +05:30

176 lines
4.8 KiB
TypeScript

"use client";
import { isSupportedCountry } from "libphonenumber-js";
import type { CSSProperties } from "react";
import { useState, useEffect } from "react";
import PhoneInput from "react-phone-input-2";
import "react-phone-input-2/lib/style.css";
import { useIsPlatform } from "@calcom/atoms/hooks/useIsPlatform";
import { trpc } from "@calcom/trpc/react";
import classNames from "@calcom/ui/classNames";
export type PhoneInputProps = {
value?: string;
id?: string;
placeholder?: string;
required?: boolean;
className?: string;
name?: string;
disabled?: boolean;
onChange: (value: string) => void;
defaultCountry?: string;
inputStyle?: CSSProperties;
flagButtonStyle?: CSSProperties;
};
function BasePhoneInput({
name,
className = "",
onChange,
value,
defaultCountry = "us",
...rest
}: PhoneInputProps) {
const isPlatform = useIsPlatform();
// This is to trigger validation on prefill value changes
useEffect(() => {
if (!value) return;
const sanitized = value
.trim()
.replace(/[^\d+]/g, "")
.replace(/^\+?/, "+");
if (sanitized === "+" || sanitized === "") return;
if (value !== sanitized) {
onChange(sanitized);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (!isPlatform) {
return (
<BasePhoneInputWeb name={name} className={className} onChange={onChange} value={value} {...rest} />
);
}
return (
<PhoneInput
{...rest}
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
enableSearch
disableSearchIcon
country={defaultCountry}
inputProps={{
name,
required: rest.required,
placeholder: rest.placeholder,
}}
onChange={(val: string) => {
onChange(`+${val}`);
}}
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",
className
)}
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
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" }}
/>
);
}
function BasePhoneInputWeb({
name,
className = "",
onChange,
value,
inputStyle,
flagButtonStyle,
...rest
}: Omit<PhoneInputProps, "defaultCountry">) {
const defaultCountry = useDefaultCountry();
return (
<PhoneInput
{...rest}
value={value ? value.trim().replace(/^\+?/, "+") : undefined}
country={value ? undefined : defaultCountry}
enableSearch
disableSearchIcon
inputProps={{
name,
required: rest.required,
placeholder: rest.placeholder,
}}
onChange={(val: string) => {
onChange(`+${val}`);
}}
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",
className
)}
inputClass="text-sm focus:ring-0 !bg-default text-default placeholder:text-muted"
buttonClass="text-emphasis !bg-default hover:!bg-emphasis"
buttonStyle={{ ...flagButtonStyle }}
searchClass="!text-default !bg-default hover:!bg-emphasis"
dropdownClass="!text-default !bg-default"
inputStyle={{ width: "inherit", border: 0, ...inputStyle }}
searchStyle={{
display: "flex",
flexDirection: "row",
alignItems: "center",
padding: "6px 12px",
gap: "8px",
width: "296px",
height: "28px",
marginLeft: "-4px",
}}
dropdownStyle={{ width: "max-content" }}
/>
);
}
const useDefaultCountry = () => {
const [defaultCountry, setDefaultCountry] = useState("us");
const query = trpc.viewer.public.countryCode.useQuery(undefined, {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
retry: false,
});
useEffect(
function refactorMeWithoutEffect() {
const data = query.data;
if (!data?.countryCode) {
return;
}
isSupportedCountry(data?.countryCode)
? setDefaultCountry(data.countryCode.toLowerCase())
: setDefaultCountry(navigator.language.split("-")[1]?.toLowerCase() || "us");
},
[query.data]
);
return defaultCountry;
};
export default BasePhoneInput;