Skip to content

Address ​

Address-book component covering three shapes in one element: an address editor, a selectable address list with a default badge, and a contact-only mode (name + phone, not a separate component).

Basic usage ​

mode defaults to edit. Region data is host-injected through regions; the component ships no administrative-division dataset (see “Region data contract” below).

Address editor

Address list ​

mode="list" renders address cards: native radios (keyboard accessible), a default badge and a selected highlight. When selected-id is absent, the isDefault item is selected, otherwise the first item.

Select a shipping address

Selected: a1

Row edit and delete ​

Every address card carries an inline "Edit" button that emits oas-edit (detail { id, item }) so the host can switch to the editor and prefill the form; with show-delete present a "Delete" button is added, emitting oas-delete (detail { id, item }). The delete confirmation is composed by the host: wrap the delete entry in an oas-popconfirm and mutate items after confirmation (the component ships no built-in confirm dialog — deletion is part of the host's business flow, and confirm copy and undo policies differ per product):

Row edit + delete (popconfirm recipe)

Click the row "Edit" / "Delete" buttons to see the events

Host-side confirm recipe (pseudo code):

html
<!-- oas-delete only reports intent; the confirmation interaction is composed by the host with oas-popconfirm -->
<oas-popconfirm content="Delete this address?">
  <oas-button slot="reference" danger>Delete</oas-button>
</oas-popconfirm>

The inline delete button emits oas-delete immediately; whether to confirm first and whether to actually delete are both decided by the host in the event handler (the dialog belongs to the host, and so does the items mutation).

Saving / deleting busy states ​

saving / deleting express that the host is submitting / deleting: the save button or the delete buttons are disabled with aria-busy + spinner; input channels stay as they are (the host decides whether to lock the form). Both attributes are orthogonal to disabled — remove them when the busy state ends:

saving / deleting busy states

Custom validation via validator ​

validator is a function property (an attribute cannot carry a function; assign it with el.validator = fn): signature (key, value) => string | null. Returning an error string marks the field invalid — the error is merged into the oas-invalid fields array and rendered under the matching field; empty string / null / undefined means valid. key is the field name (name / phone / detail / province / city / district). Built-in checks (required / phone format / region completeness) run first, the custom hook runs after. A throwing hook does not block submission (warns once in dev and is treated as "no error"):

validator blocking a phone number

Click save to trigger the custom validation

Region codes via value-format ​

value-format="name" (default) outputs region names; value-format="code" outputs region codes — in the address object carried by oas-submit and oas-change, province / city / district become the code fields from the injected regions (nodes without code fall back to the name); prefill matches by code as well. The data contract stays the host-injected regions (name required, code optional); the component ships no administrative-division dataset:

value-format=code

Contact-only mode ​

mode="contact" renders only the name and phone fields (order contact, invoice recipient and similar cases). Typing emits oas-change; region, street address and the save button are not rendered.

Contact details

Preselected address ​

selected-id controls the selected item. When the selected item is removed from items, the component falls back to the isDefault / first item and emits oas-select so the host can follow.

Second item preselected

Validation ​

Saving validates: name required, phone required and well-formed, all three region levels required when region data is injected, and street address required. Failure emits oas-invalid (detail { fields }) and focuses the first invalid field.

Validation feedback

Waiting for submit

Region data contract ​

The component does not bundle region data; regions is the only contract (JSON array, up to three levels). A full county-level dataset is large and changes often: bundling it would make the on-demand bundle size unpredictable and its data version untraceable. Inject it from your host (backend API, static JSON or an existing address book).

json
[
  {
    "name": "Zhejiang",
    "children": [
      { "name": "Hangzhou", "children": [{ "name": "Xihu" }, { "name": "Binjiang" }] }
    ]
  }
]
  • Node fields: name (required; display and submitted value), optional code, recursive children (three levels are used)
  • When absent or invalid, region selects stay empty and are skipped in validation; the street address still works
  • Changing a parent resets its children; value restores selections by name level by level
  • The same-named property channel works too: value / items / regions accept objects or arrays directly (serialized and reflected into the JSON attributes; the getters return the parsed form)

Accessibility ​

  • Every input has a visible associated label; errors are linked via aria-invalid / aria-describedby
  • The address list is a native radio group (role="radiogroup" + one native radio per item)
  • The contact and region groups expose role="group" with accessible names

Disabled

Read-only

API ​

oas-address ​

Attributes ​

AttributeDescriptionTypeDefault
deletingDelete busy state: delete buttons disabled with aria-busy + spinner (orthogonal to disabled; set while the host is deleting)boolean—
disabledDisables all inputs and actions (including list selection and add)boolean—
itemsAddress list data (mode=list): JSON array [{ id?, name, phone, province?, city?, district?, detail?, isDefault? }]; the isDefault item is preselectedAddressEntry[] | string[]
modeShape: edit (default, address editor) / list (address list with single selection and default badge) / contact (contact-only mode, name and phone only)stringedit
readonlyRead-only: focusable and copyable, but cannot edit / submit / selectboolean—
regionsRegion data (host-injected contract): JSON array [{ name, code?, children? }], up to three levels; the component ships no administrative-division datasetAddressRegion[] | string—
savingSave busy state: save button disabled with aria-busy + spinner (orthogonal to disabled; set while the host is submitting)boolean—
selected-idSelected list id (controlled); defaults to the isDefault item, otherwise the first itemstring—
show-deleteRenders a Delete button on each address card; clicking emits oas-delete (the confirm dialog is composed by the host with oas-popconfirm)boolean—
valueControlled address value (JSON object): { id?, name, phone, province?, city?, district?, detail?, isDefault? }; prefills edit mode and only name / phone in contact modeAddressEntry | string—
value-formatRegion value tier: name (default, outputs names) / code (outputs region codes, nodes without code fall back to the name; prefill matches by code)stringname

Events ​

EventDescription
oas-addEmitted when “Add address” is clicked in list mode; no detail
oas-changeEmitted on field input or region change; detail { field, value } (value is the current address draft)
oas-deleteEmitted when the row Delete button is clicked (requires show-delete), detail { id, item } — the confirm dialog is composed by the host
oas-editEmitted when the row Edit button is clicked, detail { id, item } — the host switches to the editor accordingly
oas-invalidEmitted when submit validation fails; detail { fields } (field names), marks errors and focuses the first invalid field
oas-selectEmitted when an address is selected in list mode; detail
oas-submitEmitted when edit-mode validation passes; detail { value } (full address object)

CSS Variables ​

CSS VariableDefault
--oas-address-width100%

Released under the MIT OR Apache-2.0 License.