Address Input

A postal address whose shape follows the country. A US form asks for a State and a ZIP; a UK one asks for a County and a Postcode; Japan puts the postal code first. Shipping the US layout everywhere is how forms reject valid addresses.

src/components/ui/address-input.tsx

About

From the component's own source, where it was written.

A structured postal address whose shape follows the selected country.

The country comes first and everything below it re-labels. A US form asks for a State and a ZIP code; a UK one asks for a County and a Postcode and treats the region as optional; Japan puts the postal code above the prefecture. Shipping the US layout to everyone is how forms end up rejecting valid addresses.

Nothing is validated against a format table beyond requiredness. Postal code regexes are famously wrong — Irish Eircodes, BFPO addresses, new UK districts — and a form that refuses a real address is worse than one that accepts a typo the courier will query.

autoComplete tokens are set on every field. The browser can fill a whole address from one tap, and it only does so when the tokens are right.

Install

npx astralyx-ui add address-input

Usage

import { AddressInput } from '@/components/ui/address-input'

<AddressInput value={address} onValueChange={setAddress} />

Composer

Change the props and the source below follows.

Props
tsx
<AddressInput value={address} onValueChange={setAddress} />

Examples

Country-aware fields

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

AddressInput

Extends Omit<ComponentProps<'div'>, 'onChange' | 'value'>.

PropTypeDefaultDescription
valueAddressValueEMPTY{ country, line1, line2, city, region, postalCode }. Structured, not a single textarea, so it can be validated and shipped.
onValueChange(value: AddressValue) => void——
countriesstring[]COUNTRIES—
size'sm' | 'default''default'—
disabledboolean——
countryLabelReactNode'Country'—
countryPlaceholderstring'Select country…'—
countrySearchPlaceholderstring'Search countries…'—
addressLabelstring'Address'—
addressLine2Labelstring'Apartment, suite, etc.'—
cityLabelstring'City'—
optionalLabelReactNode'(optional)'Suffix marking a non-required field.
autoCompleteset on every field—Which is the only reason a browser can fill the whole address from one tap.

Functions

FunctionSignatureDescription
addressRulesRecord<string, CountryRules>—
emptyAddressAddressValue—

Types

AddressValue

A structured postal address whose shape follows the selected country.

FieldTypeDescription
countrystringRegion and postal-code labels, requiredness and field order all follow the selected country. Hong Kong and the UAE have no postal codes at all.
line1string—
line2string—
citystring—
regionstring—
postalCodestring—

Behaviour

TopicDetailDescription
validationrequiredness onlyNo postal-code regexes. They are famously wrong — Eircodes, BFPO, new UK districts — and a form that refuses a real address is worse than one that accepts a typo.