Tag input

Short text values, typed into a field and kept as removable chips: email addresses to invite, labels on a record, keywords.

Tag input

Needs Alpine and the package's script. See getting started.

Use it

<x-ds::tag-input
    name="emails"
    label="Invite by email"
    :value="old('emails', [])"
    validate="email"
    placeholder="Add an address"
    help="Press Enter or comma after each address."
/>

Each chip posts as emails[], so the request carries an array, which is what old('emails') and Laravel's array rule expect.

Props

PropTypeDefaultWhat it does
namestringrequiredEach tag posts as name[]. The [] is added for you.
labelstring—Field label.
helpstring—Guidance under the field.
errorstring—Validation message. Replaces help.
valuearray[]The tags already chosen, rendered as chips by the server.
placeholderstring—Shown in the text box.
maxint—The most tags it takes.
separatorsarray['Enter', ',']What commits the text: 'Enter', and any single character.
uniquebooltrueRefuse a tag already in the list, compared without case.
validate'email'—Refuse an entry that is not an email address.
disabledboolfalseDisables the field, the chips' buttons, and what they post.

wire:model and x-model go on the field's wrapper and bind both ways (see below). A class goes on the wrapper too; anything else is ignored.

What it does

  • Enter, a comma, or leaving the field turns what was typed into a chip. Enter in an empty box is left alone, so it still submits the form.
  • Pasting a list (a line of addresses, a column from a spreadsheet) adds every entry at once. A line break always separates entries in a paste.
  • Backspace in an empty box moves to the last chip; a second Backspace removes it. Left and Right move between the chips.
  • Each chip's ✕ removes it, and is named for it: "Remove [email protected]".
  • Adding and removing are announced to a screen reader: "Added [email protected]."

An entry that cannot be added is never dropped

An invalid address, a duplicate, or one more than max stays in the text box, with a line under the field saying why: "maya@ is not an email address." The line takes the place of help while it is there, and goes when the entry is fixed or cleared. Nothing someone typed disappears without a reason.

validate

validate="email" checks each entry with the browser's own email rule, the one a type="email" field enforces. It is a courtesy to the person typing. Validate on the server as well, always:

$request->validate([
    'emails' => ['required', 'array', 'max:10'],
    'emails.*' => ['email'],
]);

It takes email or nothing. A named check rather than a pattern prop, on purpose: it comes with a message a person can read, and a regular expression has to mean the same thing in PHP and in the browser, which is a quiet way for the two to disagree. Any other value throws when the view renders.

Separators

{{-- Semicolons, as in a mail client's To line --}}
<x-ds::tag-input name="to" label="To" :separators="['Enter', ';']" />

'Enter' is the Enter key; anything else must be one character. A space works, for keywords, but then no tag can contain one.

Binding it: wire:model and x-model

Put the binding on the tag. It works both ways: a tag the reader adds or removes reaches your property, and a list you set is drawn as chips.

{{-- Livewire: public array $emails = []; --}}
<x-ds::tag-input name="emails" label="Invite by email" wire:model="emails" :value="$emails" validate="email" />

{{-- Alpine --}}
<div x-data="{ labels: ['billing'] }">
    <x-ds::tag-input name="labels" label="Labels" x-model="labels" :value="['billing']" />
    <button type="button" @click="labels = []">Clear all</button>
</div>

The property is an array of strings. Pass :value as well, so the chips are right on first paint, before Alpine starts, and in a page whose script never runs. When Alpine starts, the bound property wins over value, as x-model on an ordinary field does.

A list you set is shown as it is, blanks dropped: max, unique and validate guard what a person types, and you are the one deciding. It is not announced to a screen reader either, because the reader did not do it. The chips still post as name[], so the same field works in a plain form too.

Without the script

There are no chips to add, so the text field posts what was typed, as {name}_text: the tags already chosen still post as {name}[], and the reader types more, separated by commas (or your separators characters). With the script the field has no name, and only the chips post. Join the two on the server with Support\Tags, which splits the text, trims, and drops empties and repeats:

use Shipbytes\BladeUi\Support\Tags;

$emails = Tags::from($request->input('emails'), $request->input('emails_text'));

The validate and max checks are the script's: on the server, validate the list you get back.

Accessibility

  • The chips are a list named by the label, so a screen reader hears how many there are.
  • The text box is the labelled field. The message about an entry describes it and is announced when it appears.
  • Adds and removes are announced through a polite live region.
  • The ✕ buttons are real buttons with their own names. Six buttons all called "Remove" would be indistinguishable.

Don't

  • Don't use it to choose from a known list. That is a combobox with multiple, which only offers what exists.
  • Don't use it where values are long. A chip truncates; a sentence is not a tag.
  • Don't rely on validate alone. See above.

More in specs/tag-input.md.


From the package repo: edit this page on GitHub.

Was this page helpful?