Functional Weave
Code in Python

react.form.address-fields@1.0.0

README.md

2,761 bytes · view raw

# react.form.address-fields

A postal address under one legend, following the GOV.UK address pattern: one
text input per line, each with its own label and the autocomplete token that
lets a browser fill it. GB (and the Crown Dependencies GG, JE and IM) get the
UK form; any other country gets an international form with that country's
labels from `form.address` (ZIP code and State for the US, Eircode for
Ireland), and its postcode optional where the country has none.

```tsx
"use client";
import { AddressFields } from "#fune/react.form.address-fields@^1";
import { validateAddress } from "#fune/form.address@^1";

<AddressFields id="address" legend="What is your address?" value={address} onChange={setAddress}
  showErrors={submitted} countries={[{ value: "GB", label: "United Kingdom" }, { value: "US", label: "United States" }]} />

// On the server, the same check (TypeScript, Python or Rust):
const check = validateAddress(submitted); // check.address is trimmed, its postcode normalised
```

| Field | Label | autocomplete |
|-------|-------|--------------|
| line1 | Address line 1 | address-line1 |
| line2 | Address line 2 (optional) | address-line2 |
| town | Town or city | address-level2 |
| region | County (optional), or the country's region label | address-level1 |
| postcode | Postcode, or the country's label; "(optional)" where not required | postal-code |
| country | Country (a select, when `countries` is given; else a hidden input) | country |

- **Errors** come field by field: from `errors` (such as the server's
  `validateAddress(...).errors`), or with `showErrors` from checking the
  current address. Each marks its own input (`aria-invalid`,
  `aria-describedby` to its message) through `react.form.text-field`.
- **The country decides the form.** Changing the country select relabels the
  region and postcode at once. A country code the list does not know still
  renders, with the international labels.
- **Not an address lookup.** Finding the address from a postcode needs Royal
  Mail's PAF or a lookup service, which is network work and yours.

It is a client component. Classes: those of `react.form.fieldset`,
`react.form.text-field` and `react.form.select`, plus `fune-address` on the
wrapper and `fune-field--width-20` / `fune-field--width-10` on the town,
region and postcode fields.

Sources: GOV.UK Design System, "Addresses" pattern
https://design-system.service.gov.uk/patterns/addresses/ ; HTML, autofill
tokens https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#autofill .

## Notices

Portions derived from GOV.UK Frontend (https://github.com/alphagov/govuk-frontend), Copyright (c) 2017 Crown Copyright (Government Digital Service), under the MIT License; the full notice is in NOTICE.