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

@@ -6,8 +6,8 @@
"types": "./src/index.ts",
"scripts": {
"preview": "concurrently \"pnpm run watch\" \"sleep 5 && pnpm run serve\" -n \"watch,serve\" -c \"bgBlue.bold,bgMagenta.bold\"",
"watch": "tsx watch ./preview.tsx --clear-screen=false",
"serve": "http-server dist -a localhost -p 3223 -o",
"watch": "tsx watch ./src/preview.tsx --clear-screen=false",
"serve": "http-server dist -a localhost -p 3223 -o -c-1",
"lint": "eslint \"src/**/*.ts*\""
},
"keywords": [],

View File

@@ -1,4 +1,3 @@
import React from 'react'
import { Head, Text, Button } from '../components'
import {
Mjml,
@@ -25,12 +24,11 @@ export const DefaultBotNotificationEmail = ({
<MjmlBody width={600}>
<MjmlSection padding="32px" cssClass="smooth" border="1px solid #e2e8f0">
<MjmlColumn>
<Text padding="0">Your typebot has collected a new response! 🥳</Text>
{Object.keys(answers).map((key) => {
{Object.keys(answers).map((key, index) => {
const isEmail = emailRegex.test(answers[key])
return (
<Text key={key}>
<Text key={key} paddingTop={index === 0 ? 0 : undefined}>
<b>{key}</b>:{' '}
{isEmail ? (
<a href={`mailto:${answers[key]}`}>{answers[key]}</a>

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)
}

View File

@@ -1,6 +1,6 @@
{
"name": "@typebot.io/react",
"version": "0.0.19",
"version": "0.0.20",
"description": "React library to display typebots on your website",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@@ -1481,4 +1481,4 @@ export const phoneCountries = [
code: 'ZW',
dial_code: '+263',
},
]
] as const