Skip to content

Commit 74c37e4

Browse files
committed
refactor: Sync country data and phone auth changes
1 parent 2486915 commit 74c37e4

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

packages/core/src/country-data.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,17 +267,22 @@ export const countryData = [
267267

268268
export type CountryData = (typeof countryData)[number];
269269

270-
export type CountryCodes = CountryData["code"];
270+
export type CountryCode = CountryData["code"];
271271

272-
export function getCountryByDialCode(dialCode: string): (typeof countryData)[number] | undefined {
272+
export function getCountryByDialCode(dialCode: string): CountryData | undefined {
273273
return countryData.find((country) => country.dialCode === dialCode);
274274
}
275275

276-
export function getCountryByCode(code: string): (typeof countryData)[number] | undefined {
276+
export function getCountryByCode(code: CountryCode): CountryData | undefined {
277277
return countryData.find((country) => country.code === code.toUpperCase());
278278
}
279279

280-
export function formatPhoneNumberWithCountry(phoneNumber: string, countryDialCode: string): string {
280+
export function formatPhoneNumberWithCountry(phoneNumber: string, countryCode: CountryCode): string {
281+
const countryData = getCountryByCode(countryCode);
282+
if (!countryData) {
283+
return phoneNumber;
284+
}
285+
const countryDialCode = countryData.dialCode;
281286
// Remove any existing dial code if present
282287
const cleanNumber = phoneNumber.replace(/^\+\d+/, "").trim();
283288
return `${countryDialCode}${cleanNumber}`;

packages/react/src/auth/forms/phone-auth-form.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import {
2020
confirmPhoneNumber,
21-
CountryData,
21+
CountryCode,
2222
countryData,
2323
createPhoneFormSchema,
2424
FirebaseUIError,
@@ -47,7 +47,8 @@ function PhoneNumberForm({ onSubmit, formError, recaptchaVerifier, recaptchaCont
4747
const ui = useUI();
4848

4949
// TODO(ehesp): How does this support allowed countries?
50-
const [selectedCountry, setSelectedCountry] = useState<CountryData>(countryData[0]);
50+
// TODO(ehesp): How does this support default country?
51+
const [selectedCountry, setSelectedCountry] = useState<CountryCode>(countryData[0].code);
5152
const [firstValidationOccured, setFirstValidationOccured] = useState(false);
5253

5354
const phoneFormSchema = useMemo(
@@ -67,7 +68,7 @@ function PhoneNumberForm({ onSubmit, formError, recaptchaVerifier, recaptchaCont
6768
onSubmit: phoneFormSchema,
6869
},
6970
onSubmit: async ({ value }) => {
70-
const formattedNumber = formatPhoneNumberWithCountry(value.phoneNumber, selectedCountry.dialCode);
71+
const formattedNumber = formatPhoneNumberWithCountry(value.phoneNumber, selectedCountry);
7172
await onSubmit(formattedNumber);
7273
},
7374
});
@@ -94,7 +95,7 @@ function PhoneNumberForm({ onSubmit, formError, recaptchaVerifier, recaptchaCont
9495
<div className="fui-phone-input">
9596
<CountrySelector
9697
value={selectedCountry}
97-
onChange={(country) => setSelectedCountry(country as CountryData)}
98+
onChange={(code) => setSelectedCountry(code as CountryCode)}
9899
className="fui-phone-input__country-selector"
99100
/>
100101
<input

packages/react/src/components/country-selector.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("CountrySelector Component", () => {
2929
});
3030

3131
it("renders with the selected country", () => {
32-
render(<CountrySelector value={defaultCountry} onChange={mockOnChange} />);
32+
render(<CountrySelector value={defaultCountry.code} onChange={mockOnChange} />);
3333

3434
// Check if the country flag emoji is displayed
3535
expect(screen.getByText(defaultCountry.emoji)).toBeInTheDocument();
@@ -43,15 +43,15 @@ describe("CountrySelector Component", () => {
4343
});
4444

4545
it("applies custom className", () => {
46-
render(<CountrySelector value={defaultCountry} onChange={mockOnChange} className="custom-class" />);
46+
render(<CountrySelector value={defaultCountry.code} onChange={mockOnChange} className="custom-class" />);
4747

4848
const selector = screen.getByRole("combobox").closest(".fui-country-selector");
4949
expect(selector).toHaveClass("fui-country-selector");
5050
expect(selector).toHaveClass("custom-class");
5151
});
5252

5353
it("calls onChange when a different country is selected", () => {
54-
render(<CountrySelector value={defaultCountry} onChange={mockOnChange} />);
54+
render(<CountrySelector value={defaultCountry.code} onChange={mockOnChange} />);
5555

5656
const select = screen.getByRole("combobox");
5757

@@ -72,7 +72,7 @@ describe("CountrySelector Component", () => {
7272
});
7373

7474
it("renders all countries in the dropdown", () => {
75-
render(<CountrySelector value={defaultCountry} onChange={mockOnChange} />);
75+
render(<CountrySelector value={defaultCountry.code} onChange={mockOnChange} />);
7676

7777
const select = screen.getByRole("combobox");
7878
const options = select.querySelectorAll("option");

packages/react/src/components/country-selector.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,38 @@
1616

1717
"use client";
1818

19-
import { CountryCodes, CountryData, countryData } from "@firebase-ui/core";
19+
import { CountryCode, countryData, getCountryByCode } from "@firebase-ui/core";
2020
import { ComponentProps } from "react";
2121
import { cn } from "~/utils/cn";
2222

2323
export type CountrySelectorProps = ComponentProps<"div"> & {
24-
value: CountryData;
25-
onChange: (country: CountryData) => void;
26-
allowedCountries?: CountryCodes[];
24+
value: CountryCode;
25+
onChange: (code: CountryCode) => void;
26+
allowedCountries?: CountryCode[];
2727
};
2828

2929
export function CountrySelector({ value, onChange, allowedCountries, className, ...props }: CountrySelectorProps) {
3030

31+
const country = getCountryByCode(value);
3132
const countries = allowedCountries ? countryData.filter((c) => allowedCountries.includes(c.code)) : countryData;
3233

34+
if (!country) {
35+
return null;
36+
}
37+
3338
return (
3439
<div className={cn("fui-country-selector", className)} {...props}>
3540
<div className="fui-country-selector__wrapper">
36-
<span className="fui-country-selector__flag">{value.emoji}</span>
41+
<span className="fui-country-selector__flag">{country.emoji}</span>
3742
<div className="fui-country-selector__select-wrapper">
38-
<span className="fui-country-selector__dial-code">{value.dialCode}</span>
43+
<span className="fui-country-selector__dial-code">{country.dialCode}</span>
3944
<select
4045
className="fui-country-selector__select"
41-
value={value.code}
46+
value={country.code}
4247
onChange={(e) => {
43-
const country = countries.find((c) => c.code === e.target.value);
48+
const country = getCountryByCode(e.target.value as CountryCode);
4449
if (country) {
45-
onChange(country);
50+
onChange(country.code);
4651
}
4752
}}
4853
>

0 commit comments

Comments
 (0)