import { ShortTextInput } from '@/components' import { ChevronDownIcon } from '@/components/icons/ChevronDownIcon' import { SendButton } from '@/components/SendButton' import { InputSubmitContent } from '@/types' import { isMobile } from '@/utils/isMobileSignal' import { createSignal, For, onCleanup, onMount } from 'solid-js' import { isEmpty } from '@indite.io/lib' import { phoneCountries } from '@indite.io/lib/phoneCountries' import { CommandData } from '@/features/commands/types' import { PhoneNumberInputBlock } from '@indite.io/schemas' import { defaultPhoneInputOptions } from '@indite.io/schemas/features/blocks/inputs/phone/constants' type PhoneInputProps = Pick< NonNullable, 'labels' | 'defaultCountryCode' > & { defaultValue?: string onSubmit: (value: InputSubmitContent) => void } export const PhoneInput = (props: PhoneInputProps) => { const [selectedCountryCode, setSelectedCountryCode] = createSignal( isEmpty(props.defaultCountryCode) ? 'INT' : props.defaultCountryCode ) const [inputValue, setInputValue] = createSignal(props.defaultValue ?? '') let inputRef: HTMLInputElement | undefined 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 } return matchedCountry }, null ) if (matchedCountry) setSelectedCountryCode(matchedCountry.code) } const checkIfInputIsValid = () => inputRef?.value !== '' && inputRef?.reportValidity() const submit = () => { const selectedCountryDialCode = phoneCountries.find( (country) => country.code === selectedCountryCode() )?.dial_code if (checkIfInputIsValid()) { const val = inputRef?.value ?? inputValue() props.onSubmit({ type: 'text', value: val.startsWith('+') ? val : `${selectedCountryDialCode ?? ''}${val}`, }) } else inputRef?.focus() setInputValue('') } const submitWhenEnter = (e: KeyboardEvent) => { if (e.key === 'Enter') submit() } const selectNewCountryCode = ( event: Event & { currentTarget: { value: string } } ) => { const code = event.currentTarget.value setSelectedCountryCode(code) const dial_code = phoneCountries.find( (country) => country.code === code )?.dial_code if (inputValue() === '' && dial_code) setInputValue(dial_code) inputRef?.focus() } onMount(() => { if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true }) window.addEventListener('message', processIncomingEvent) }) onCleanup(() => { window.removeEventListener('message', processIncomingEvent) }) const processIncomingEvent = (event: MessageEvent) => { const { data } = event if (!data.isFromBot) return if (data.command === 'setInputValue') setInputValue(data.value) } return (
{ phoneCountries.find( (country) => selectedCountryCode() === country.code )?.flag }
{props.labels?.button}
) }