Skip to content

Commit 027653b

Browse files
committed
fix: address PhoneInput review feedback
Document the menu as the union of countries and preferredCountries, add preferredDivider to the classNames list, correct remaining 50-country copy to ~244, uppercase-normalize the preferred-code dedup, reject a non-leading plus sign, and assert the exact E.164 table size.
1 parent 7824360 commit 027653b

4 files changed

Lines changed: 65 additions & 53 deletions

File tree

‎content/docs/components/index.mdx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Choose a region and read back the locale defaults that come with it — for sett
2424

2525
International phone number input with country selector and dial code search.
2626

27-
| Component | Description |
28-
| ------------------------------------------------------ | -------------------------------------------------------------------- |
29-
| **[Phone Input](/docs/components/inputs/phone-input)** | Country selector with flags, dial codes, and search for 50 countries |
27+
| Component | Description |
28+
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
29+
| **[Phone Input](/docs/components/inputs/phone-input)** | Country selector with flags, dial codes, and search for every E.164 country (~244 entries) |
3030

3131
### Navigation
3232

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

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ the field — the prefix the `+` declares outranks a manually picked country:
161161
the offered list — so `+1` never jumps away from US or CA mid-typing. The same
162162
rule covers any shared code in a custom `countries` list, such as `+7` if you
163163
offer both RU and KZ.
164-
- A dial code that is not in your `countries` list is left alone — the country
165-
only ever switches to a country you actually offer.
164+
- A dial code that is not offered is left alone — the country only ever
165+
switches to a country you actually offer. The menu is the union of
166+
`countries` and `preferredCountries`, so a country pinned via
167+
`preferredCountries` stays selectable even when it is absent from
168+
`countries`.
166169
- After a switch, the country menu still works normally: picking a country
167170
overrides the detected one, and nothing re-detects until the next time the
168171
value starts with `+`.
@@ -192,32 +195,32 @@ deliberately out of scope.
192195

193196
## Props
194197

195-
| Prop | Type | Required | Description |
196-
| -------------------- | ------------------------------------------- | -------- | --------------------------------------------------------------------------------------------------------- |
197-
| `value` | `string` | No | Phone number — pass this to control the component |
198-
| `defaultValue` | `string` | No | Initial phone number when uncontrolled |
199-
| `onValueChange` | `(phone: string, country: Country) => void` | No | Callback when phone number or country changes |
200-
| `country` | `string` | No | Selected country code (ISO 3166-1 alpha-2) — pass this to control the country |
201-
| `onCountryChange` | `(country: Country) => void` | No | Callback when the selected country changes — from the menu or dial-code detection |
202-
| `validate` | `boolean` | No | Mark the input `aria-invalid` when a non-empty number is malformed (default: `false`) |
203-
| `onValidityChange` | `(valid: boolean, e164: string) => void` | No | Callback with basic E.164 validity and the normalized number, whenever the digits change |
204-
| `defaultCountry` | `string` | No | Initial country code when uncontrolled (ISO 3166-1 alpha-2, e.g. `"US"`, `"GB"`, `"IN"`; default: `"US"`) |
205-
| `countries` | `(Country \| string)[]` | No | Countries offered, in display order; entries are `Country` objects or ISO codes (default: `COUNTRIES`) |
206-
| `preferredCountries` | `(Country \| string)[]` | No | Countries pinned above a divider at the top; added even when not in `countries`, and deduplicated |
207-
| `placeholder` | `string` | No | Placeholder text |
208-
| `disabled` | `boolean` | No | Disable the input |
209-
| `showFlags` | `boolean` | No | Show country flag next to dial code (default: `true`) |
210-
| `size` | `"sm" \| "default" \| "lg"` | No | Field height (default: `"default"`) |
211-
| `align` | `"start" \| "end"` | No | Edge of the field the menu lines up with (default: `"start"`) |
212-
| `side` | `"top" \| "bottom"` | No | Preferred menu side; flips when there is no room (default: `"bottom"`) |
213-
| `searchPlaceholder` | `string` | No | Placeholder for the country search (default: `"Search countries..."`) |
214-
| `emptyMessage` | `ReactNode` | No | Shown when the search matches nothing (default: `"No countries found."`) |
215-
| `labels` | `Partial<PhoneInputLabels>` | No | Override the built-in English UI text (see below) |
216-
| `displayLocale` | `string` | No | Locale for country names, e.g. `"fr"` (default: English names) |
217-
| `maxHeight` | `number \| string` | No | Max height of the scrolling country list (default: `"15rem"`) |
218-
| `label` | `string` | No | Accessible label for the input |
219-
| `className` | `string` | No | Classes for the root wrapper |
220-
| `classNames` | `PhoneInputClassNames` | No | Per-part classes: `root`, `field`, `trigger`, `input`, `content`, `search`, `list`, `item`, `empty` |
198+
| Prop | Type | Required | Description |
199+
| -------------------- | ------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------- |
200+
| `value` | `string` | No | Phone number — pass this to control the component |
201+
| `defaultValue` | `string` | No | Initial phone number when uncontrolled |
202+
| `onValueChange` | `(phone: string, country: Country) => void` | No | Callback when phone number or country changes |
203+
| `country` | `string` | No | Selected country code (ISO 3166-1 alpha-2) — pass this to control the country |
204+
| `onCountryChange` | `(country: Country) => void` | No | Callback when the selected country changes — from the menu or dial-code detection |
205+
| `validate` | `boolean` | No | Mark the input `aria-invalid` when a non-empty number is malformed (default: `false`) |
206+
| `onValidityChange` | `(valid: boolean, e164: string) => void` | No | Callback with basic E.164 validity and the normalized number, whenever the digits change |
207+
| `defaultCountry` | `string` | No | Initial country code when uncontrolled (ISO 3166-1 alpha-2, e.g. `"US"`, `"GB"`, `"IN"`; default: `"US"`) |
208+
| `countries` | `(Country \| string)[]` | No | Countries offered, in display order; entries are `Country` objects or ISO codes (default: `COUNTRIES`) |
209+
| `preferredCountries` | `(Country \| string)[]` | No | Countries pinned above a divider at the top; added even when not in `countries`, and deduplicated |
210+
| `placeholder` | `string` | No | Placeholder text |
211+
| `disabled` | `boolean` | No | Disable the input |
212+
| `showFlags` | `boolean` | No | Show country flag next to dial code (default: `true`) |
213+
| `size` | `"sm" \| "default" \| "lg"` | No | Field height (default: `"default"`) |
214+
| `align` | `"start" \| "end"` | No | Edge of the field the menu lines up with (default: `"start"`) |
215+
| `side` | `"top" \| "bottom"` | No | Preferred menu side; flips when there is no room (default: `"bottom"`) |
216+
| `searchPlaceholder` | `string` | No | Placeholder for the country search (default: `"Search countries..."`) |
217+
| `emptyMessage` | `ReactNode` | No | Shown when the search matches nothing (default: `"No countries found."`) |
218+
| `labels` | `Partial<PhoneInputLabels>` | No | Override the built-in English UI text (see below) |
219+
| `displayLocale` | `string` | No | Locale for country names, e.g. `"fr"` (default: English names) |
220+
| `maxHeight` | `number \| string` | No | Max height of the scrolling country list (default: `"15rem"`) |
221+
| `label` | `string` | No | Accessible label for the input |
222+
| `className` | `string` | No | Classes for the root wrapper |
223+
| `classNames` | `PhoneInputClassNames` | No | Per-part classes: `root`, `field`, `trigger`, `input`, `content`, `search`, `list`, `item`, `preferredDivider`, `empty` |
221224

222225
Any other prop — `name`, `required`, `autoComplete`, `aria-invalid`, `data-*` — is
223226
spread onto the underlying `<input>`, and the ref is forwarded to it, so the
@@ -431,21 +434,22 @@ scrolls.
431434

432435
## Supported Countries
433436

434-
The input ships with 50 countries pre-loaded. Use the dropdown search to find any country by name or dial code. The full list includes:
435-
436-
| Country | Code | Dial Code | Flag |
437-
| -------------------------------------- | ---- | --------- | ---- |
438-
| United States | `US` | `+1` | 🇺🇸 |
439-
| United Kingdom | `GB` | `+44` | 🇬🇧 |
440-
| Canada | `CA` | `+1` | 🇨🇦 |
441-
| Australia | `AU` | `+61` | 🇦🇺 |
442-
| India | `IN` | `+91` | 🇮🇳 |
443-
| Germany | `DE` | `+49` | 🇩🇪 |
444-
| France | `FR` | `+33` | 🇫🇷 |
445-
| Japan | `JP` | `+81` | 🇯🇵 |
446-
| Brazil | `BR` | `+55` | 🇧🇷 |
447-
| South Korea | `KR` | `+82` | 🇰🇷 |
448-
| ...and 40 more via the dropdown search |
437+
The input ships with ~244 ITU-T E.164 countries pre-loaded. Use the dropdown
438+
search to find any country by name or dial code. A sample of the list:
439+
440+
| Country | Code | Dial Code | Flag |
441+
| --------------------------------------- | ---- | --------- | ---- |
442+
| United States | `US` | `+1` | 🇺🇸 |
443+
| United Kingdom | `GB` | `+44` | 🇬🇧 |
444+
| Canada | `CA` | `+1` | 🇨🇦 |
445+
| Australia | `AU` | `+61` | 🇦🇺 |
446+
| India | `IN` | `+91` | 🇮🇳 |
447+
| Germany | `DE` | `+49` | 🇩🇪 |
448+
| France | `FR` | `+33` | 🇫🇷 |
449+
| Japan | `JP` | `+81` | 🇯🇵 |
450+
| Brazil | `BR` | `+55` | 🇧🇷 |
451+
| South Korea | `KR` | `+82` | 🇰🇷 |
452+
| ...and 235 more via the dropdown search |
449453

450454
## Dependencies
451455

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,10 @@ function resolveCountries(
498498

499499
// Characters a user may legitimately type as visual formatting are stripped
500500
// before the digits-only check; anything beyond those and a leading "+" makes
501-
// the number malformed.
502-
const UNEXPECTED = /[^0-9 \-().+]/;
501+
// the number malformed. The leading "+" is removed first, so any "+" that
502+
// remains (e.g. "1+2") is unexpected.
503+
const FORMATTING = /[ \-().]/g;
504+
const UNEXPECTED = /[^0-9 ]/;
503505

504506
/**
505507
* Basic E.164 normalization. Formatting characters are stripped, the result
@@ -508,9 +510,12 @@ const UNEXPECTED = /[^0-9 \-().+]/;
508510
* selected country's dial code is not prepended on top of it.
509511
*/
510512
function toE164(input: string, dialCode: string): { e164: string; valid: boolean } {
511-
const digits = input.replace(/\D/g, "");
512-
const hasUnexpected = UNEXPECTED.test(input);
513+
// A leading "+" declares its own country code; strip it (and the formatting
514+
// characters) before the digits-only / unexpected-character checks.
513515
const declaresOwn = input.trim().startsWith("+");
516+
const stripped = input.replace(FORMATTING, "").replace(declaresOwn ? /^\+/ : /^$/, "");
517+
const digits = stripped.replace(/\D/g, "");
518+
const hasUnexpected = UNEXPECTED.test(stripped);
514519
const all = (declaresOwn ? "" : dialCode.replace(/\D/g, "")) + digits;
515520
// With no digits typed there is no number yet, whatever the dial code says.
516521
return {
@@ -617,12 +622,14 @@ export const PhoneInput = React.forwardRef<HTMLInputElement, PhoneInputProps>(fu
617622
() => resolveCountries(preferredCountries, []),
618623
[preferredCountries],
619624
);
625+
// One normalized (uppercase) preferred-code set, so a hand-written lowercase
626+
// `Country.code` still matches its `baseList` twin and is deduplicated out.
620627
const preferredCodes = React.useMemo(
621-
() => new Set(preferredList.map((c) => c.code)),
628+
() => new Set(preferredList.map((c) => c.code.toUpperCase())),
622629
[preferredList],
623630
);
624631
const offered = React.useMemo(
625-
() => [...preferredList, ...baseList.filter((c) => !preferredCodes.has(c.code))],
632+
() => [...preferredList, ...baseList.filter((c) => !preferredCodes.has(c.code.toUpperCase()))],
626633
[preferredList, preferredCodes, baseList],
627634
);
628635

‎test/phone-input.test.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,8 @@ describe("PhoneInput full country list", () => {
692692
.join("");
693693

694694
it("exports a full E.164 table with derived names and flags", () => {
695-
expect(COUNTRIES.length).toBeGreaterThanOrEqual(240);
695+
// Update this expected count whenever the ITU-T E.164 country list changes.
696+
expect(COUNTRIES.length).toBe(244);
696697

697698
const seen = new Set<string>();
698699
for (const c of COUNTRIES) {

0 commit comments

Comments
 (0)