# Phone field

Source: https://ecosphere.dev/docs/forms/phone-field (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

A dial code and a number in one box: one border, one focus ring.

`import { EcoPhoneField } from "vue-ecosphere"`

## Phone

Open the dial code with a click or ArrowDown; Escape closes it.

Vue (vue-ecosphere):

```vue
<EcoPhoneField v-model="mobile" v-model:code="mobileCode" :codes="codes"
  label="Mobile" hint="We only use it for delivery alerts." first />
```

React (react-ecosphere):

```tsx
const [mobile, setMobile] = useState("98765 43210");
const [mobileCode, setMobileCode] = useState("+91");

<EcoPhoneField value={mobile} onChange={setMobile} code={mobileCode} onCodeChange={setMobileCode}
  codes={codes} label="Mobile" hint="We only use it for delivery alerts." first />
```

## Empty

Vue (vue-ecosphere):

```vue
<EcoPhoneField v-model="work" v-model:code="workCode" :codes="codes"
  label="Work phone" placeholder="(555) 000-0000" first />
```

React (react-ecosphere):

```tsx
const [work, setWork] = useState("");
const [workCode, setWorkCode] = useState("+1");

<EcoPhoneField value={work} onChange={setWork} code={workCode} onCodeChange={setWorkCode}
  codes={codes} label="Work phone" placeholder="(555) 000-0000" first />
```

## Not a number

Type the missing digits to clear the error.

Vue (vue-ecosphere):

```vue
<EcoPhoneField v-model="uk" v-model:code="ukCode" :codes="codes" label="Mobile"
  :error="uk.replace(/\D/g, '').length === 10 ? undefined : 'A UK mobile has ten digits after the dial code.'" first />
```

React (react-ecosphere):

```tsx
<EcoPhoneField value={uk} onChange={setUk} code={ukCode} onCodeChange={setUkCode} codes={codes} label="Mobile"
  error={uk.replace(/\D/g, "").length === 10 ? undefined : "A UK mobile has ten digits after the dial code."} first />
```

## Fixed dial code

Without codes the dial code is text: nothing to tab to or click, and it is read with the number.

Vue (vue-ecosphere):

```vue
<EcoPhoneField v-model="local" code="+91" label="Mobile"
  hint="Numbers in India only." first />
```

React (react-ecosphere):

```tsx
<EcoPhoneField value={local} onChange={setLocal} code="+91" label="Mobile"
  hint="Numbers in India only." first />
```

## Disabled

Vue (vue-ecosphere):

```vue
<EcoPhoneField model-value="98765 43210" code="+91" label="Mobile" disabled
  hint="Verified numbers are changed from account settings." first />
```

React (react-ecosphere):

```tsx
<EcoPhoneField value="98765 43210" code="+91" label="Mobile" disabled
  hint="Verified numbers are changed from account settings." first />
```

## API

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `string` | "" | The number, kept as typed. |
| `v-model:code` | `string` | "+1" | The dial code. Default it from the account's country. |
| `codes` | `{ code: string; label?: string }[]` | [] | Dial codes in the menu. Empty: the code is plain text, not a button, and describes the number. |
| `error` | `string` | — | Turns the whole phonefield; say what a valid number looks like there. |
| `label / required / hint / invalid / first / disabled / placeholder / id` | `—` | — | Field props. |
| `numberLabel` | `string` | "Phone number" | Accessible name of the number when there is no label. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `value / onChange` | `string` | "" | The number, kept as typed. |
| `code / onCodeChange` | `string` | "+1" | The dial code (defaultCode when uncontrolled). Default it from the account's country. |
| `codes` | `{ code: string; label?: string }[]` | [] | Dial codes in the menu. Empty: the code is plain text, not a button, and describes the number. |
| `error` | `string` | — | Turns the whole phonefield; say what a valid number looks like there. |
| `label / required / hint / invalid / first / disabled / placeholder / id` | `—` | — | Field props. The ref goes to the number input. |
| `numberLabel` | `string` | "Phone number" | Accessible name of the number when there is no label. |
