From f20a2ed9c30c16c8bceb4ced018ec1ade11ef856 Mon Sep 17 00:00:00 2001 From: Om Pathak Date: Sat, 8 Feb 2025 16:57:07 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Avoid=20country=20code=20reset?= =?UTF-8?q?=20on=20phone=20input=20emptying=20(#2000)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/embeds/js/package.json | 2 +- .../inputs/phone/components/PhoneInput.tsx | 46 ++++++++----------- packages/embeds/nextjs/package.json | 2 +- packages/embeds/react/package.json | 2 +- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/packages/embeds/js/package.json b/packages/embeds/js/package.json index db6b73a99..a5a4676c5 100644 --- a/packages/embeds/js/package.json +++ b/packages/embeds/js/package.json @@ -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", diff --git a/packages/embeds/js/src/features/blocks/inputs/phone/components/PhoneInput.tsx b/packages/embeds/js/src/features/blocks/inputs/phone/components/PhoneInput.tsx index 53ecead48..1b9d9f235 100644 --- a/packages/embeds/js/src/features/blocks/inputs/phone/components/PhoneInput.tsx +++ b/packages/embeds/js/src/features/blocks/inputs/phone/components/PhoneInput.tsx @@ -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 = () => diff --git a/packages/embeds/nextjs/package.json b/packages/embeds/nextjs/package.json index 3bc0e6136..6f2b1b48e 100644 --- a/packages/embeds/nextjs/package.json +++ b/packages/embeds/nextjs/package.json @@ -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", diff --git a/packages/embeds/react/package.json b/packages/embeds/react/package.json index a6a54d4ca..25b36c360 100644 --- a/packages/embeds/react/package.json +++ b/packages/embeds/react/package.json @@ -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",