v1.5

Phone Input

Phone number field with a country flag select, a dial code prefix and per-country formatting. It reports a submit-ready E.164 value.

+1

Description

PhoneInput is a complete phone field. It combines a country select with flags, a display-only dial code, and a text input that formats the national number for the chosen country. It uses libphonenumber-js for formatting and validation.

The input shows national digits only. The full E.164 number (for example +15551234567) exists in the onValueChange payload, and an empty field reports "".

Use it whenever a form collects a phone number. Do not rebuild it from InputGroup, InputGroupSelect and CountryFlag; it already is that composition. For prefix-and-select fields that are not phone numbers, such as amount and currency, compose InputGroup directly. For plain text, use Input.

Installation

pnpm dlx @create-ui/cli add phone-input

The command also adds input-group, select, country-flag, tooltip and the usePhoneInput hook.

Usage

import { PhoneInput } from "@/components/ui/phone-input"
<Field>
  <FieldLabel htmlFor="phone">Phone Number</FieldLabel>
  <PhoneInput
    id="phone"
    defaultCountry="US"
    onValueChange={(value, details) => console.log(value, details.isValid)}
  />
</Field>

Examples

With a label

Wrap the input in Field, add a FieldLabel, and tie them together with id and htmlFor. Field does not wire the id for you. size, invalid, disabled and loading flow from Field when you leave them unset on the input.

+1

Reading the value

onValueChange receives the E.164 string and a details object. Validate with the isPossible and isValid flags instead of parsing the string again.

<PhoneInput
  defaultCountry="TR"
  onValueChange={(
    value,
    { country, dialCode, nationalNumber, isPossible, isValid }
  ) => {
    // value: "+905321234567", or "" when the field is empty
  }}
/>

The callback fires again when the country changes, with the new dial code applied to the same digits.

Seeding a value

defaultValue takes either an E.164 string with a leading + or plain national digits. A leading + carries its own country, so the select follows it and defaultCountry becomes a fallback. Pasting a number that starts with + into the field does the same.

<PhoneInput defaultValue="+4915112345678" />

Controlled

Pass value with onValueChange to own the value in the parent. value takes the E.164 string that onValueChange emits (national digits also work). Setting value from the parent updates the field without firing onValueChange again, and "" clears it. A value with a leading + carries its own country: the select follows it and onCountryChange reports it.

const [phone, setPhone] = React.useState("")
 
<PhoneInput value={phone} onValueChange={(next) => setPhone(next)} />

With React Hook Form, wire it through Controller so the form stores the E.164 string and reset() refills the field. register() works for submit only: it collects the text typed in the field, and reset() and setValue() do not update the field.

+1

Controlling the country

The text value can be controlled with value and onValueChange, as above. The country has its own pair, country and onCountryChange.

const [country, setCountry] = React.useState<PhoneCountryCode>("DE")
 
<PhoneInput country={country} onCountryChange={setCountry} />

PhoneCountryCode comes from @/hooks/use-phone-input.

Limiting the countries

countries takes a list of PhoneCountryConfig entries. Build them from PHONE_COUNTRIES, which the hook exports. The default is every country, sorted by English name.

import { PHONE_COUNTRIES } from "@/hooks/use-phone-input"
 
const NORDICS = [
  PHONE_COUNTRIES.SE,
  PHONE_COUNTRIES.NO,
  PHONE_COUNTRIES.DK,
  PHONE_COUNTRIES.FI,
]
 
<PhoneInput countries={NORDICS} defaultCountry="SE" />

Helper icon

A help icon with a tooltip sits at the end of the field. Change the tooltip text with helperTooltip, pass null to keep the icon without a tooltip, or hide the icon with showHelperIcon={false}.

<PhoneInput helperTooltip="We only call about your order." />
<PhoneInput showHelperIcon={false} />

Accessibility

PhoneInput renders a real text input inside an InputGroup. The input uses type="tel", an inputMode for a numeric keyboard, and autoComplete="tel-national".

KeyDescription
TabMoves focus from the country select to the input.
Shift + TabMoves focus backwards through the same order.
EnterSubmits the surrounding <form> when focus is in the input.

The country select follows the select keyboard pattern of the Select component.

ARIA notes:

  • The country select has an accessible name from countryLabel (default "Country"). Translate it in non-English interfaces.
  • Give the input a name with FieldLabel htmlFor, aria-label or aria-labelledby. Do not rely on the placeholder.
  • Flags are decorative and hidden from assistive technology. The select options carry the country name and dial code as text.
  • aria-invalid and error styling follow invalid, directly or from a parent Field.
  • The dial code is a display-only label in front of the input. It is not part of the input's value.

The input sets autoComplete="tel-national".

Styling

Tailwind override: pass className to restyle the group:

<PhoneInput className="w-[320px]" />

Data slots: the component sets these for CSS targeting:

  • data-slot="input-group" on the group.
  • data-slot="phone-dial-code" on the dial code label.
  • data-slot="input-group-select" on the country select.
  • data-slot="input-group-control" on the input.
  • data-slot="input-group-helper-icon" on the helper icon.
  • Input Group: the shell and parts PhoneInput is built from. Use it to build other prefix-and-select fields.
  • Country Flag: renders the flags in the country select.
  • Field: adds the label, description and error footer, and cascades size, invalid and disabled.
  • Credit Card Input and Date Input: other masked fields built on InputGroup.

API Reference

PhoneInput

Extends React.ComponentProps<"input"> minus size and type. Standard attributes such as id, name, placeholder, required and aria-* reach the inner <input>. Seed an uncontrolled field with defaultValue, or control it with value and onValueChange.

Props

PropTypeDefaultDescription
defaultValuestring-E.164 with a leading + (the country is read from it), or plain national digits, which are kept as typed.
valuestring-Controlled value: E.164 (national digits also work). "" clears it.
onChangeReact.ChangeEventHandler<HTMLInputElement>-Native change event with the text typed in the field. For register().
defaultCountryPhoneCountryCode"DE"Seeds the country and resolves an ambiguous calling code such as +1. Ignored once defaultValue carries a country.
countryPhoneCountryCode-Controlled country.
onCountryChange(country: PhoneCountryCode) => void-Fires when the country changes.
onValueChange(value: string, details: PhoneInputDetails) => void-Emits the E.164 string ("" when empty) and details. Fires again when the country changes.
countriesPhoneCountryConfig[]every countrySubset or reorder the dropdown. Build entries from PHONE_COUNTRIES.
countryLabelstring"Country"Accessible name of the country select.
invalidboolean-Marks the field invalid. Inherits from Field.
disabledboolean-Disables the field. Inherits from Field.
loadingboolean-Shows a spinner and sets aria-busy on the group. Inherits from Field.
showHelperIconbooleantrueShows the help icon at the end of the field.
helperIconReactNodequestion mark iconReplaces the help icon.
helperTooltipReactNode"Enter your phone number without the country code."Tooltip text. null keeps the icon without a tooltip.
classNamestring-Tailwind classes merged on the group.
refReact.Ref<HTMLInputElement>-Ref to the inner input.

Variants

VariantOptionsDefaultDescription
size"xs" "sm" "md""sm"Height and type scale. Omit it inside Field; the size comes from there.

PhoneInputDetails

FieldTypeDescription
countryPhoneCountryCodeThe selected country.
dialCodestringThe dial code of that country.
nationalNumberstringThe digits without the dial code.
isPossiblebooleanThe number has a possible length for the country.
isValidbooleanThe number is valid for the country.