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.

Keyboard, accessibility & templates

Keyboard navigation and typing, the live announcements and their localization, prefix and suffix texts, and tag templates with accessible names and per-tag styles.

Keyboard & typing

The chips are a single stop of the tab order rather than one per tag. Left and Right (swapped in RTL) move between them, Home and End jump to the ends, Delete and Backspace remove the focused one and hand the focus to its neighbour, Enter and F2 open the inline edit, Alt with the arrows moves the tag, and Escape goes back to the input. Every one of those keys carries a browser default that is held back for it, so a gesture of the field never turns into a gesture of the browser. NoClearOnEscape leaves Escape alone altogether, for a field inside a surface that closes on it.

From the input, Backspace on an empty field removes the last tag - NoBackspaceRemove turns that off and BackspaceEditsLastTag turns it into a correction, putting the tag's text back into the input instead of letting it disappear - and the backwards arrow walks into the last chip. Tab commits the text left in the input unless NoAddOnTab is set, while Shift+Tab always walks straight out, so the field is never a trap. Losing the focus commits too, which NoAddOnBlur turns off and ClearOnBlur turns into a discard. While an input method is composing a character the Enter that picks a candidate is not treated as a confirmation, so a tag is never cut in half by it.

InputMode and EnterKeyHint decide the virtual keyboard a phone opens and the label on its return key; SpellCheck turns the red underline back on for a field that collects words rather than identifiers, and AutoComplete hands the browser's own autofill back where the field collects values it knows about.

one
two
three
3 tags.
one
two
2 tags.
one
two
Backspace on the empty input takes the last tag back for correction; leaving the field throws away what was still being typed.
2 tags.
An email keyboard with a Done return key.

Accessibility & localization

A chip appearing, disappearing or changing place reaches no screen reader on its own - the list sits away from the caret - so the component says each of them out loud through a polite live region: AddedAnnouncementFormat, AddedManyAnnouncementFormat, RemovedAnnouncementFormat, ClearedAnnouncementFormat, EditedAnnouncementFormat, MovedAnnouncementFormat, PickedUpAnnouncementFormat, PutBackAnnouncementFormat and InvalidAnnouncementFormat. Most take {0} for the tag - the move also {1} for the position it landed on and {2} for how many tags there are, the refusal {1} for why it was turned down - and an empty string keeps that event silent. The two standing for a batch count instead of naming: reading fifty names out is not a confirmation but a wall, and the tags are in the list to be walked through anyway.

The list itself sits away from the caret, so a reader landing in a field that already holds tags would otherwise be told only that the input is empty: the input describes itself with how many there are - "3 of 5 tags" where MaxTags sets a ceiling - which TagCountAriaDescriptionFormat writes. It is referenced rather than announced, so it is read on arriving and never while the field is being typed into.

The names of the parts follow the same idea: TagsAriaLabel names the list, TagAriaDescription is the sentence telling what the keyboard can do with the chip just reached (built from the gestures the field was actually given, and left out when there are none), DismissAriaLabelFormat and DismissTitle name the dismiss button, ReorderAriaLabelFormat, ReorderDropAriaLabelFormat and ReorderTitle the reorder handle, EditAriaLabelFormat the little edit input, and ClearButtonAriaLabel the button that empties the field. Each chip also carries its own position in the whole list, so "the third of six" stays true where MaxDisplayedTags folded the rest away.

One thing a chip cannot give on its own is a 24 CSS-pixel pointer target (WCAG 2.2, SC 2.5.8): the dismiss button and the reorder handle take the full height of the chip and a pad on either side of the glyph, which is all a chip as tall as a line of text has to give. Where that minimum has to be met, grow the chips - --bit-TagsInput-tag-min-height: 1.5rem with a roomier --bit-TagsInput-tag-padding - and widen the buttons through Classes.DismissButton; the keyboard reaches both at any size, Delete removing the focused chip and Alt with the arrows moving it.

blazor
dotnet
2 tags.
Press Enter to edit, Delete to remove, or Alt with the arrow keys to move.
blazor
dotnet
2 of 5 skills chosen.
Press Enter to rename this skill, or Delete to drop it.

Prefix & Suffix

Prefix and Suffix draw a short text inside the field, at its start and at its end, which is not part of the value: the To: of a recipients field, the unit of a list of measurements. The prefix sits in front of the chips and the suffix after everything else, the clear button included. Both are announced with the field - the input references them, ahead of the Description - so a reader is told what a sighted user reads inside it. PrefixTemplate and SuffixTemplate take markup instead, for an icon or a badge; markup is as likely to be a glyph as a word, so those are not announced and what they mean has to be in the Label as well.

1 tags.
12
34
cm
2 tags.
blazor
dotnet
2 tags.

Tag template & per-tag styling

TagTemplate replaces the text of every chip with markup of your own, the tag being the context; the chip around it - its background, its focus ring, its dismiss button - is still the component's, so a template only describes what goes inside. Markup is not something a name can be read off, though, so a chip drawing Ada Lovelace over the tag [email protected] is announced by neither the one nor the other: GetTagName is what names it, and the name it gives is carried by the chip, by its dismiss button, by its reorder handle, by its inline edit and by every announcement the tag takes part in. GetTagClass and GetTagStyle are what tell one chip apart from the next: each receives a tag and returns what that particular chip should carry on top of the shared styling - the address that is not well formed drawn in red, each priority in a color of its own. They run for every drawn tag on every render, so they belong to a lookup rather than a computation, and an exception thrown out of one leaves that chip looking like all the others.

blazor
dotnet
2 tags.
Ada Lovelace
Grace Hopper
The chips read as people; the value keeps the addresses, and so does the form.
2 tags.
not-an-address
An address that is not well formed is drawn in red.
2 tags.
low
medium
high
Each level carries a color of its own.
3 tags.

Feedback

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