TagInput Web Component
Type and press Enter or comma to add a tag. Backspace removes the last one.
How it helpsIt is form-associated, so it submits with a normal
<form> as a comma-separated value. You don't need a hidden input.In a form
export default function Example() {
return (
<>
<label className="w-full" style={{ maxWidth: "420px" }}>
Categories
<sd-tag-input name="tags" value="groceries,fuel,rent" placeholder="Add category…"></sd-tag-input>
<span className="hint">Press Enter or comma to add.</span>
</label>
</>
);
}<label class="w-full" style="max-width:420px">
Categories
<sd-tag-input name="tags" value="groceries,fuel,rent" placeholder="Add category…"></sd-tag-input>
<span class="hint">Press Enter or comma to add.</span>
</label>In Next.js
// Server Action: the field submits as one comma-separated value.
export default function Page() {
async function save(form: FormData) {
"use server";
const tags = String(form.get("tags")).split(","); // ["groceries", "fuel", "rent"]
}
return (
<form action={save}>
<sd-tag-input name="tags" value="groceries,fuel,rent" />
<button>Save</button>
</form>
);
}
// Client side: useSidedishEvent(ref, "change", (e) => e.detail.tags)
// Methods: ref.current.add("travel"), ref.current.remove(0), ref.current.valueAPI
| Name | Type | Description |
|---|---|---|
name | attr | Form field name. |
value | attr / prop | Comma-separated tags. |
placeholder | attr | Input placeholder. |
add(tag) / remove(i) | method | Change tags from code. |
change | event | detail.tags: the current array. |