Address Input
Address fields that follow each country's field order, labels and postal code rules, with browser autofill.
Try it: switch to Japan or the United Kingdom and watch the fields reorder, or type a four digit ZIP code and leave the field.
On this page
Installation
The CLI also installs the npm packages and registry items it builds on, and reads the style in your components.json to pick the Base UI or Radix build.
Usage
"use client"
import * as React from "react"
import { AddressInput, type Address } from "@/components/ui/address-input"
export function ShippingAddress() {
const [address, setAddress] = React.useState<Address>({
country: "DE",
line1: "",
line2: "",
city: "",
region: "",
postalCode: "",
})
return (
<AddressInput
aria-label="Shipping address"
addressType="shipping"
value={address}
onValueChange={setAddress}
/>
)
}Examples
Field order by country
defaultCountry picks the starting layout. Japan starts with the postal code and asks for the prefecture, Germany puts the postal code before the city, and the United Kingdom has no state field.
Limit the country list
countries lists only these, in this order, for a store that ships to a few countries. If the current country is not in the list, it is added at the top. Without countries, every country is listed by name.
Add a country layout
Countries without a built-in layout get a generic one. Pass layouts to give one its own field order, required fields, labels and postal code pattern, here Brazil's CEP.
In a form
name submits each field as name.field, and addressType="billing" scopes the autofill tokens so a page with shipping and billing addresses fills each one correctly. A bad postal code blocks submit and shows its message.
API reference
AddressInput
A country select followed by the address fields that country uses, in its order and with its labels, such as State and ZIP code for the US or Prefecture first after the postal code for Japan. Tab moves through the fields in layout order, and the country is a native select, so arrow keys and typing a name pick a country. A filled-in postal code that fails its layout pattern shows an error when the field loses focus or a form is submitted, linked to the field with aria-describedby. An empty required postal code gets only the native required check.
| Prop | Type | Default |
|---|---|---|
value{ country: string; line1: string; line2: string; city: string; region: string; postalCode: string }Controlled address. | { country: string; line1: string; line2: string; city: string; region: string; postalCode: string } | |
defaultValuePartial<Address>Defaultan empty addressInitial value when uncontrolled. | Partial<Address> | an empty address |
onValueChange(address: Address) => voidCalled on every edit, and when the country changes, with fields the new layout lacks cleared. | (address: Address) => void | |
defaultCountrystringDefault"US"ISO 3166-1 alpha-2 country used when defaultValue has none. | string | "US" |
countriesstring[]Defaultevery countryCountry codes to offer, in display order. The current country is always listed. | string[] | every country |
layoutsAddressLayoutsLayouts by country code: rows (field order, grouped into rows), required, labels and postalCode ({ pattern, format }). They override the built-in layouts for AU, CA, DE, FR, GB, IN, JP and US and replace the generic layout for other countries. | AddressLayouts | |
localestringDefault"en"Locale for country names. | string | "en" |
addressType"shipping" | "billing"Prefixes every autocomplete token, such as "shipping postal-code", so a page with two addresses autofills each one. | "shipping" | "billing" | |
namestringSubmits each field with a form as name.country, name.line1, name.line2, name.city, name.region and name.postalCode. | string | |
disabledbooleanDisables the country select and every field. | boolean | |
classNamestringClasses for the group wrapper. | string |
Other props go to the <div role="group">.