InstallComponentsExample

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.value

API

NameTypeDescription
nameattrForm field name.
valueattr / propComma-separated tags.
placeholderattrInput placeholder.
add(tag) / remove(i)methodChange tags from code.
changeeventdetail.tags: the current array.