enes

TagPicker AI

A TagPicker combines a text field and a dropdown, giving people a way to choose several options from a list or to enter their own choice. Every choice becomes a dismissible Tag shown in the control.

Usage

<script>
	import { TagPicker, TagPickerOption } from 'fluentui-svelte';

	const options = ['John Doe', 'Jane Doe', 'Max Mustermann'];

	let selectedOptions = $state([]);
</script>

<TagPicker
	bind:selectedOptions
	inputAttributes={{ 'aria-label': 'Select Employees' }}
	tagGroupProps={{ 'aria-label': 'Selected Employees' }}
>
	{#each options as option (option)}
		<TagPickerOption value={option}>{option}</TagPickerOption>
	{/each}
</TagPicker>

Keyboard navigation

Pressing Backspace in an empty input removes the last tag, so tell your users about it. ArrowDown and ArrowUp open the list and move through the options, Enter chooses the option under the cursor, and Escape or Tab closes the list. Home and End move the caret of the input.

Shape

Set shape to rounded (default), square, or circular.

Tags inherit shape. Use tagGroupProps.shape to override.

Default

Bind selectedOptions to the values of the chosen options, and pass the TagPickerOption elements as children. The tag snippet renders the tag of a chosen option, here with an Avatar as its media. Render a Tag with the given value.

Button

Pass readonly through inputAttributes to make the input read only and use the TagPicker as a button that opens the list.

Disabled

A disabled TagPicker cannot be opened, typed in or changed, and its tags cannot be dismissed.

Expand icon

Replace the dropdown arrow with expandIcon, a component or a snippet, or remove it by passing null.

Filtering

The options are filtered as the user types: an option stays when its text contains what was typed, ignoring case. The first match is highlighted so that Enter chooses it, and notFoundText is shown when no option is left.

Grouped

Group options with TagPickerOptionGroup. A group with no option left on screen hides itself.

No popover

Leave out the options to get a TagPicker without a list, to let users enter their own tags. Bind value and add the tag in querySubmitted, called when Enter is pressed with text in the input.

Secondary action

Add a button next to the dropdown arrow with the secondaryAction snippet.

Single select

With single a new choice replaces the current tag and closes the list.

Size

A TagPicker is medium by default, or large or extra-large. The size of the control also picks the size of its tags.

Truncated text

A tag that is wider than the control, or than its own max-width, shortens its text with an ellipsis. Give the tag a title so the full text stays reachable, and truncate the text of the options yourself.

Form Control Attributes

Attributes and events apply to the native form field; ref gives you access to it. For the container, use wrapperAttributes and wrapperRef.

Component Props (TagPicker)

NameTypeDefaultDescription
shape'circular' | 'rounded' | 'square''rounded'Shape of the field and the default shape of its selected tags.
wrapperRef bindableHTMLDivElement | nullThe DOM reference of the wrapper element.
ref bindableHTMLSelectElement | nullThe DOM reference of the native multiple select that holds the selected options. The other attributes of the picker are applied to it.
inputRef bindableHTMLInputElement | nullThe DOM reference of the text input.
inputAttributesHTMLInputAttributesThe attributes to spread on the text input.
selectedOptions bindablestring[][]The values of the chosen options, shown as tags in the control. A value no option has is shown as it is.
value bindablestring''The text typed in the input. It filters the options, and is cleared when one is chosen.
open bindablebooleanfalseControls the open state of the option list.
singlebooleanLimits the picker to one tag: choosing an option replaces the current one and closes the list.
disabledbooleanDisables the user interaction: the input, the tags and the list.
size'medium' | 'large' | 'extra-large''medium'The size of the control. It also picks the size of the tags.
placeholderstringThe placeholder text of the input.
notFoundTextstring'No options available'The message shown in place of the list when there is no option left to choose.
expandIconSnippet | Component | nullChevronDownRegularThe dropdown arrow. Pass null to remove it.
secondaryActionSnippetA button-like element rendered at the end of the control, before the dropdown arrow.
tagSnippet<[{ value: string; text: string }]>Renders the tag of a chosen option, to give it media or custom text. Falls back to a plain tag. Render a Tag with the given value.
tagGroupPropsTagGroupPropsThe props to spread on the group holding the tags. Override shape here to give the tags a different shape from the field. This is where its aria-label goes, 'Selected options' by default.
wrapperAttributesHTMLAttributesThe attributes to spread on the wrapper element.
onSelectionChange(e: Event, selectedOptions: string[]) => voidCalled when an option is chosen, or a tag is removed.
querySubmitted(e: KeyboardEvent, query: string) => voidCalled when Enter is pressed with text in the input and no option under the cursor. This is where a free-form tag is added.
childrenSnippetThe options. Leave it out to get a picker without a list.
NativeFieldAttributes<Omit<HTMLSelectAttributes, 'value' | 'multiple' | 'size' | 'disabled' | 'children'>>

Component Props (TagPickerOption)

NameTypeDefaultDescription
valuestringThe value added to selectedOptions when the option is chosen.
textstringThe text the option is filtered by, and the text of its tag. Falls back to value.
idstringThe id of the option element, referenced by the input through aria-activedescendant. Falls back to a generated id.
ref bindableHTMLLIElement | nullThe DOM reference of the option element.
disabledbooleanTakes the option out of reach: it is shown, but the cursor steps over it.
ListViewItemProps<'li'> without as, value, onAction, checkmark

Component Props (TagPickerOptionGroup)

NameTypeDefaultDescription
labelstringThe label of the group.
ref bindableHTMLUListElement | nullThe DOM reference of the group element.
childrenSnippetThe options of the group.
wrapperAttributesHTMLAttributesThe attributes to spread on the wrapper element.
wrapperRef bindableHTMLLIElement | nullThe DOM reference of the wrapper element.
HTMLAttributes<HTMLUListElement> without children
Edit this Doc View as Markdown