Skip to content
EspañolEnglish
All components (283)

Address

Fields · AddressField

What it is and when to use it

A postal address in one block: street and number, apartment, postal code and city. In Spain the province is not asked for: it comes from the first two digits of the postal code and is written below it ("Madrid"), so a 28001 in Seville cannot get through. With countries the country is chosen and the fields change with it: every country (252 territories) with its order, the name of its region and the shape of its postal code. ADDRESS_COUNTRIES brings them all.

When to use it

  • Shipping and billing at checkout, a customer record, a store address.
  • value and onChange carry the whole address (EMPTY_ADDRESS to start blank).
  • section="billing" for the billing one: the browser tells the two apart and does not fill one with the other.
  • addressErrors(value) says what is missing before moving on; errors shows the server's, which take precedence over the local ones.
  • countries={['ES', 'PT', 'FR', 'US']} when shipping to several countries: city, state and ZIP in the United States; postal code and city in France; neighbourhood or colonia in Mexico. The chosen country goes in value.country.

When not to

  • An address that is not postal (coordinates, a pickup point): a Select of points or a map.

Keyboard: Tab goes through the four fields. Each one is checked when you leave it, not on every keystroke. The postal code stays at five digits even if something else is pasted.

Accessibility: it is a fieldset with its legend (the label, "Shipping address" by default), and each field has its own. Street, postal code and city are required (aria-required); the apartment field carries "Optional" as its hint. A code that is not from Spain says so in those words. The autocomplete values are the ones from the specification with their section (shipping address-line1, shipping postal-code…): with them, autofill resolves half the form.

Fine-tuning: no variables of its own: the fields use --amset-field-* and the block is adjusted with the classes amset-address__title, amset-address__row and amset-address__cp.

What it looks like

Shipping address
Optional
Madrid

<AddressField
  label="Shipping address"
  section="shipping"
  value={direccion}
  onChange={setDireccion}
/>

Props

Import it with import { AddressField } from '@amset/ui'. Required props are marked.

PropTypeWhat it does
onChangerequired(address: PostalAddress) => void—
valuerequiredPostalAddress—
classNamestringMerged with the library classes; it does not replace them.
countriesreadonly string[]The countries to choose from, by ISO code (`['ES', 'PT', 'FR']`). With more than one, a country picker appears first and the fields change with it: postal code and city in France; city, state and ZIP in the United States. The chosen country goes in `value.country`. Without this, the address is from `value.country`, or Spain.
disabledboolean—
errorsAddressErrorsExternal errors (the server's). They take precedence over the ones computed here.
labelstringTitle of the block. It is the legend of the group of fields.
section"shipping" | "billing"Prefix of the `autocomplete` values: `shipping` or `billing`. With it, the browser tells the shipping address from the billing one and does not fill one with the other.
size"sm" | "md" | "lg"—
suggestAddressSuggesterAn address search (that of your map provider or your server). With it, the street proposes addresses from three letters and choosing one fills all the fields. It receives what was typed and a signal to cancel the old request. Without it, the street is a normal text field with the browser's autofill.
variantFieldVariant—

It also accepts the attributes of the element it renders: className, style, data-*, aria-* and its events.

In the test bench

Every state and variant, to try one at a time, on its page at storybook.amsetui.com: