Tag input

Free text committed as removable chips. Different from combobox.md multiple, which chooses from a known list: here the values are whatever the person types.

Form control. It takes input in a form, so it is drawn on the Every input type block, beside every other control, as it starts and with an error where it has one.

Anatomy

Invite by email                                  ← label, names the chip list
┌─────────────────────────────────────────────┐
│ ([email protected] ✕) ([email protected] ✕) Add… │  ← chips and the text box share one frame
└─────────────────────────────────────────────┘
maya@ is not an email address.                   ← the note, in the help line's place
  • The frame is built from the field recipe's pieces (padding, text, border, tint) on a wrapping flex row, inside the recipe's shell, so it is the input's height, border, focus ring and disabled look.
  • Chips are the combobox chips: same classes, same remove button.
  • The chip list is a ul role="list" laid out as contents, so its chips flow in the frame's row beside the text box rather than on a line of their own. Hidden when empty.
  • Each chip posts a hidden name[] input. PHP renders value as chips, so the list posts without the script.
  • A template holds one empty chip, cloned by the script, so every class is a literal in the Blade file.

States

StateTreatment
default, hover, focusAs ds::input; the ring when focus is anywhere in the frame
a note showingThe note (danger, with the icon) replaces help; help returns when it clears
errorThe error border, and the message replaces help
disabledFlat shell; the text box, the remove buttons and the hidden inputs disabled

Behaviour (dsTagInput)

InputResult
Enter, with textCommits the text. With no text, Enter is left alone and submits the form
A separator character (default ,)Commits what is before it
Leaving the frameCommits what is typed
PasteSplits on the separators and on line breaks; commits each
Backspace in an empty box, or Left at its startFocuses the last chip's remove button
Backspace or Delete on a chipRemoves it; focus returns to the text box
Left / Right on a chipPrevious / next chip; Right past the last returns to the text box
The ✕Removes the chip; focus returns to the text box

Refusals stay in the box. Over max, invalid under validate, or a duplicate under unique (compared without case): the entry is left in the text box and the note says why. In a paste, the refused entries are left, joined, and the accepted ones are added. Nothing typed disappears silently.

validate takes 'email' or null and throws on anything else. The email check is the browser's own, from an <input type="email"> the script keeps off-page. A pattern prop was rejected: a regular expression has to mean the same in PHP and JavaScript, and a refusal needs a message a person can read.

separators is 'Enter' plus any single characters, and throws on anything longer. A phone keyboard sends a comma as text rather than as a key, so separators are also caught in the input event.

Accessibility

Chip listrole="list" labelled by the label, so its size is announced
Text boxThe labelled field; described by the note and the help or error
RemoveA button named "Remove {tag}"
AnnouncementsA polite, visually hidden live region: "Added {tag}.", "Removed {tag}."
The noteIts own polite live region, so a refusal is read when it appears
KeyboardTab passes each chip's ✕ in order, then the text box; see the table above
FocusRemoving a chip returns focus to the text box, never to body
PhoneWith validate="email": inputmode="email", no autocapitalise, no spellcheck

Binding

wire:model* and x-model* on the tag land on the root, beside x-modelable="dsValue", Alpine's core directive for a component that is a form control. dsValue is the scope's one exposed name: an array of the chips' values, set by the behaviour after every add and remove, and watched, so a list the host sets is rendered as chips (from the template, blanks dropped, without an announcement or the max/unique/validate checks) and handed back normalised. The prefix matters: the host's expression on the root is read in this scope, and an unprefixed name would shadow a host property of the same name. On first start the host's value wins over the server-rendered value, which is how x-model treats any field. The hidden inputs still post.

Without the script

The text field is named {name}_text in the markup, and an Alpine binding takes the name off, so with the script only the chips post ({name}[]). Without it what is typed posts, separated by commas or the component's separator characters, beside the server's chips. Support\Tags::from($tags, $text, $separators) joins, splits, trims and dedupes the two. Added in 30B; before it, a page with no script posted only the server's chips.

Do not

  • Do not use it to pick from known values. That is a combobox.
  • Do not let refusals vanish. The note is the contract.
  • Do not rely on validate. Validate name.* on the server.

From the package repo: edit this page on GitHub.

Was this page helpful?