# PhoneInput

A telephone field that pairs a searchable country dial-code selector with a national number input inside one unified raised field box. Typing filters countries by name, ISO code or dial code; submitting with name sends the combined dial-prefixed number.

**Dependencies:** npm: 2 direct, 3 total; registry: 4 direct, 4 total.

## Usage

### When to use

- Collect an international phone number with a country dial code in checkout, verification or profile forms.
- Pair a searchable country picker directly beside the national phone number inside one unified field.

### When not to use

- Collect a single fixed-country number without a country code picker. Use Input with type tel.
- Enter an adjustable quantity or amount. Use NumberInput.

## Variants

### size

Field height from size-element-*: sm 28px, md 32px (default), lg 36px, xl 44px on mouse.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

## Examples

### Checkout contact

```tsx
<Field className="w-full max-w-sm">
  <FieldLabel>Phone number</FieldLabel>
  <FieldControl>
    <PhoneInput defaultCountry="US" placeholder="(415) 555-0192" />
  </FieldControl>
  <FieldDescription>Used only for delivery updates.</FieldDescription>
</Field>
```

### Restricted regions

Limit the country selector to supported shipping regions with countries.

```tsx
<Field className="w-full max-w-sm">
  <FieldLabel>Delivery phone</FieldLabel>
  <FieldControl>
    <PhoneInput defaultCountry="VN" countries={["VN", "SG", "TH", "JP", "US"]} placeholder="901 234 567" />
  </FieldControl>
</Field>
```

### Invalid

```tsx
<Field className="w-full max-w-sm">
  <FieldLabel>Phone number</FieldLabel>
  <FieldControl>
    <PhoneInput defaultCountry="US" defaultValue="123" />
  </FieldControl>
  <FieldError>Enter a valid phone number.</FieldError>
</Field>
```

## Do and don't

### Do

- Pair PhoneInput with a visible FieldLabel inside Field so the telephone input has an accessible name and linked error text.
- Use defaultCountry to preselect the expected region while allowing users to switch countries.
- Use size lg or xl on touch-first checkout screens for comfortable tapping.

### Don't

- Don't strip or block formatting characters like spaces, dashes or parentheses while the user types.
- Don't use NumberInput for phone numbers; phone numbers are identifiers, not quantities.
- Don't leave out a visible label; placeholder text disappears as soon as typing begins.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `string` |  |  | Controlled national phone number value. |
| `defaultValue` | `string` |  | `` | Initial national phone number value when uncontrolled. |
| `onValueChange` | `function` |  |  | Called with the national phone number string when the input changes. |
| `country` | `string` |  |  | Controlled ISO 3166-1 alpha-2 country code (uppercase or lowercase), such as "US" or "VN". |
| `defaultCountry` | `string` |  | `US` | Initial ISO 3166-1 alpha-2 country code when uncontrolled. |
| `onCountryChange` | `function` |  |  | Called with the uppercase ISO country code when the user selects another country. |
| `countries` | `readonly string[]` |  |  | Optional allowlist of ISO 3166-1 alpha-2 country codes to display in the country list. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Field height from size-element-*: sm 28px, md 32px (default), lg 36px, xl 44px on mouse. |
| `placeholder` | `string` |  |  | Hint text shown while the telephone input is empty, such as "(415) 555-0192". |
| `disabled` | `boolean` |  | `false` | Disables both the country selector button and the telephone input. |
| `readOnly` | `boolean` |  | `false` | Keeps the telephone number readable and focusable while blocking edits and country changes. |
| `invalid` | `boolean` |  | `false` | Sets aria-invalid on the telephone input and draws the danger border on the field box. |
| `required` | `boolean` |  | `false` | Sets the native required attribute on the telephone input. |
| `name` | `string` |  |  | Form field name; submits the combined dial-prefixed phone number via a hidden input. |
| `labels` | `PhoneInputLabels` |  |  | Localized labels for the country selector button, search placeholder and empty search state. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | PhoneInput: the field group and the anchored country picker layer. |
| `group` | The unified raised field box: border-border-strong, bg-background-raised, shadow-low, radius-element and size-element-* heights, showing focus, disabled and invalid states through :has(). |
| `trigger` | The leading country selector button showing the 4:3 flag, dial code and chevron, separated from the text input by a border-border divider. |
| `hitArea` | The ::before touch-target extension on the country selector button up to size-target-min. |
| `input` | The national telephone input (type="tel", inputMode="tel") filling the rest of the field box. |
| `placeholder` | Placeholder text inside the telephone input in secondary text color. |
| `content` | The searchable country picker panel anchored under the field box on a raised surface with shadow-med. |
| `item` | A country option row in the listbox with flag, country name and dial code. |
| `indicator` | The check mark shown at the end of the currently selected country option. |
| `empty` | The status message shown when no country matches the search query. |

## Accessibility

Role: `textbox`

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus between the country selector button and the telephone input. |
| `Enter, Space, ArrowDown` | On the country selector button, opens the searchable country list and focuses its search field. |
| `ArrowDown, ArrowUp` | Inside the open country list, moves the highlight through matching countries. |
| `Enter` | Picks the highlighted country, closes the list and moves focus to the telephone input. |
| `Escape` | Closes the country list and returns focus to the country selector button. |

- Always pair with a visible FieldLabel or aria-label so the telephone input has an accessible name.
- The phone input uses type="tel", inputMode="tel", autoComplete="tel-national" and font-size-input (16px on touch) so mobile browsers do not zoom on focus.
- The country trigger extends its touch target to size-target-min via ::before while the native input keeps its full row height.
- With name, a hidden input submits the full dial-prefixed phone number.

## Install

```bash
npx werewolf-ui add https://www.werewolf-ui.dev/r/phone-input.json
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.
