🐛 Avoid country code reset on phone input emptying (#2000)

Fixed #1999 Prevent selected country code from resetting to INT when
input is cleared

- Removed the block that reset **selectedCountryCode** to **INT** when
the input field was cleared.

- This ensures that if a country is selected, clearing the input does
not revert it to **INT**.

---------

Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
This commit is contained in:
Om Pathak 2025-02-08 16:57:07 +05:30 committed by GitHub
parent 9738b4d2c0
commit f20a2ed9c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 23 additions and 29 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.3.47",
"version": "0.3.48",
"description": "Javascript library to display typebots on your website",
"license": "FSL-1.1-ALv2",
"type": "module",

View File

@ -27,33 +27,27 @@ export const PhoneInput = (props: PhoneInputProps) => {
const handleInput = (inputValue: string | undefined) => {
setInputValue(inputValue as string);
if (
(inputValue === "" || inputValue === "+") &&
selectedCountryCode() !== "INT"
)
setSelectedCountryCode("INT");
const matchedCountry =
inputValue?.startsWith("+") &&
inputValue.length > 2 &&
phoneCountries.reduce<(typeof phoneCountries)[number] | null>(
(matchedCountry, country) => {
if (
!country?.dial_code ||
(matchedCountry !== null && !matchedCountry.dial_code)
) {
return matchedCountry;
}
if (
inputValue?.startsWith(country.dial_code) &&
country.dial_code.length > (matchedCountry?.dial_code.length ?? 0)
) {
return country;
}
if (inputValue?.startsWith("+") && inputValue.length > 2) {
const matchedCountry = phoneCountries.reduce<
(typeof phoneCountries)[number] | null
>((matchedCountry, country) => {
if (
!country?.dial_code ||
(matchedCountry !== null && !matchedCountry.dial_code)
) {
return matchedCountry;
},
null,
);
if (matchedCountry) setSelectedCountryCode(matchedCountry.code);
}
if (
inputValue?.startsWith(country.dial_code) &&
country.dial_code.length > (matchedCountry?.dial_code.length ?? 0)
) {
return country;
}
return matchedCountry;
}, null);
if (matchedCountry) setSelectedCountryCode(matchedCountry.code);
}
};
const checkIfInputIsValid = () =>

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/nextjs",
"version": "0.3.47",
"version": "0.3.48",
"license": "FSL-1.1-ALv2",
"description": "Convenient library to display typebots on your Next.js website",
"type": "module",

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.3.47",
"version": "0.3.48",
"description": "Convenient library to display typebots on your React app",
"license": "FSL-1.1-ALv2",
"type": "module",