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.
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
The command also adds input-group, select, country-flag, tooltip and the usePhoneInput hook.
Usage
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.
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.
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.
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.
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.
Controlling the country
The text value can be controlled with value and onValueChange, as above. The country has its own pair, country and onCountryChange.
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.
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}.
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".
| Key | Description |
|---|---|
Tab | Moves focus from the country select to the input. |
Shift + Tab | Moves focus backwards through the same order. |
Enter | Submits 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-labeloraria-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-invalidand error styling followinvalid, directly or from a parentField.- 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:
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.
Related Components
- Input Group: the shell and parts
PhoneInputis 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,invalidanddisabled. - 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
| Prop | Type | Default | Description |
|---|---|---|---|
defaultValue | string | - | E.164 with a leading + (the country is read from it), or plain national digits, which are kept as typed. |
value | string | - | Controlled value: E.164 (national digits also work). "" clears it. |
onChange | React.ChangeEventHandler<HTMLInputElement> | - | Native change event with the text typed in the field. For register(). |
defaultCountry | PhoneCountryCode | "DE" | Seeds the country and resolves an ambiguous calling code such as +1. Ignored once defaultValue carries a country. |
country | PhoneCountryCode | - | 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. |
countries | PhoneCountryConfig[] | every country | Subset or reorder the dropdown. Build entries from PHONE_COUNTRIES. |
countryLabel | string | "Country" | Accessible name of the country select. |
invalid | boolean | - | Marks the field invalid. Inherits from Field. |
disabled | boolean | - | Disables the field. Inherits from Field. |
loading | boolean | - | Shows a spinner and sets aria-busy on the group. Inherits from Field. |
showHelperIcon | boolean | true | Shows the help icon at the end of the field. |
helperIcon | ReactNode | question mark icon | Replaces the help icon. |
helperTooltip | ReactNode | "Enter your phone number without the country code." | Tooltip text. null keeps the icon without a tooltip. |
className | string | - | Tailwind classes merged on the group. |
ref | React.Ref<HTMLInputElement> | - | Ref to the inner input. |
Variants
| Variant | Options | Default | Description |
|---|---|---|---|
size | "xs" "sm" "md" | "sm" | Height and type scale. Omit it inside Field; the size comes from there. |
PhoneInputDetails
| Field | Type | Description |
|---|---|---|
country | PhoneCountryCode | The selected country. |
dialCode | string | The dial code of that country. |
nationalNumber | string | The digits without the dial code. |
isPossible | boolean | The number has a possible length for the country. |
isValid | boolean | The number is valid for the country. |