2
0

🚸 (js) Improve phone number parsing

This commit is contained in:
Baptiste Arnaud
2023-03-03 10:50:39 +01:00
parent 04028e74d9
commit f1a9a1ce8b
6 changed files with 32 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@typebot.io/js",
"version": "0.0.19",
"version": "0.0.20",
"description": "Javascript library to display typebots on your website",
"type": "module",
"main": "dist/index.js",

View File

@ -25,11 +25,32 @@ export const PhoneInput = (props: PhoneInputProps) => {
const handleInput = (inputValue: string | undefined) => {
setInputValue(inputValue as string)
const matchedCountry = phoneCountries.find(
(country) =>
country.dial_code === inputValue &&
country.code !== selectedCountryCode()
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
}
return matchedCountry
},
null
)
if (matchedCountry) setSelectedCountryCode(matchedCountry.code)
}