A phone number with its country's flag and calling code. Type it as written at home or with its code; it's handed over as +447700900123.
- 4.8 kB JS 4.4 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 11 tests
This example also uses Button. Install it too:
npx shadcn@latest add checkfu/uicontext/buttonimport { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { Button } from "./ui/uicontext/components/Button.ts"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: space.lg, inlineSize: "min(100%, 24rem)" }, note: { margin: 0, fontSize: font.sm, color: color.muted, overflowWrap: "anywhere" }, value: { fontFamily: font.mono, color: color.fg },})const Message = UiSchema.TaggedEnum({ ChangedPhone: { value: Schema.String, valid: Schema.Boolean }, Submitted: {},})// two-step sign-in asks for a mobile number: type it as written at home, or paste it with its codeexport const MobileNumber = Html.component({ name: "MobileNumber", model: Schema.Struct({ value: Schema.String, valid: Schema.Boolean, tried: Schema.Boolean, sent: Schema.Boolean }), message: Message,}).make({ init: () => ({ value: "", valid: false, tried: false, sent: false }), update: { ChangedPhone: ($, { value, valid }) => $.evolve({ value, valid, sent: false }), Submitted: ($) => $.evolve({ tried: true, sent: $.model.valid }), }, view: (h, model) => { const error = model.tried && !model.valid ? (model.value === "" ? "Enter a mobile number." : "That number is too short or too long for its country.") : undefined return h.form.attrs({ novalidate: "", ...sx(styles.form) }).on("submit", Message.Submitted()).children( FormField(h, { id: "mobile", label: "Mobile number", description: "We'll text a code to it when you sign in.", required: true, ...(error === undefined ? {} : { error }) }, (field) => PhoneInput(h, { key: "mobile", props: { field, country: "GB" }, onEmit: { RequestedChange: ({ value, valid }) => Message.ChangedPhone({ value, valid }) }, })), Button(h, { tone: "primary", type: "submit" }).children("Send code"), h.p.attrs({ role: "status", ...sx(styles.note) }).children( model.sent ? "Code sent to " : "Saved as ", h.span.attrs(sx(styles.value)).children(model.value === "" ? "—" : model.value), ), ) },})When to use
A phone number with its country's flag and calling code. Type it as written at home or with its code; it's handed over as +447700900123. Use Input instead when the number is free text you won't call or text, so no country or format matters.
Usage
import { FormField } from "./ui/uicontext/components/FormControls.ts"import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"export const Phone = <M>(h: Html.H<M>, onChange: (value: string, valid: boolean) => M) => FormField(h, { id: "phone", label: "Phone" }, (field) => PhoneInput(h, { props: { field, country: "US" }, onEmit: { RequestedChange: ({ value, valid }) => onChange(value, valid) } }))Examples
A few countries, a saved number
countries sets which countries are offered and in what order. locale sets the language of their names.
Only for delivery questions.
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { FormField } from "./ui/uicontext/components/FormControls.ts"import { PhoneInput } from "./ui/uicontext/components/PhoneInput.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ grid: { display: "grid", gap: space.lg, inlineSize: "min(100%, 24rem)" } })// a shop that ships to a few countries offers only those, and fills in a saved numberexport const ShippingContact = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.grid)).children( FormField(h, { id: "ship-phone", label: "Phone for the courier", description: "Only for delivery questions." }, (field) => PhoneInput(h, { key: "ship", props: { field, value: "+4915123456789", countries: ["DE", "AT", "CH", "NL", "FR"] } })), FormField(h, { id: "ship-phone-fr", label: "Phone, in French", optional: true }, (field) => PhoneInput(h, { key: "ship-fr", props: { field, country: "FR", locale: "fr", countries: ["FR", "BE", "CH", "LB"] } })), FormField(h, { id: "ship-phone-off", label: "Office line" }, (field) => PhoneInput(h, { key: "office", props: { field, value: "+12125550199", disabled: true } })), )API reference
Props
| Name | Type | Default | Description |
|---|---|---|---|
field | FieldAttrs | — | The attributes a Form Field hands its control: id, description and error wiring. |
label | string | — | The field's name when there's no Form Field: "Mobile number". |
value | string | — | The number to start with, as "+447700900123". |
country | string | "US" | The country picked when the value doesn't say; "US" by default. |
countries | string[] | phoneCountries | The countries offered, by code, in order; all of phoneCountries by default. |
locale | string | — | The language country names are written in; the browser's by default. |
disabled | boolean | — | — |
Events
| Event | Payload | Description |
|---|---|---|
RequestedChange | { value: string; country: string; valid: boolean } | The number changed: as "+447700900123" ("" when empty), its country, and whether it has the right digits. |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/phone-input