Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite rendering in PhoneNumberFormField component. Handled err… #9341

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/components/Form/FormFields/PhoneNumberFormField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,36 @@ const PhoneNumberFormField = React.forwardRef<HTMLInputElement, Props>(
useEffect(() => {
if (field.value && field.value.length > 0) {
if (field.value.startsWith("1800")) {
setCountry({ flag: "📞", name: "Support", code: "1800" });
setCountry((prev) =>
prev.code === "1800"
? prev
: { flag: "📞", name: "Support", code: "1800" },
);
return;
}
if (field.value === "+") {
setCountry({ flag: "🌍", name: "Other", code: "+" });
setCountry((prev) =>
prev.code === "+" ? prev : { flag: "🌍", name: "Other", code: "+" },
);
return;
}
setCountry(phoneCodes[getCountryCode(field.value)!]);
const countryCode = getCountryCode(field.value);
const newCountry = countryCode ? phoneCodes[countryCode] : null;

// Allow users to continue editing even with undefined country
if (newCountry) {
setCountry((prev) =>
prev.code === newCountry.code ? prev : newCountry,
);
} else {
// Reset to default when no matching country code is found
setCountry({ flag: "❓", name: "Unknown", code: "" });
}
} else {
// Handle empty or removed country code
setCountry({ flag: "🌍", name: "Other", code: "" });
}
}, [setValue]);
}, [field.value]);

return (
<FormField
Expand Down
Loading