import React from 'react'; import { CountryCode, formatPhoneNumber } from 'soapbox/utils/phone'; import HStack from '../hstack/hstack'; import Input from '../input/input'; import CountryCodeDropdown from './country-code-dropdown'; interface IPhoneInput extends Pick, 'required' | 'autoFocus'> { /** Input phone number. */ value?: string, /** E164 country code. */ countryCode?: CountryCode, /** Change event handler taking the formatted input. */ onChange?: (phone: string) => void, } /** Internationalized phone input with country code picker. */ const PhoneInput: React.FC = (props) => { const { countryCode = 1, value = '', onChange, ...rest } = props; const handleCountryChange = (code: CountryCode) => { if (onChange) { onChange(formatPhoneNumber(countryCode, value)); } }; /** Pass the formatted phone to the handler. */ const handleChange: React.ChangeEventHandler = ({ target }) => { if (onChange) { onChange(formatPhoneNumber(countryCode, target.value)); } }; return ( ); }; export default PhoneInput;