Tags Input
A component that allows users to add tags to an input field.
Anatomy
To set up the tags input correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Examples
Learn how to use the TagsInput component in your project. Let's take a look at the most basic example:
Example not foundExample not foundExample not foundExample not foundInitial tags
To set the initial tag values, set the defaultValue prop.
Example not foundExample not foundExample not foundExample not foundMax Tags
To limit the number of tags within the component, you can set the max property to the limit you want. The default
value is Infinity.
When the tag reaches the limit, new tags cannot be added except the allowOverflow prop is set to true.
Example not foundExample not foundExample not foundExample not foundControlled
Use the value and onValueChange props to programmatically control the tags input's state. This allows you to manage
the tags array externally and respond to changes.
Example not foundExample not foundExample not foundExample not foundControlled Input Value
Use the inputValue and onInputValueChange props to control the text input field independently. This is useful for
clearing the input or pre-filling it programmatically.
Example not foundExample not foundExample not foundExample not foundCustom Delimiter
Use the delimiter prop with a regex pattern to specify multiple characters that can separate tags. By default, only
the Enter key creates tags.
Example not foundExample not foundExample not foundExample not foundDisabled
Use the disabled prop to make the tags input non-interactive. Users won't be able to add, remove, or edit tags.
Example not foundExample not foundExample not foundExample not foundInvalid
Use the invalid prop to mark the tags input as invalid for form validation purposes.
Example not foundExample not foundExample not foundExample not foundMax Tag Length
Use the maxLength prop to limit the number of characters allowed per tag. This prevents users from creating overly
long tags.
Example not foundExample not foundExample not foundExample not foundProgrammatic Control
Use the useTagsInput hook with RootProvider to access the component's API methods like addValue(), setValue(),
and clearValue() for full programmatic control.
Example not foundExample not foundExample not foundExample not foundRead-only
Use the readOnly prop to make tags visible but not editable. Users can view tags but cannot add, remove, or modify
them.
Example not foundExample not foundExample not foundExample not foundValidating Tags
Before a tag is added, the validate function is called to determine whether to accept or reject a tag.
A common use-case for validating tags is preventing duplicates or validating the data type.
Example not foundExample not foundExample not foundExample not foundBlur behavior
When the tags input is blurred, you can configure the action the component should take by passing the blurBehavior
prop.
add— Adds the tag to the list of tags.clear— Clears the tags input value.
Example not foundExample not foundExample not foundExample not foundPaste behavior
To add a tag when a arbitrary value is pasted in the input element, pass the addOnPaste prop.
When a value is pasted, the component will:
- check if the value is a valid tag based on the
validateoption - split the value by the
delimiteroption passed
Example not foundExample not foundExample not foundExample not foundDisable tag editing
by default the tags can be edited by double-clicking on the tag or focusing on them and pressing
Enter. To disable this behavior, pass editable={false}
Example not foundExample not foundExample not foundExample not foundEvents
During the lifetime of the tags input interaction, here's a list of events we emit:
onValueChange— invoked when the tag value changes.onHighlightChange— invoked when a tag has visual focus.onValueInvalid— invoked when the max tag count is reached or thevalidatefunction returnsfalse.
Example not foundExample not foundExample not foundExample not foundUsing the Field Component
The Field component helps manage form-related state and accessibility attributes of a tags input. It includes handling
ARIA labels, helper text, and error text to ensure proper accessibility.
Example not foundExample not foundExample not foundExample not foundUsing the Root Provider
The RootProvider component provides a context for the tags-input. It accepts the value of the useTags-input hook.
You can leverage it to access the component state and methods from outside the tags-input.
Example not foundExample not foundExample not foundExample not foundIf you're using the
RootProvidercomponent, you don't need to use theRootcomponent.
Guides
Navigating and Editing tags
When the input has an empty value or the caret is at the start position, the tags can be selected by using the arrow left and arrow right keys. When "visual" focus in on any tag:
- Pressing Enter or double-clicking on the tag will put it in edit mode, allowing the user change its value and press Enter to commit the changes.
- Pressing Delete or Backspace will delete the tag that has visual focus.
API Reference
Props
Root
| Prop | Default | Type |
|---|---|---|
addOnPaste | false | booleanWhether to add a tag when you paste values into the tag input |
allowOverflow | booleanWhether to allow tags to exceed max. In this case, we'll attach `data-invalid` to the root | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
autoFocus | booleanWhether the input should be auto-focused | |
blurBehavior | 'clear' | 'add'The behavior of the tags input when the input is blurred - `"add"`: add the input value as a new tag - `"clear"`: clear the input value | |
defaultInputValue | stringThe initial tag input value when rendered. Use when you don't need to control the tag input value. | |
defaultValue | string[]The initial tag value when rendered. Use when you don't need to control the tag value. | |
delimiter | ',' | string | RegExpThe character that serves has: - event key to trigger the addition of a new tag - character used to split tags when pasting into the input |
disabled | booleanWhether the tags input should be disabled | |
editable | true | booleanWhether a tag can be edited after creation, by pressing `Enter` or double clicking. |
form | stringThe associate form of the underlying input element. | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
input: string
hiddenInput: string
clearBtn: string
label: string
control: string
item: (opts: ItemProps) => string
itemDeleteTrigger: (opts: ItemProps) => string
itemInput: (opts: ItemProps) => string
}>The ids of the elements in the tags input. Useful for composition. | |
inputValue | stringThe controlled tag input's value | |
invalid | booleanWhether the tags input is invalid | |
max | Infinity | numberThe max number of tags |
maxLength | numberThe max length of the input. | |
name | stringThe name attribute for the input. Useful for form submissions | |
onFocusOutside | (event: FocusOutsideEvent) => voidFunction called when the focus is moved outside the component | |
onHighlightChange | (details: HighlightChangeDetails) => voidCallback fired when a tag is highlighted by pointer or keyboard navigation | |
onInputValueChange | (details: InputValueChangeDetails) => voidCallback fired when the input value is updated | |
onInteractOutside | (event: InteractOutsideEvent) => voidFunction called when an interaction happens outside the component | |
onPointerDownOutside | (event: PointerDownOutsideEvent) => voidFunction called when the pointer is pressed down outside the component | |
onValueChange | (details: ValueChangeDetails) => voidCallback fired when the tag values is updated | |
onValueInvalid | (details: ValidityChangeDetails) => voidCallback fired when the max tag count is reached or the `validateTag` function returns `false` | |
readOnly | booleanWhether the tags input should be read-only | |
required | booleanWhether the tags input is required | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
validate | (details: ValidateArgs) => booleanReturns a boolean that determines whether a tag can be added. Useful for preventing duplicates or invalid tag values. | |
value | string[]The controlled tag value |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | root |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-disabled] | Present when disabled |
[data-focus] | Present when focused |
[data-empty] | Present when the content is empty |
ClearTrigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | clear-trigger |
[data-readonly] | Present when read-only |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | control |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-invalid] | Present when invalid |
[data-focus] | Present when focused |
HiddenInput
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Input
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | input |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-empty] | Present when the content is empty |
ItemDeleteTrigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemInput
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
ItemPreview
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | item-preview |
[data-value] | The value of the item |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
Item
| Prop | Default | Type |
|---|---|---|
index | string | number | |
value | string | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
disabled | boolean |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | item |
[data-value] | The value of the item |
[data-disabled] | Present when disabled |
ItemText
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | item-text |
[data-disabled] | Present when disabled |
[data-highlighted] | Present when highlighted |
Label
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | tags-input |
[data-part] | label |
[data-disabled] | Present when disabled |
[data-invalid] | Present when invalid |
[data-readonly] | Present when read-only |
[data-required] | Present when required |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseTagsInputReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
These are the properties available when using UtagsUinput.Context, useUtagsUinputContext hook or useUtagsUinput hook.
API
| Property | Type |
|---|---|
empty | booleanWhether the tags are empty |
inputValue | stringThe value of the tags entry input. |
value | string[]The value of the tags as an array of strings. |
valueAsString | stringThe value of the tags as a string. |
count | numberThe number of the tags. |
atMax | booleanWhether the tags have reached the max limit. |
setValue | (value: string[]) => voidFunction to set the value of the tags. |
clearValue | (id?: string) => voidFunction to clear the value of the tags. |
addValue | (value: string) => voidFunction to add a tag to the tags. |
setValueAtIndex | (index: number, value: string) => voidFunction to set the value of a tag at the given index. |
setInputValue | (value: string) => voidFunction to set the value of the tags entry input. |
clearInputValue | VoidFunctionFunction to clear the value of the tags entry input. |
focus | VoidFunctionFunction to focus the tags entry input. |
getItemState | (props: ItemProps) => ItemStateReturns the state of a tag |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
ArrowLeft | Moves focus to the previous tag item |
ArrowRight | Moves focus to the next tag item |
Backspace | Deletes the tag item that has visual focus or the last tag item |
Enter | When a tag item has visual focus, it puts the tag in edit mode. When the input has focus, it adds the value to the list of tags |
Delete | Deletes the tag item that has visual focus |
Control + V | When `addOnPaste` is set. Adds the pasted value as a tags |