|Kit

Phone Input

A phone number field with a searchable country picker — it formats as the user types and hands back one E.164 value, on the shared input shell.

PhoneInput is a phone number field with a country picker in front of it. It formats as the user types — 06 12 34 56 78 in France, (201) 555-0123 in the US — and reports one canonical value, the E.164 string ("+33612345678"), which is what a backend or an SMS provider expects.

'use client';
import { PhoneInput } from '@42-central/ui-react/phone-input';

<PhoneInput label="Phone" defaultCountry="FR" />

Formatting and validation come from libphonenumber-js, and the flags from country-flag-icons. The flags load in one lazy chunk on first use, so pages without a phone field never pay for them.

Playground

Component

Loading preview

Props

Code

<Component
  label="Phone"
  size="md"
  defaultCountry="FR"
/>

Value

onChange(value, details) hands back the E.164 value, or null while there is no number. details says which country the number belongs to and whether it is complete and valid. value / defaultValue take the same E.164 string and show it in its country's national format.

+33612345678

validFR

Code

const [value, setValue] = useState<string | null>("+33612345678");

<PhoneInput
  label="Phone"
  value={value}
  onChange={(next, { country, isValid }) => setValue(next)}
/>

Separators are free-form: spaces, dots, dashes and parentheses all fold into the country's own grouping.

Typing the dial code

The dial code behaves like a token in front of the number. Typing a complete code moves it into the country trigger, and Backspace at the very start of the number takes it back out, so the whole number stays editable from the keyboard:

KeysTriggerField
+ 3🌐+3
3🇫🇷 +33(empty)
6 1 2🇫🇷 +336 12
Backspace at the start🌐+33 6 12
  • On an empty field, Backspace is the exact inverse of the last keystroke: 🇫🇷 with nothing typed goes back to +3.
  • With a number after it, the code comes back whole, caret after it, and nothing is deleted. Deleting its last digit would leave +36 12, a Hungarian number nobody typed.
  • While the caret is inside the code, it stays text. It collapses again once the code is finished (typing a 3 back, or a 2 for Belgium).
  • Pasting +44 20 7946 0958 lands as 🇬🇧 + 20 7946 0958 in one go.
  • A code shared by several countries resolves to its main one, and the number refines it: +1 shows 🇺🇸, then 204 … (a Canadian area code) switches to 🇨🇦. A country the user picked, or passed as defaultCountry, is never overridden that way.

Country

The country defaults to the region of the active locale (fr-FR gives France, see IntlProvider). defaultCountry pins it, and country / onCountryChange control it. With the locale on "auto", pass defaultCountry so the server and the first client render agree.

The list is searchable by name or dial code — typing 33 finds France — and the names are in the locale's language.

Preferred countries

preferredCountries are pinned at the top of the list, under their own heading.

Allowlist

countries limits what the list offers. A number from another country still formats, so the user sees what they typed, but details.isValid is false.

No country

defaultCountry={null} starts with no country: the trigger shows a globe until the user types a + and a dial code, which then moves into the trigger.

Placeholder

By default the placeholder is an example number of the selected country, written the way its people write it: 06 12 34 56 78 for France, 07400 123456 for the UK, (201) 555-0123 for the US. It follows the country, including one resolved from a typed +44, and there's none while no country is selected.

A fixed string would contradict the field as soon as the country changes (+33 … next to a selected UK). To word it yourself, pass a function of the country; what it returns is used as-is. placeholder="" turns it off.

<PhoneInput placeholder={(country) => (country ? 'Mobile number' : '+ dial code and number')} />

Validation

The field never turns red on its own: flagging a number as wrong while it's still being typed is noise. Read details.isValid, or validate in your schema with the same engine through isValidPhoneNumber, and pass error when the user is done — on blur, or on submit.

Code

const [valid, setValid] = useState(true);
const [touched, setTouched] = useState(false);

<PhoneInput
  label="Phone"
  defaultCountry="FR"
  onChange={(_, { isValid }) => setValid(isValid)}
  onBlur={() => setTouched(true)}
  error={touched && !valid ? "Enter a complete phone number" : undefined}
/>
import { isValidPhoneNumber } from '@42-central/ui-react/phone-input';
import { z } from 'zod';

const schema = z.object({
  phone: z.string().refine((value) => isValidPhoneNumber(value), 'Enter a complete phone number'),
});

Forms

name submits the E.164 value with a native <form>. With react-hook-form, bind it through Controller — the value is not a DOM string, so register doesn't fit:

<Controller
  name="phone"
  control={control}
  render={({ field, fieldState }) => (
    <PhoneInput
      label="Phone"
      value={field.value ?? null}
      onChange={field.onChange}
      onBlur={field.onBlur}
      ref={field.ref}
      error={fieldState.error?.message}
    />
  )}
/>

Sizes

Five heights — xs through xl — matching the rest of the family. The flag and dial code scale with the text.

Code

<PhoneInput size="xs" defaultValue="+33612345678" />
<PhoneInput size="sm" defaultValue="+33612345678" />
<PhoneInput size="md" defaultValue="+33612345678" />
<PhoneInput size="lg" defaultValue="+33612345678" />
<PhoneInput size="xl" defaultValue="+33612345678" />

Error & disabled

This number is incomplete

Custom flags

renderFlag draws each flag yourself — your own sprite, or emoji. Emoji flags cost nothing, but Windows shows them as two letters (FR), which is why the kit ships SVGs.

Code

<PhoneInput
  defaultCountry="FR"
  renderFlag={(code) => <span aria-hidden>{toEmoji(code)}</span>}
/>

Right to left

A phone number reads left to right in every script, so the number itself stays dir="ltr"; under an RTL locale it is right-aligned, and the country picker moves to the inline-start (right) edge with the rest of the shell.

Translations

translations covers the country picker's wording. Unset keys keep their English default.

<PhoneInput
  translations={{
    triggerLabel: (country) => (country ? `Pays : ${country}` : 'Pays : aucun'),
    searchPlaceholder: 'Rechercher un pays',
    empty: 'Aucun pays trouvé',
    preferredLabel: 'Suggestions',
    allLabel: 'Tous les pays',
  }}
/>

API

Prop

Type

Changelog

v0.30.0
  • Controls with no label or Field now dim in a disabled Fieldset and turn red in an invalid one

    #493
v0.29.0
  • New PhoneInput: a phone number field with a searchable country picker, as-you-type formatting and an E.164 value

    #463

On this page