Skip to content

Tag Input

A free form tag field. The user types a tag and presses Enter or comma to add it, and a pasted list with commas is split into tags. The tags use the stolon-tag-pill style.

Decision

When Use
Short free form labels, such as group tags Tag Input
Picking from a known list Chip Select
Longer values the reader reviews one per row String List

Usage

import { createTagInput } from './configurationpage?name=myplugin_stolon.js';

var tags = createTagInput({ values: group.Tags, onChange: function (values) { group.Tags = values; } });
container.appendChild(tags.element);

Classes: stolon-tag-input, stolon-tag-input-box, stolon-tag-input-field. The handle is { element, input, getValue, setValue, add, setSize, destroy }.

States and Modifiers

  • add(tag) returns true when the tag was added.
  • Enter and the keys in separators (comma by default) add the tag. Backspace on an empty input removes the last tag. A refused piece of a pasted list stays in the input.
  • addOnBlur (default true) adds the typed text when the input loses focus.
  • duplicates: true allows the same tag twice. maxTags caps the tags. validate(tag) returns an error sentence, or an empty value when valid. The typed text checks through the validation core.
  • The defaults are "New tag" (inputLabel), "Remove {value}", "Already added." (duplicateText), and "No more tags can be added." (limitText).
  • The box has the field look, with a search icon at its trailing end, and clicking it focuses the input.
  • A removed tag passes focus to the next remove button, or to the input.
  • size is small, medium (default), or large, and sizes its fields on the size scale. setSize(size) changes it later.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createTagInput

function createTagInput(options?: TagInputOptions): TagInputHandle

A free form tag field. The user types a tag and presses Enter or comma to add it, Backspace on an empty input removes the last tag, a removed tag passes focus to the next remove button or the input, and a pasted list that has commas is split into tags. The tags use the stolon-tag-pill style, and a search icon sits at the box's trailing end.

Option Type What it does
values unknown[] array of strings [optional]
placeholder string a valid example tag [optional]
inputLabel string aria-label of the input (default 'New tag')
removeLabel string title and aria-label of a remove button, where {value} is the tag (default 'Remove {value}')
duplicateText string message for a repeated tag (default 'Already added.')
limitText string message when maxTags is reached (default 'No more tags can be added.')
separators string[] keys that add a tag, besides Enter, where a one character key also splits typed and pasted text (default [','])
duplicates boolean true allows the same tag twice (default false)
addOnBlur boolean true adds the typed text when the input loses focus (default true)
maxTags number most tags allowed [optional]
size 'small' | 'medium' | 'large' 'small', 'medium', or 'large' for its fields (default 'medium')
validate (tag: string): string | null | undefined function(tag) returning an error sentence, or an empty value when valid, checked through the validation core [optional]
onChange (values: string[]): void function(values) [optional]
Handle member Type
input HTMLInputElement
getValue (): string[]
setValue (next: unknown[] | null): void
add (raw: unknown): boolean
setSize (size?: 'small' | 'medium' | 'large'): void
destroy (): void
Class What it looks like
stolon-tag-input-box Field box that holds tag pills and a text input that grows to the free width

Example

Example Code

<!-- title: Tag Input -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const view = document.getElementById('view');
const mount = document.getElementById('mount');
mount.appendChild(base.createTagInput({ values: ['news', 'movies'], placeholder: 'music' }).element);
</script>