diff --git a/packages/features/components/phone-input/PhoneInput.tsx b/packages/features/components/phone-input/PhoneInput.tsx index 070eea48b2..644ef67cb8 100644 --- a/packages/features/components/phone-input/PhoneInput.tsx +++ b/packages/features/components/phone-input/PhoneInput.tsx @@ -31,6 +31,22 @@ function BasePhoneInput({ }: 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); + } + }, []); + if (!isPlatform) { return ( @@ -45,12 +61,12 @@ function BasePhoneInput({ disableSearchIcon country={defaultCountry} inputProps={{ - name: name, + name, required: rest.required, placeholder: rest.placeholder, }} - onChange={(value) => { - onChange(`+${value}`); + 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", @@ -84,6 +100,7 @@ function BasePhoneInputWeb({ ...rest }: Omit) { const defaultCountry = useDefaultCountry(); + return ( { - onChange(`+${value}`); + 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",