Tags forms Controller
Multi-value chips + free create — a native text input carrying a comma-joined value, progressively enhanced into a chips UI. JS off: a usable comma-separated text field. JS on: type + Enter/comma creates a chip, blur commits a leftover token, × removes; the native input stays as the submitted value.
Layer: L1 surface · Recipe: multi-label-form — multi free-form labels. Curriculum: AGENTS.md; pick matrix: docs/agent/pick-a-surface.md; blast radius: CONSUMER_MAP.md.
Copy this
<label class="field hm-measure" for="hm-tags-field"><span class="field__label">Labels</span><input id="hm-tags-field" name="labels" type="text" data-tags class="form-input" value="urgent,backend" placeholder="Add a label…"></label>Server exchange
This Hyperpart has no server exchange — it is presentation or client chrome only. htmx does not issue a request on this part's behalf. If you put an affordance (hx-*) on a control that uses this markup, that action's exchange belongs to the action, not this part. See Swap contract for host-owned envelopes.
Swap contract
Agent-visible HTMX topology (ADR-0054 / decision 0012). exchange envelope = what the response may re-emit relative to the persistent slot (body_only | outer | none | host_owned | document). dual-lock validates part markup only — not this envelope. Stem: stems/morph-safe-hypermedia.md; decision: docs/decisions/0012-swap-identity-contract.md.
No host HTMX exchange on this part — presentation or client chrome only. exchange envelope: n/a.
If a host wraps this markup in hx-*, that host owns the swap contract (sole identity + envelope). Prefer innerMorph / outerMorph for stable slots; replacement for flash; body-only responses under inner swaps.
Envelope response examples
This part has no owned exchange (envelope n/a). If a host adds hx-*, that host’s envelope applies — typically body_only:
<!-- Host wraps this presentation part with hx-* (host owns envelope) -->
<!-- Prefer: hx-swap="innerMorph" hx-target="#panel-body" -->
<!-- Server returns body_only interior for #panel-body -->
<div class="dz-stack">content…</div>
Do not re-own the slot:
<!-- WRONG: server returns the presentation root with a new id every poll -->
<div id="tags-root" data-dz-region>…</div>
How to use it
Seams
- native input value is a comma-joined tag string — the permanent submit shape
- chips UI rewrites the native input on every add/remove and fires change
Do / Don't
| Do | Don't |
|---|---|
| keep the native input as the form value under the chips root | post a JSON array the server has never seen from a plain field |
Pitfalls
- JS off: type a, b, c — server splits on comma; do not require the chips UI
- dedupe/trim/skip-empty on add; paste splits on comma and newline
Keyboard / AT
- chips are role=list of listitem; each × is labelled Remove {tag}
- Enter or comma creates a chip; blur commits leftover; Backspace on empty entry removes the last
Related parts
DOM contract
What the emitted HTML must satisfy — the table is the required surface; Python under contracts/ is the package-internal dual-lock CI runs (tests/test_contracts.py), not an app route. Standalone HTMX4: implement the API so responses match this markup. Dazzle: the agent emits SSR that already satisfies it. Do not invent attrs outside these tables. For request/response wiring see Server exchange.
contracts/tags.py
Required in the DOM: root [data-tags] (part tags). Emit only these attributes — inventing extras is fine only if controllers ignore them; omitting required ones fails CI (tests/test_contracts.py).
| Node | Attr | Constraint |
|---|---|---|
[data-tags] | name | present (any value) |
Ingestion model TagsField
Server-side shape before render — one normalisation boundary for producers.
| Field | Type | Required |
|---|---|---|
name | string | required |
field_id | string | required |
label | string | required |
tags | array | optional |
placeholder | string | optional |
Exemplar render()
Executable in CI: the Python below is render(); the boxed preview is render(EXEMPLARS[0]) — the first fixture the dual-lock tests emit, not a separate widget and not gallery mock data. Sample model: label=Labels.
def render(field: TagsField) -> str:
joined = ",".join(field.tags)
return (
f'<label class="dz-field" for="{html.escape(field.field_id, quote=True)}">'
f'<span class="dz-field__label">{html.escape(field.label)}</span>'
f'<input id="{html.escape(field.field_id, quote=True)}" '
f'name="{html.escape(field.name, quote=True)}" type="text" data-dz-tags '
f'class="dz-form-input" value="{html.escape(joined, quote=True)}" '
f'placeholder="{html.escape(field.placeholder, quote=True)}"></label>'
)
Live output of render(EXEMPLARS[0]) — fixture markup the dual-lock validates (sample field values only).
Notes
<input type="text" data-tags> whose value is a COMMA-JOINED tag string — usable and submittable with no JS (type a, b, c; the server splits on comma), native required intact. Seeded non-empty values enhance on DOM ready; empty fields enhance on first interaction. tags.js wraps the input in a .tags root — a role="list" of removable chips + a borderless entry — and hides the native input (kept in the DOM as the submitted value). Every add/remove rewrites the native input to the comma-joined chip list and fires change, so the submit shape never changes. Type + Enter or comma creates a chip (trim/dedup/skip-empty); leftover typed text commits on blur (must not vanish — cycle 2131); paste splits on comma/newline; × or Backspace-on-empty removes a chip; add/remove is announced via a visually-hidden aria-live region.Source files
One logical Hyperpart, 4 code items (CSS layered, JS bundled). Bound by HYPERPART: tags — python tools/hyperpart.py tags lists them.
site/registry.py · contracts/tags.py · components/tags.css:1 · controllers/dz-tags.js