import React, {useState} from 'react'; import { InputAdornment, MenuItem, Select, TextField, Typography, } from '@mui/material'; import { defaultCountries, FlagImage, getActiveFormattingMask, parseCountry, usePhoneInput, } from 'react-international-phone'; import 'react-international-phone/style.css'; interface Props { value: string; onChange: (phoneNumber: string) => void; label: string; } const PhoneInput = ({value, onChange, label}: Props) => { const [phone, setPhone] = useState(value); const browserLocales = navigator.language.split('-'); const defaultCountry = browserLocales[browserLocales.length - 1].toLowerCase(); const {inputValue, handlePhoneValueChange, inputRef, country, setCountry} = usePhoneInput({ defaultCountry: defaultCountry || defaultCountries[0][1], value: phone, countries: defaultCountries, onChange: data => { setPhone(data.phone); const mask = getActiveFormattingMask({ phone: data.phone, country: data.country, }); const digitnumbers = mask.split('').filter(c => c === '.').length; const isValid = data.phone.length === 1 + data.country.dialCode.length + digitnumbers; if (isValid) { onChange(data.phone); } else { onChange(''); } }, }); return ( ), }} /> ); }; export default PhoneInput;