# Phone Input

> Source: https://design.nanatec.co.ke/components/phone-input/
> A telephone field with a country picker that groups the number as it is typed and submits it in international form.

The phone input takes a mobile or landline number with its country. The country picker
shows the flag and dialling code, Kenya by default, and the number is grouped as the
country writes it while people type: `712 345 678`. Whatever is typed or pasted, the value
is the international form, `+254712345678`, ready to store or send an SMS to.

## Preview

## Installation

```html
<nana-phone-input label="Phone number" name="phone" required></nana-phone-input>

<script type="module">
  const phone = document.querySelector('nana-phone-input');
  phone.addEventListener('nana-change', (event) => {
    console.log(event.detail.value, event.detail.country, event.detail.complete);
  });
</script>
```

The element takes part in its form: it submits `value` under `name`, resets with the
form, and is disabled with a disabled fieldset. `nana-input` fires while typing and
`nana-change` when the field is left or the country changes.

Set `value` to show a stored number; its dialling code picks the country. Set `country` to
an ISO code, such as `UG`, to start an empty field elsewhere.

## Typing and pasting

- A leading `0`, the trunk prefix most countries dial at home, is dropped:
  `0712 345 678` becomes `712 345 678`.
- A pasted number starting with `+` or `00` switches to its country:
  `+256 772 123 456` picks Uganda.
- A number typed with its dialling code but no `+`, such as `254712345678`, loses the code.
- Anything that is not a digit is ignored, and typing stops at the country's length.
- Editing in the middle keeps the caret after the digit it followed.

## Countries

The list starts with East Africa, then the rest of Africa and the countries most often
called from it. Each country knows its dialling code, how its numbers are grouped, and how
many digits they have. Pass `countries` to offer your own list, built from the same shape:

```ts
import { PHONE_COUNTRIES } from '@nana-tec/ui-core/phone';

const eastAfrica = PHONE_COUNTRIES.filter((country) =>
  ['KE', 'UG', 'TZ', 'RW', 'BI', 'SS'].includes(country.code),
);
```

## Validation

A number shorter than its country's numbers blocks the form, and once the field is left it
shows "Enter a complete phone number". With `required`, an empty field blocks the form too.
For any other problem, such as a number already in use, set `invalid` with your own
`errorMessage`.

## Accessibility

- The number field is a `tel` input with `inputmode="tel"`, so phones show the dial pad,
  and `autocomplete="tel-national"`, so browsers can fill it.
- The country picker is a native select named "Country code", with each option read as
  "Kenya (+254)". The flag and code drawn over it are hidden from screen readers.
- The help text describes the field; an error replaces it and is announced.

## API
