Phone Input
An international phone number: a country, searched by name, code or dial code, and the number, formatted as you type — its value the whole number in E.164. On libphonenumber-js.
Installation
It has its own entry point, @semantic-components/ui/phone-input, built on libphonenumber-js — an optional peer of ui: install it where you use the component.
import { ScPhoneInput, ScPhoneInputCountry, ScPhoneInputFlag, ScPhoneInputNumber, phoneNumber } from '@semantic-components/ui/phone-input';Flags
The flags are country-flag-icons' SVGs, served with your app — no CDN. Install it, copy them into your assets, and say where they are with withPhoneInput(); only the flags shown are loaded. Without it, the phone input works with no flags.
// angular.json (or project.json): the flags, in the assets{ "glob": "*.svg", "input": "node_modules/country-flag-icons/3x2", "output": "flags"}import { withPhoneInput } from '@semantic-components/ui/phone-input';
provideSemanticComponents( { preset: { style } }, withPhoneInput({ flagUrl: '/flags/{XX}.svg' }),);Usage
Usage
Value: —
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup, ScComboboxPortal,Anatomy
The group, the picker — your scCombobox, its list from results() — and the number.
import { ScPhoneInput, ScPhoneInputCountry, ScPhoneInputFlag, ScPhoneInputNumber } from '@semantic-components/ui/phone-input';<div scPhoneInput #phone="scPhoneInput" [formField]="form.phone"> <div scCombobox scPhoneInputCountry #country="scPhoneInputCountry"> <button scComboboxTrigger variant="ghost"> <img scPhoneInputFlag alt="" /> +{{ phone.callingCode() }} … </button> … @for (item of country.results(); track item.code) { <div scComboboxItem [value]="item.code">…</div> } </div> <input scPhoneInputNumber /></div>Country and number
- The value is the whole number, E.164 (
+33612345678), empty when there is none. The dial code shows in the picker, apart from the number: it can't be deleted. A number typed or pasted with+chooses its country. - The list: every country with a numbering plan, named in the app's language (
LOCALE_ID) —countriesto keep only some,preferredto put some first. Typed in its search: a name (accents ignored), its initials, the code (fr) or the dial code (+33). - The flags are your app's: country-flag-icons' SVGs copied by your build's assets, and their address given once —
withPhoneInput({ flagUrl: '/flags/{XX}.svg' }). None set, no flag. - Validation:
phoneNumber()checks the length fits the country (numbering plans change: a stricter check can refuse real numbers);strict, the digits too.
Accessibility
- The number is named by its field's label; name the picker ("Country") — its button reads that and the dial code.
- The number reads left to right in any language (
dir="ltr"): its groups stay in order. - The list is the Combobox's: arrows, typing to search, Enter to choose, Escape to close.
Examples
Preferred countries
Value: —
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup, ScComboboxPortal,Only some countries
Value: —
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup, ScComboboxPortal,Example placeholder
Value: —
import { Component, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup, ScComboboxPortal,Form
import { Component, ViewEncapsulation, signal } from '@angular/core';import { FormField, FormRoot, form, required } from '@angular/forms/signals';import { ScButton, ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate,Disabled and read-only
import { Component, ViewEncapsulation } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup, ScComboboxPortal,RTL
import { Dir } from '@angular/cdk/bidi';import { Component, LOCALE_ID, ViewEncapsulation, signal } from '@angular/core';import { ScCombobox, ScComboboxEmpty, ScComboboxInput, ScComboboxItem, ScComboboxList, ScComboboxListTemplate, ScComboboxPopup,API reference
From @semantic-components/ui/phone-input. Each part takes class, merged over its own (cn: a later class wins).
| Part | Selector | Description |
|---|---|---|
ScPhoneInput | div[scPhoneInput] | The group: the value (E.164), the country. exportAs: callingCode(), text(), focus(). |
ScPhoneInputCountry | div[scCombobox][scPhoneInputCountry] | On your combobox: the country both ways. exportAs: results(), all(). |
ScPhoneInputNumber | input[scPhoneInputNumber] | The number, formatted as you type; examples for a placeholder. |
ScPhoneInputFlag | img[scPhoneInputFlag] | A flag, from withPhoneInput({ flagUrl }). |
| Input | Type | Default | Description |
|---|---|---|---|
value | string | '' | Two-way, or [formField]: E.164. |
country | ScPhoneCountryCode | undefined | undefined | Two-way: the country chosen. |
defaultCountry | ScPhoneCountryCode | — | The country to start with. |
disabled, readonly, required, invalid, touched | boolean | false | Bound by [formField]. |
| Input | Type | Default | Description |
|---|---|---|---|
countries | ScPhoneCountryCode[] | null | null | Only these; all, by default. |
preferred | ScPhoneCountryCode[] | [] | These first, in this order. |