Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 82a51e9

Browse files
authored
Pick sensible default option for phone country dropdown (#10627)
* Pick sensible default option for phone country dropdown * Add tests * Add tests
1 parent f241dea commit 82a51e9

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

Diff for: src/components/views/auth/CountryDropdown.tsx

+16-2
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,30 @@ export default class CountryDropdown extends React.Component<IProps, IState> {
5757
public constructor(props: IProps) {
5858
super(props);
5959

60-
let defaultCountry: PhoneNumberCountryDefinition = COUNTRIES[0];
60+
let defaultCountry: PhoneNumberCountryDefinition | undefined;
6161
const defaultCountryCode = SdkConfig.get("default_country_code");
6262
if (defaultCountryCode) {
6363
const country = COUNTRIES.find((c) => c.iso2 === defaultCountryCode.toUpperCase());
6464
if (country) defaultCountry = country;
6565
}
6666

67+
if (!defaultCountry) {
68+
try {
69+
const locale = new Intl.Locale(navigator.language ?? navigator.languages[0]);
70+
const code = locale.region ?? locale.language ?? locale.baseName;
71+
const displayNames = new Intl.DisplayNames(["en"], { type: "region" });
72+
const displayName = displayNames.of(code).toUpperCase();
73+
defaultCountry = COUNTRIES.find(
74+
(c) => c.iso2 === code.toUpperCase() || c.name.toUpperCase() === displayName,
75+
);
76+
} catch (e) {
77+
console.warn("Failed to detect default locale", e);
78+
}
79+
}
80+
6781
this.state = {
6882
searchQuery: "",
69-
defaultCountry,
83+
defaultCountry: defaultCountry ?? COUNTRIES[0],
7084
};
7185
}
7286

Diff for: test/components/views/auth/CountryDropdown-test.tsx

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
import { render } from "@testing-library/react";
19+
20+
import CountryDropdown from "../../../../src/components/views/auth/CountryDropdown";
21+
import SdkConfig from "../../../../src/SdkConfig";
22+
23+
describe("CountryDropdown", () => {
24+
describe("default_country_code", () => {
25+
afterEach(() => {
26+
SdkConfig.unset();
27+
});
28+
29+
it.each([
30+
["GB", 44],
31+
["IE", 353],
32+
["ES", 34],
33+
["FR", 33],
34+
["PL", 48],
35+
["DE", 49],
36+
])("should respect configured default country code for %s", (config, defaultCountryCode) => {
37+
SdkConfig.add({
38+
default_country_code: config,
39+
});
40+
41+
const fn = jest.fn();
42+
render(<CountryDropdown onOptionChange={fn} isSmall={false} showPrefix={false} />);
43+
expect(fn).toHaveBeenCalledWith(expect.objectContaining({ prefix: defaultCountryCode.toString() }));
44+
});
45+
});
46+
47+
describe("defaultCountry", () => {
48+
it.each([
49+
["en-GB", 44],
50+
["en-ie", 353],
51+
["es-ES", 34],
52+
["fr", 33],
53+
["pl", 48],
54+
["de-DE", 49],
55+
])("should pick appropriate default country for %s", (language, defaultCountryCode) => {
56+
Object.defineProperty(navigator, "language", {
57+
configurable: true,
58+
get() {
59+
return language;
60+
},
61+
});
62+
63+
const fn = jest.fn();
64+
render(<CountryDropdown onOptionChange={fn} isSmall={false} showPrefix={false} />);
65+
expect(fn).toHaveBeenCalledWith(expect.objectContaining({ prefix: defaultCountryCode.toString() }));
66+
});
67+
});
68+
});

0 commit comments

Comments
 (0)