Skip to content

Commit 2661465

Browse files
committed
fix: compare against the displayed country and resolve codes case-insensitively
- pickCountry guarded on the raw country code, so country "gb" or an unknown code like "XX" reported a spurious onCountryChange when the displayed country was re-picked - getCountryByCode uppercased only its argument, leaving lowercase entries in a hand-written countries list unreachable, including for the onCountryChange resolver - regression tests: lowercase code, unknown-code fallback, and a controlled parent that ignores onCountryChange - docs: shared dial-code bullet no longer implies +7 is shared out of the box
1 parent 651bdb8 commit 2661465

3 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎content/docs/components/inputs/phone-input.mdx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ country to match, stripping the dial code from the field — the prefix the
134134
once the `1` lands — and the number keeps every digit it had.
135135
- The **longest** matching dial code wins, so `+880…` picks Bangladesh even if a
136136
shorter code also matches.
137-
- **Shared dial codes** (`+1` US/CA, `+7` RU/KZ) keep the currently selected
138-
country when it is one of the sharing countries, and otherwise pick the first
139-
one in the offered list.
137+
- **Shared dial codes** keep the currently selected country when it is one of
138+
the countries sharing the matched code, and otherwise pick the first one in
139+
the offered list — so `+1` never jumps away from US or CA mid-typing. The same
140+
rule covers any shared code in a custom `countries` list, such as `+7` if you
141+
offer both RU and KZ.
140142
- A dial code that is not in your `countries` list is left alone — the country
141143
only ever switches to a country you actually offer.
142144
- After a switch, the country menu still works normally: picking a country

‎src/registry/components/phone-input.tsx‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ const INPUT_SIZES = {
214214
} as const;
215215

216216
function getCountryByCode(code: string, list: Country[] = COUNTRIES): Country | undefined {
217-
return list.find((c) => c.code === code.toUpperCase());
217+
// Case-insensitive on both sides: the lookup arg arrives in any case
218+
// ("gb", "GB"), and a hand-written `countries` list may not be uppercase.
219+
return list.find((c) => c.code.toUpperCase() === code.toUpperCase());
218220
}
219221

220222
/**
@@ -312,9 +314,12 @@ export const PhoneInput = React.forwardRef<HTMLInputElement, PhoneInputProps>(fu
312314
// country, or keeping it across a shared dial code, is not a change.
313315
const pickCountry = React.useCallback(
314316
(next: Country) => {
315-
if (next.code !== countryCode) setCountryCode(next.code);
317+
// Compare against what is displayed, not the raw code: `country="gb"`
318+
// resolves to GB, and an unknown code falls back to `countries[0]` —
319+
// either way the visible country must match before we report a change.
320+
if (next.code !== selectedCountry.code) setCountryCode(next.code);
316321
},
317-
[countryCode, setCountryCode],
322+
[selectedCountry.code, setCountryCode],
318323
);
319324
const [inputValue, setInputValue] = useControllableState(value, defaultValue ?? "", undefined);
320325
const [open, setOpen] = React.useState(false);

‎test/phone-input.test.tsx‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe("PhoneInput", () => {
104104
});
105105
});
106106

107-
describe("PhoneInput controlled country", () => {
107+
describe("PhoneInput country control and detection", () => {
108108
const numberField = () => screen.getByRole<HTMLInputElement>("textbox", { name: "Phone number" });
109109

110110
it("follows an externally changed country prop", async () => {
@@ -141,6 +141,49 @@ describe("PhoneInput controlled country", () => {
141141
expect(onCountryChange).toHaveBeenCalledTimes(1);
142142
});
143143

144+
it("does not report a change for a lowercase country code's own country", async () => {
145+
const onCountryChange = vi.fn();
146+
render(<PhoneInput defaultCountry="gb" onCountryChange={onCountryChange} />);
147+
expect(screen.getByText("+44")).toBeInTheDocument();
148+
149+
// "gb" resolves to GB for display — re-picking GB is not a transition.
150+
await userEvent.click(screen.getByRole("button", { name: "Select country" }));
151+
await userEvent.click(screen.getByRole("option", { name: /United Kingdom/ }));
152+
expect(onCountryChange).not.toHaveBeenCalled();
153+
154+
// A country that actually differs still reports.
155+
await userEvent.click(screen.getByRole("button", { name: "Select country" }));
156+
await userEvent.click(screen.getByRole("option", { name: /France/ }));
157+
expect(onCountryChange).toHaveBeenCalledTimes(1);
158+
expect(onCountryChange).toHaveBeenCalledWith(expect.objectContaining({ code: "FR" }));
159+
});
160+
161+
it("does not report a change when picking the country an unknown code falls back to", async () => {
162+
const onCountryChange = vi.fn();
163+
render(<PhoneInput defaultCountry="XX" onCountryChange={onCountryChange} />);
164+
// An unknown code displays the first offered country.
165+
expect(screen.getByText("+1")).toBeInTheDocument();
166+
167+
await userEvent.click(screen.getByRole("button", { name: "Select country" }));
168+
await userEvent.click(screen.getByRole("option", { name: /United States/ }));
169+
expect(onCountryChange).not.toHaveBeenCalled();
170+
});
171+
172+
it("keeps a fixed controlled country when the parent ignores onCountryChange", async () => {
173+
const onCountryChange = vi.fn();
174+
render(<PhoneInput country="IN" onCountryChange={onCountryChange} />);
175+
expect(screen.getByText("+91")).toBeInTheDocument();
176+
177+
await userEvent.click(numberField());
178+
await userEvent.paste("+44 20 7946 0958");
179+
180+
// The parent owns the country, so the trigger stays put — but detection
181+
// still strips the prefix and reports the country it intended.
182+
expect(screen.getByText("+91")).toBeInTheDocument();
183+
expect(numberField()).toHaveValue(" 20 7946 0958");
184+
expect(onCountryChange).toHaveBeenCalledWith(expect.objectContaining({ code: "GB" }));
185+
});
186+
144187
it("selects GB when a +44 number is pasted, stripping the prefix", async () => {
145188
const onValueChange = vi.fn();
146189
const onCountryChange = vi.fn();

0 commit comments

Comments
 (0)