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
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)
Host-side confirm recipe (pseudo code):
<!-- 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-deleteimmediately; 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 theitemsmutation).
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
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
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).
[
{
"name": "Zhejiang",
"children": [
{ "name": "Hangzhou", "children": [{ "name": "Xihu" }, { "name": "Binjiang" }] }
]
}
]- Node fields:
name(required; display and submitted value), optionalcode, recursivechildren(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;
valuerestores selections by name level by level - The same-named property channel works too:
value/items/regionsaccept 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
| Attribute | Description | Type | Default |
|---|---|---|---|
deleting | Delete busy state: delete buttons disabled with aria-busy + spinner (orthogonal to disabled; set while the host is deleting) | boolean | — |
disabled | Disables all inputs and actions (including list selection and add) | boolean | — |
items | Address list data (mode=list): JSON array [{ id?, name, phone, province?, city?, district?, detail?, isDefault? }]; the isDefault item is preselected | AddressEntry[] | string | [] |
mode | Shape: edit (default, address editor) / list (address list with single selection and default badge) / contact (contact-only mode, name and phone only) | string | edit |
readonly | Read-only: focusable and copyable, but cannot edit / submit / select | boolean | — |
regions | Region data (host-injected contract): JSON array [{ name, code?, children? }], up to three levels; the component ships no administrative-division dataset | AddressRegion[] | string | — |
saving | Save busy state: save button disabled with aria-busy + spinner (orthogonal to disabled; set while the host is submitting) | boolean | — |
selected-id | Selected list id (controlled); defaults to the isDefault item, otherwise the first item | string | — |
show-delete | Renders a Delete button on each address card; clicking emits oas-delete (the confirm dialog is composed by the host with oas-popconfirm) | boolean | — |
value | Controlled address value (JSON object): { id?, name, phone, province?, city?, district?, detail?, isDefault? }; prefills edit mode and only name / phone in contact mode | AddressEntry | string | — |
value-format | Region value tier: name (default, outputs names) / code (outputs region codes, nodes without code fall back to the name; prefill matches by code) | string | name |
Events
| Event | Description |
|---|---|
oas-add | Emitted when “Add address” is clicked in list mode; no detail |
oas-change | Emitted on field input or region change; detail { field, value } (value is the current address draft) |
oas-delete | Emitted when the row Delete button is clicked (requires show-delete), detail { id, item } — the confirm dialog is composed by the host |
oas-edit | Emitted when the row Edit button is clicked, detail { id, item } — the host switches to the editor accordingly |
oas-invalid | Emitted when submit validation fails; detail { fields } (field names), marks errors and focuses the first invalid field |
oas-select | Emitted when an address is selected in list mode; detail |
oas-submit | Emitted when edit-mode validation passes; detail { value } (full address object) |
CSS Variables
| CSS Variable | Default |
|---|---|
--oas-address-width | 100% |