PhoneInput: prettify input while typing

environments/review-phone-inpu-3kvl0k/deployments/542
Alex Gleason 2022-07-13 18:08:59 -05:00
rodzic f62dcc6650
commit ba98a8e82f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
1 zmienionych plików z 10 dodań i 2 usunięć

Wyświetl plik

@ -1,4 +1,4 @@
import { parsePhoneNumber } from 'libphonenumber-js';
import { parsePhoneNumber, AsYouType } from 'libphonenumber-js';
import React, { useState, useEffect } from 'react';
import { CountryCode } from 'soapbox/utils/phone';
@ -29,7 +29,15 @@ const PhoneInput: React.FC<IPhoneInput> = (props) => {
};
const handleChange: React.ChangeEventHandler<HTMLInputElement> = ({ target }) => {
setNationalNumber(target.value);
// 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);
}
};
// When the internal state changes, update the external state.