Skip to content

Inputs

TagsInput

Bit.BlazorUIChipsTokens

The TagsInput turns free text into a list of short values: the user types a tag, confirms it with Enter (or with a separator of your own), and it becomes a removable chip in front of the input. It binds to a collection of strings, offers known values through the browser's own suggestion list - or accepts nothing else - splits a pasted list into a tag each, and keeps the entries in shape on the way in: trimming, transforming, capping their length and their number, and rejecting the ones that repeat, that are too short, that fail a pattern or that a validator turns down, each rejection reported with the rule that caused it. The chips form a proper accessible list: one stop of the tab order that the arrow keys walk through, removable with Delete, correctable in place, movable by drag, by two taps on the handle a chip grows or with Alt and the arrows, with the ones a predicate pins in place carrying no dismiss button at all, and with every change announced to screen readers. It comes in three sizes, three variants and the whole accent palette, folds a long list away behind a chip, carries a label, a helper text, a prefix, a suffix, a counter, a clear button and a spinner for the suggestions it is still fetching, can be driven entirely from code, and takes part in an EditForm like any other input.

Forms, events & API

Binding the list of tags with form validation, the events that can cancel or correct a change, and driving the field from code.

Binding, forms & validation

The value is the list of tags itself, bound two ways with @bind-Value; DefaultValue seeds an uncontrolled field read through OnChange instead. Removing the last tag leaves the value at null rather than at an empty list, which is what makes a plain [Required] catch an empty field. Inside an EditForm the field validates like any other input: it reports to the EditContext, paints the error state, marks its input with aria-invalid and turns its description red - the annotations that apply being the collection ones. Enter confirms a tag, so it is held back from submitting the form; CancelConfirmKeysOnEmpty hands it back once there is nothing left to confirm. Name posts the whole list as a single field of a plain HTML form, the tags joined by the first separator.

Tags: null
blazor
1 tags.
Tags: null

At least one tag is required.


Form submitted: False

Events

OnBeforeAdd and OnBeforeRemove run before the list changes and can call the change off with args.Cancel, which is the hook for a confirmation or a rule that lives on the server. OnBeforeAdd can also correct the tag on its way in by writing to args.Tag - the address an alias resolves to, the id a code stands for - and what it leaves there is what is added, announced and reported. OnAdd receives every tag added in one go (a paste adds several), OnRemove the one taken off, OnInvalid the one refused with the rule that refused it, and OnInput the text of the input as it is typed - which is what an external suggestion list is driven by. OnInput also reports the emptying the component does itself, so a list of your own is never left filtering on a word that is already a chip. OnTagClick turns a chip into a way in to whatever it stands for - the card of the recipient, the filter the tag is built on - without taking the focus the click already gives it, and Enter on the focused chip does the very same thing, so the way in is not one only a pointer has (unless EditableTags has claimed that key for the inline edit). OnFocusIn, OnFocusOut and OnKeyDown report the plain input events on top of them.

blazor
1 tags.
Press Enter to open, or Delete to remove.
Typing:
Last event:

Public API

The component can be driven from the outside: AddTagAsync and AddTagsAsync add through exactly the same pipeline as typing does, RemoveTagAsync and RemoveTagAtAsync take one off, MoveTagAsync moves one - raising OnReorder, and without needing AllowReorder, which offers the gesture to the user rather than permitting the list to be reordered - EditTagAsync opens the inline edit of one, SetInputTextAsync writes the text of the input (which is what fills the field from a suggestion list of your own driven by OnInput), Clear empties the list and FocusAsync puts the caret back. All of them do nothing while the field is disabled or read-only, and all go through the callbacks, the announcements and the validation the equivalent gesture does.

blazor
1 of 6 tags.
Press Enter to edit, or Delete to remove.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.