The Future of Web Dev
The Future of Web Dev
ISO-Compliant Country Dropdown & Phone Input Components for shadcn/ui
Enhance your web forms with ISO-compliant country dropdown and phone input components using shadcn/ui.

Shadcn-country-dropdown provides a searchable country dropdown with flags and an international phone input for React and Next.js forms that use shadcn/ui.
CountryDropdown returns ISO 3166 country data, and PhoneInput parses international numbers, identifies country calling codes, and displays the corresponding flag.
You can use each component on its own or connect them in a registration, contact, or checkout form.
The country selector uses shadcn/ui Popover and Command components, while the phone field uses libphonenumber-js for number parsing and validation.


Features
- Searchable country selection with country names and circular flags.
- ISO 3166-1 alpha-2 and alpha-3 country codes, international calling codes, and additional country data.
- Single-country and multiple-country selection in
CountryDropdown. - Custom country lists through
options, plusdefaultValueanddisabledsettings. - Compact
slimmode and aninlinelayout for country selection. - International number parsing, country detection, and phone flag updates.
- Country-change callback through
onCountryChangeinPhoneInput. - Exported
phoneSchemafor phone number validation with Zod. - Editable React and TypeScript component source, styled for Tailwind CSS and shadcn/ui.
Use Cases
- International registration and contact forms that request a country and phone number.
- Checkout forms that collect international dialing prefixes and contact details.
- Country selection in profile, location, and account settings.
Installation
In a React or Next.js project configured with shadcn/ui, install the country data, flag, phone parsing, and validation dependencies:
pnpm add react-circle-flags country-data-list libphonenumber-js zod lucide-reactInstall the shadcn/ui components used by CountryDropdown:
npx shadcn@latest add command popoverCopy the component source into your project. Save CountryDropdown as components/ui/country-dropdown.tsx and PhoneInput as components/ui/phone-input.tsx. Both files import cn from @/lib/utils. In Next.js, render them inside a client component.
How To Use It
Country Dropdown
Set defaultValue to an ISO alpha-3 code such as USA. The onChange callback receives the selected country object, including alpha2, alpha3, name, and countryCallingCodes.
import { CountryDropdown } from "@/components/ui/country-dropdown";
<CountryDropdown
placeholder="Select a country"
defaultValue="USA"
onChange={(country) => console.log(country.alpha2, country.name)}
/>International Phone Input
The phone field accepts an ISO alpha-2 code through defaultCountry. Its onChange callback receives an input change event, and e.target.value contains the entered or parsed number.
import { PhoneInput } from "@/components/ui/phone-input";
<PhoneInput
placeholder="Enter phone number"
defaultCountry="US"
onChange={(e) => console.log(e.target.value)}
/>Connect the Country Dropdown and Phone Input
Use the dropdown’s alpha-2 value for the phone field and reset the phone value when the selected country changes. The key forces a fresh phone field after a country change, including its initial calling prefix.
"use client";
import { useState } from "react";
import { CountryDropdown } from "@/components/ui/country-dropdown";
import { PhoneInput, phoneSchema } from "@/components/ui/phone-input";
export function CountryPhoneFields() {
const [country, setCountry] = useState({ alpha2: "US", alpha3: "USA" });
const [phone, setPhone] = useState("");
const [error, setError] = useState("");
return (
<form
className="grid gap-4"
onSubmit={(event) => {
event.preventDefault();
setError(phoneSchema.safeParse(phone).success ? "" : "Enter a valid phone number.");
}}
>
<CountryDropdown
defaultValue={country.alpha3}
onChange={(selected) => {
setCountry({ alpha2: selected.alpha2, alpha3: selected.alpha3 });
setPhone("");
setError("");
}}
/>
<PhoneInput
key={country.alpha2}
defaultCountry={country.alpha2}
value={phone}
onChange={(e) => {
setPhone(e.target.value);
setError("");
}}
placeholder="Enter phone number"
/>
<button type="submit">Validate number</button>
{error && <p role="alert">{error}</p>}
</form>
);
}Select Multiple Countries
Set multiple to select several countries. In this mode, defaultValue accepts an array of alpha-3 codes, and onChange returns an array of country objects.
<CountryDropdown
multiple
defaultValue={["USA", "CAN"]}
onChange={(countries) => console.log(countries.map((country) => country.alpha3))}
/>Component Props
CountryDropdown Props
| Prop | Behavior |
|---|---|
defaultValue | Initial ISO alpha-3 code ("USA"), or an array of codes for multiple selection. |
onChange | Receives a selected Country object, or a Country[] array in multiple mode. |
multiple | Enables selection of more than one country. |
options | Replaces the default searchable country list with a supplied array of country objects. |
placeholder | Text shown before selection in the standard dropdown. |
disabled | Disables the dropdown trigger. |
slim | Uses a compact trigger that omits the selected country name. |
inline | Applies the compact trigger styling used beside a phone input. |
className | Custom classes for the dropdown trigger. |
PhoneInput Props
| Prop | Behavior |
|---|---|
defaultCountry | Initial ISO alpha-2 code ("US"). The component initializes the matching flag and can populate the calling prefix when the value is empty. |
value | Phone number text, for a controlled input. |
onChange | Receives a React input change event. Read the updated number from e.target.value. |
onCountryChange | Receives country data when a parsed number identifies a country, or undefined when that country cannot be determined. |
inline | Uses the paired-field layout and hides the input’s own flag. |
placeholder | Text shown when the phone field is empty. |
className | Custom classes for the phone field container and input. |
| Standard input props | Accepts other React input attributes such as disabled and required. Both components forward their respective element refs. |
Related Resources
- Add Phone Input with Country Validation to Shadcn/ui
- Country & Region Select Components for shadcn/ui
- Flagcn: React Flag Components & Picker for shadcn/ui
FAQs
Should I use USA or US for the default country?
Use USA for CountryDropdown.defaultValue and US for PhoneInput.defaultCountry. The dropdown works with three-letter codes for default selections. The phone component looks up two-letter codes.
Does PhoneInput reject invalid phone numbers automatically?
No. It parses input values and exports phoneSchema for validation with libphonenumber-js and Zod. Run the schema check in your form validation logic.
Can I limit the available countries?
Yes. Supply a filtered country array through CountryDropdown.options. The phone component accepts an initial country through defaultCountry.
