diff --git a/src/common/geolocator.ts b/src/common/geolocator.ts index 4e519bd..ebaec86 100644 --- a/src/common/geolocator.ts +++ b/src/common/geolocator.ts @@ -1,9 +1,7 @@ export class Geolocator { async getCurrentRegion(): Promise { - return fetch('https://api.country.is').then((response) => - response - .json() - .then((data: { ip: string; country: string }) => data.country) - ) + const response = await fetch('https://api.country.is') + const json: { ip: string; country: string } = await response.json() + return json.country } } diff --git a/src/form/PhoneField/index.tsx b/src/form/PhoneField/index.tsx index 8a02f39..c92729a 100644 --- a/src/form/PhoneField/index.tsx +++ b/src/form/PhoneField/index.tsx @@ -115,17 +115,20 @@ export const PhoneField: React.FC = ({ ) const filterOption = useCallback((option: Option, rawInput: string) => { - let equal = true - if (rawInput) { - equal = [ - option.value, - option.data.countryName, - option.data.code, - ].some(x => x.startsWith(rawInput)) + const inputLowerCase = rawInput.toLowerCase() + if (inputLowerCase.startsWith('+')) { + return inputLowerCase.length == 1 || option.data.code.startsWith(inputLowerCase.substr(1)) + } else { + return [ + option.value, + option.data.countryName, + option.data.code, + ].some(x => x.toLowerCase().startsWith(inputLowerCase)) + } } - return equal + return true }, []) useEffect(() => { @@ -135,13 +138,19 @@ export const PhoneField: React.FC = ({ return } - new Geolocator().getCurrentRegion().then((countryName) => { - const region = getRegionByPhone( - `+${getCountryCodeForRegion(countryName as CountryName) ?? 7}` - ) + const languages = navigator.languages || [navigator.language] + const firstNonEnglish = languages.find(x => !x.toLowerCase().includes('en')) + if (firstNonEnglish?.toLowerCase() !== 'ru') { + new Geolocator().getCurrentRegion().then(countryName => { + const region = getRegionByPhone( + `+${getCountryCodeForRegion(countryName as CountryName) ?? 7}` + ) - setRegion(region) - }) + setRegion(region) + }) + } else { + setRegion(getRegionByPhone('+7')) + } }, []) return useMemo(() => { diff --git a/src/form/PhoneField/utils/phone.tsx b/src/form/PhoneField/utils/phone.tsx index 094f432..7766f7f 100644 --- a/src/form/PhoneField/utils/phone.tsx +++ b/src/form/PhoneField/utils/phone.tsx @@ -93,10 +93,7 @@ function prepareRegionData() { return { byRegion, byCode } } -export const { - byRegion: phoneNumberDataByRegion, - byCode: phoneNumberDataByCode, -} = prepareRegionData() +export const { byRegion: phoneNumberDataByRegion } = prepareRegionData() export const regionSelectOptions: Region[] = values( phoneNumberDataByRegion