2022-07-13 16:08:59 -07:00
|
|
|
import { parsePhoneNumber, AsYouType } from 'libphonenumber-js';
|
2022-07-13 15:37:40 -07:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2022-07-13 07:42:58 -07:00
|
|
|
|
2022-07-13 15:37:40 -07:00
|
|
|
import { CountryCode } from 'soapbox/utils/phone';
|
2022-07-13 07:42:58 -07:00
|
|
|
|
2022-07-13 09:40:02 -07:00
|
|
|
import HStack from '../hstack/hstack';
|
2022-07-13 07:42:58 -07:00
|
|
|
import Input from '../input/input';
|
|
|
|
|
2022-07-13 09:40:02 -07:00
|
|
|
import CountryCodeDropdown from './country-code-dropdown';
|
|
|
|
|
|
|
|
interface IPhoneInput extends Pick<React.InputHTMLAttributes<HTMLInputElement>, 'required' | 'autoFocus'> {
|
2022-07-13 15:37:40 -07:00
|
|
|
/** E164 phone number. */
|
2022-07-13 07:42:58 -07:00
|
|
|
value?: string,
|
2022-07-13 15:37:40 -07:00
|
|
|
/** Change handler which receives the E164 phone string. */
|
|
|
|
onChange?: (phone: string | undefined) => void,
|
|
|
|
/** Country code that's selected on mount. */
|
|
|
|
defaultCountryCode?: CountryCode,
|
2022-07-13 07:42:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Internationalized phone input with country code picker. */
|
|
|
|
const PhoneInput: React.FC<IPhoneInput> = (props) => {
|
2022-07-13 15:37:40 -07:00
|
|
|
const { value, onChange, defaultCountryCode = '1', ...rest } = props;
|
|
|
|
|
|
|
|
const [countryCode, setCountryCode] = useState<CountryCode>(defaultCountryCode);
|
|
|
|
const [nationalNumber, setNationalNumber] = useState<string>('');
|
2022-07-13 09:40:02 -07:00
|
|
|
|
2022-07-13 07:42:58 -07:00
|
|
|
const handleChange: React.ChangeEventHandler<HTMLInputElement> = ({ target }) => {
|
2022-07-13 16:08:59 -07:00
|
|
|
// HACK: AsYouType is not meant to be used this way. But it works!
|
|
|
|
const asYouType = new AsYouType({ defaultCallingCode: countryCode });
|
|
|
|
const formatted = asYouType.input(target.value);
|
|
|
|
|
|
|
|
if (formatted === nationalNumber && target.value !== nationalNumber) {
|
|
|
|
setNationalNumber(target.value);
|
|
|
|
} else {
|
|
|
|
setNationalNumber(formatted);
|
|
|
|
}
|
2022-07-13 15:37:40 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// When the internal state changes, update the external state.
|
|
|
|
useEffect(() => {
|
2022-07-13 07:42:58 -07:00
|
|
|
if (onChange) {
|
2022-07-13 15:37:40 -07:00
|
|
|
try {
|
|
|
|
const opts = { defaultCallingCode: countryCode, extract: false } as any;
|
|
|
|
const result = parsePhoneNumber(nationalNumber, opts);
|
|
|
|
|
|
|
|
if (!result.isPossible()) {
|
|
|
|
throw result;
|
|
|
|
}
|
|
|
|
|
|
|
|
onChange(result.format('E.164'));
|
|
|
|
} catch (e) {
|
|
|
|
// The value returned is always a valid E164 string.
|
|
|
|
// If it's not valid, it'll return undefined.
|
|
|
|
onChange(undefined);
|
|
|
|
}
|
2022-07-13 07:42:58 -07:00
|
|
|
}
|
2022-07-13 15:37:40 -07:00
|
|
|
}, [countryCode, nationalNumber]);
|
2022-07-13 07:42:58 -07:00
|
|
|
|
|
|
|
return (
|
2022-07-13 16:45:42 -07:00
|
|
|
<HStack className='mt-1 shadow-sm'>
|
|
|
|
<div className='dark:bg-slate-800 border border-solid border-r-0 border-gray-300 dark:border-gray-600 flex items-center rounded-l-md'>
|
|
|
|
<CountryCodeDropdown
|
|
|
|
countryCode={countryCode}
|
|
|
|
onChange={setCountryCode}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-07-13 09:40:02 -07:00
|
|
|
|
|
|
|
<Input
|
|
|
|
type='text'
|
2022-07-13 16:45:42 -07:00
|
|
|
outerClassName='mt-0 shadow-none'
|
|
|
|
className='rounded-l-none'
|
2022-07-13 09:40:02 -07:00
|
|
|
onChange={handleChange}
|
2022-07-13 15:37:40 -07:00
|
|
|
value={nationalNumber}
|
2022-07-13 09:40:02 -07:00
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
</HStack>
|
2022-07-13 07:42:58 -07:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PhoneInput;
|