Tags Input
Features
- Can be controlled or uncontrolled.
- Full keyboard navigation.
- Limit the number of tags.
- Accept value from clipboard.
- Clear button to reset all tags values.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import all parts and piece them together.
<script setup>
import { TagsInputClear, TagsInputInput, TagsInputItem, TagsInputItemDelete, TagsInputItemText, TagsInputRoot } from 'reka-ui'
</script>
<template>
<TagsInputRoot>
<TagsInputItem>
<TagsInputItemText />
<TagsInputItemDelete />
</TagsInputItem>
<TagsInputInput />
<TagsInputClear />
</TagsInputRoot>
</template>API Reference
Root
Contains all the tags input component parts.
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when disabled |
[data-focused] | Present when focus on input |
[data-invalid] | Present when input value is invalid |
Item
The component that contains the tag.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-disabled] | Present when disabled |
ItemText
The textual part of the tag. Important for accessibility.
ItemDelete
The button that delete the associate tag.
| Data Attribute | Value |
|---|---|
[data-state] | "checked" | "unchecked" |
[data-disabled] | Present when disabled |
Input
The input element for the tags input.
| Data Attribute | Value |
|---|---|
[data-invalid] | Present when input value is invalid |
Clear
The button that remove all tags.
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when disabled |
Examples
Paste behavior
You can automatically add tags on paste by passing the add-on-paste prop.
<script setup lang="ts">
import { TagsInputInput, TagsInputItem, TagsInputItemDelete, TagsInputItemText, TagsInputRoot } from 'reka-ui'
</script>
<template>
<TagsInputRoot
v-model="modelValue"
add-on-paste
>
…
</TagsInputRoot>
</template>Multiple delimiters
You can pass RegExp as delimiter to allow multiple characters to trigger addition of a new tag. When add-on-paste is passed it will be also used to split tags for @paste event.
<script setup lang="ts">
import { TagsInputInput, TagsInputItem, TagsInputItemDelete, TagsInputItemText, TagsInputRoot } from 'reka-ui'
// split by space, comma, semicolon, tab, or newline
const delimiter = /[ ,;\t\n\r]+/
</script>
<template>
<TagsInputRoot
v-model="modelValue"
:delimiter="delimiter"
add-on-paste
>
…
</TagsInputRoot>
</template>Real-world examples
- Combobox TagsInput: Rendering Combobox with Tags Input.
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
Delete | When tag is active, remove it and set the tag on right active. |
Backspace | When tag is active, remove it and set the tag on left active. If there are no tags to the left, either the next tags gets focus, or the input. |
ArrowRight | Set the next tag active. |
ArrowLeft | Set the previous tag active. |
Home | Set the first tag active |
End | Set the last tag active |
In a right-to-left layout (dir="rtl" on TagsInputRoot, or on a ConfigProvider), ArrowRight moves from the start of the input to the last tag, and ArrowLeft and ArrowRight swap when moving between tags. See Internationalization.
Headless composable
useTagsInput holds the tags (modelValue, addTags(), removeTag(), clear()), validation (max, duplicate, isInvalidInput, onInvalid), the delimiters and paste behaviour, and the root, input and clearButton part surfaces with getItemSurfaces(value) for each tag. It is pure, so it can be called outside setup(). The composable is experimental; see Headless composables.
