Pick sensible default option for phone country dropdown (#10627)

* Pick sensible default option for phone country dropdown

* Add tests

* Add tests
This commit is contained in:
Michael Telatynski 2023-04-18 09:23:37 +01:00 committed by GitHub
parent f241dea137
commit 82a51e9674
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 2 deletions

View file

@ -57,16 +57,30 @@ export default class CountryDropdown extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);
let defaultCountry: PhoneNumberCountryDefinition = COUNTRIES[0];
let defaultCountry: PhoneNumberCountryDefinition | undefined;
const defaultCountryCode = SdkConfig.get("default_country_code");
if (defaultCountryCode) {
const country = COUNTRIES.find((c) => c.iso2 === defaultCountryCode.toUpperCase());
if (country) defaultCountry = country;
}
if (!defaultCountry) {
try {
const locale = new Intl.Locale(navigator.language ?? navigator.languages[0]);
const code = locale.region ?? locale.language ?? locale.baseName;
const displayNames = new Intl.DisplayNames(["en"], { type: "region" });
const displayName = displayNames.of(code).toUpperCase();
defaultCountry = COUNTRIES.find(
(c) => c.iso2 === code.toUpperCase() || c.name.toUpperCase() === displayName,
);
} catch (e) {
console.warn("Failed to detect default locale", e);
}
}
this.state = {
searchQuery: "",
defaultCountry,
defaultCountry: defaultCountry ?? COUNTRIES[0],
};
}