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.

shadcn country dropdown
shadcn phone input

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, plus defaultValue and disabled settings.
  • Compact slim mode and an inline layout for country selection.
  • International number parsing, country detection, and phone flag updates.
  • Country-change callback through onCountryChange in PhoneInput.
  • Exported phoneSchema for 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-react

Install the shadcn/ui components used by CountryDropdown:

npx shadcn@latest add command popover

Copy 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

PropBehavior
defaultValueInitial ISO alpha-3 code ("USA"), or an array of codes for multiple selection.
onChangeReceives a selected Country object, or a Country[] array in multiple mode.
multipleEnables selection of more than one country.
optionsReplaces the default searchable country list with a supplied array of country objects.
placeholderText shown before selection in the standard dropdown.
disabledDisables the dropdown trigger.
slimUses a compact trigger that omits the selected country name.
inlineApplies the compact trigger styling used beside a phone input.
classNameCustom classes for the dropdown trigger.

PhoneInput Props

PropBehavior
defaultCountryInitial ISO alpha-2 code ("US"). The component initializes the matching flag and can populate the calling prefix when the value is empty.
valuePhone number text, for a controlled input.
onChangeReceives a React input change event. Read the updated number from e.target.value.
onCountryChangeReceives country data when a parsed number identifies a country, or undefined when that country cannot be determined.
inlineUses the paired-field layout and hides the input’s own flag.
placeholderText shown when the phone field is empty.
classNameCustom classes for the phone field container and input.
Standard input propsAccepts other React input attributes such as disabled and required. Both components forward their respective element refs.

Related Resources

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.

Matt

Matt

Design Engineer

Leave a Reply

Your email address will not be published. Required fields are marked *