Skip to content

Commit 882eb85

Browse files
Vibgitcode27kart1kaanikdhabal
authored
fix(phone input): trigger validation and formatting for prefill query… (calcom#23123)
* fix(phone input): trigger validation and formatting for prefill query values * fix: strengthen phone input sanitization to prevent edge cases --------- Co-authored-by: Kartik Saini <41051387+kart1ka@users.noreply.github.com> Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com>
1 parent 6b0726b commit 882eb85

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

packages/features/components/phone-input/PhoneInput.tsx

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@ function BasePhoneInput({
3131
}: PhoneInputProps) {
3232
const isPlatform = useIsPlatform();
3333

34+
// This is to trigger validation on prefill value changes
35+
useEffect(() => {
36+
if (!value) return;
37+
38+
const sanitized = value
39+
.trim()
40+
.replace(/[^\d+]/g, "")
41+
.replace(/^\+?/, "+");
42+
43+
if (sanitized === "+" || sanitized === "") return;
44+
45+
if (value !== sanitized) {
46+
onChange(sanitized);
47+
}
48+
}, []);
49+
3450
if (!isPlatform) {
3551
return (
3652
<BasePhoneInputWeb name={name} className={className} onChange={onChange} value={value} {...rest} />
@@ -45,12 +61,12 @@ function BasePhoneInput({
4561
disableSearchIcon
4662
country={defaultCountry}
4763
inputProps={{
48-
name: name,
64+
name,
4965
required: rest.required,
5066
placeholder: rest.placeholder,
5167
}}
52-
onChange={(value) => {
53-
onChange(`+${value}`);
68+
onChange={(val: string) => {
69+
onChange(`+${val}`);
5470
}}
5571
containerClass={classNames(
5672
"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({
84100
...rest
85101
}: Omit<PhoneInputProps, "defaultCountry">) {
86102
const defaultCountry = useDefaultCountry();
103+
87104
return (
88105
<PhoneInput
89106
{...rest}
@@ -92,12 +109,12 @@ function BasePhoneInputWeb({
92109
enableSearch
93110
disableSearchIcon
94111
inputProps={{
95-
name: name,
112+
name,
96113
required: rest.required,
97114
placeholder: rest.placeholder,
98115
}}
99-
onChange={(value) => {
100-
onChange(`+${value}`);
116+
onChange={(val: string) => {
117+
onChange(`+${val}`);
101118
}}
102119
containerClass={classNames(
103120
"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",

0 commit comments

Comments
 (0)