Phone field
A phone number with a searchable country picker; the value is international (E.164).
Example
<div class="stack"> <focsd-phone-field label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-phone-field.js';</script>
<focsd-phone-field label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field>import { PhoneField } from '@focsd-dev/react';
export function Example() { return <PhoneField onChange={(e) => console.log(e.target)} label="Mobile" default-country="GB" preferred="GB,US,IE"></PhoneField>;}<script setup>import '@focsd-dev/components/focsd-phone-field.js';</script>
<template> <focsd-phone-field @change="onChange" label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-phone-field.js';
// template<focsd-phone-field (change)="onChange($event)" label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field><script> import '@focsd-dev/components/focsd-phone-field.js';</script>
<focsd-phone-field onchange={(e) => console.log(e.target)} label="Mobile" default-country="GB" preferred="GB,US,IE"></focsd-phone-field>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | — |
country | country | string | — |
default-country | defaultCountry | string | — |
countries | countries | string | — |
preferred | preferred | string | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
checkPhone() | Check the number: missing (with required), rejected by validator, or not a valid number for the country. Returns null when it passes. |
requestValidation() | Re-run validation, for example after changing validator. |
Events
| Event | Description |
|---|---|
input | Fired when the number or the country changes. |
change | Fired when the person commits a change. |
CSS parts
Style the inside of the component with ::part(), for example focsd-phone-field::part(label).
| Part | Description |
|---|---|
label | The label. |
layout | The label and the body (see label position). |
body | The field with its example, hint and message. |
hint | The author hint. |
message | The shown error, warning or success message. |
message-icon | The icon of the message. |
field | The group holding both inputs. |
flag | The flag of the chosen country. |
country | The country combobox. |
popup | The country list popup. |
listbox | The country list. |
option | One country. |
control | The number input. |
example | The example number. |
status | The live status (visually hidden). |
announcer | The visually hidden status region that announces a new message. |
Accessibility
- One
role="group"named by the label holds a "Country" combobox and atype="tel"input. - The tel input is described by your
hint, then an example number for the chosen country, then the shown message, and usesautocomplete="tel-national". Style the example with::part(example);::part(hint)is your hint, as on every control. - Typing or pasting a number with
+or00switches the country, announced in a polite status. - Invalid numbers report "Enter a valid United Kingdom phone number"; pass
validatorfor stricter checks. - The value and the form entry are E.164 (
+442071838750), so servers never parse local formats.
Keyboard
| Key | Action |
|---|---|
| Type in Country | Filter by name, code or dial code (+353) |
| Down / Up | Move through countries |
| Home / End (open list) | First / last country |
| Enter | Choose the country |
| Escape | Close the list and keep the country |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.