import React, { useState } from 'react'; import { View, Text, TouchableOpacity, TextInput, FlatList, } from 'react-native'; import ChevronDownIcon from '../icons/chevron-down.svg'; import ChevronUpIcon from '../icons/chevron-up.svg'; import InformationGray from '../icons/information-gray.svg'; import InformationError from '../icons/information-error.svg'; import { classNames } from '../utils'; import { RadioButton } from './RadioButton'; export enum PhoneNumberStatusEnum { DEFAULT = 'DEFAULT', ERROR = 'ERROR', } export interface CountryCodeOption { label: string; value: string; } export interface PhoneNumberInputProps { status?: PhoneNumberStatusEnum; label?: string; caption?: string; disabled?: boolean; phoneNumber: string; countryOptions: CountryCodeOption[]; selectedCountry: CountryCodeOption; onFocus?: () => void; onBlur?: () => void; onCountryChange: (countryCode: CountryCodeOption) => void; onPhoneNumberChange: (phoneNumber: string) => void; placeholder?: string; className?: string; } export const PhoneNumberInput: React.FC = ({ status = PhoneNumberStatusEnum.DEFAULT, label, caption, disabled = false, phoneNumber, countryOptions, selectedCountry = countryOptions[0], onCountryChange, onPhoneNumberChange, onFocus, onBlur, placeholder, className, ...restOfProps }) => { const [dropdownVisible, setDropdownVisible] = useState(false); const [isFocused, setIsFocused] = useState(false); const handleSelect = (value: string | number) => { onCountryChange?.( countryOptions.find((option) => option.value === value) || countryOptions[0] ); setDropdownVisible(false); }; const handleOnFocus = () => { setIsFocused(true); onFocus?.(); }; const handleOnBlur = () => { setIsFocused(false); onBlur?.(); }; const handlePhoneNumberInputChange = (input: string) => { onPhoneNumberChange?.(input); }; const getBorderColor = () => { if (disabled) { return 'border-neutral-500'; } if (dropdownVisible || isFocused) { return 'border-brand-500'; } if (status === PhoneNumberStatusEnum.ERROR) { return 'border-error-500'; } return 'border-neutral-500'; }; return ( {label && ( {label} )} !disabled ? setDropdownVisible(!dropdownVisible) : undefined } > {selectedCountry?.value} {dropdownVisible ? : } {dropdownVisible && ( String(item.label)} renderItem={({ item, index }) => ( handleSelect(item.value)} className={classNames( 'flex p-3 rounded-lg flex-row items-center w-full', `${ selectedCountry?.value === item?.value ? 'bg-brand-50' : '' }`, `${index !== countryOptions?.length - 1 ? 'mb-2' : ''}` )} > handleSelect(item?.value)} /> {item?.label} )} /> )} {caption && ( {status === PhoneNumberStatusEnum.ERROR ? ( ) : ( )} {caption} )} ); };