TagsInput
Capture a list of values from user with free input and suggestions
Import
Source
Docs
Package
Made with Combobox
TagsInput
is an opinionated component built on top of Combobox component. It has a limited set of features to cover only the basic use cases. If you need more advanced features, you can build your own component with Combobox. You can find examples of custom tags input components on the examples page.
Usage
TagsInput
provides a way to enter multiple values. It can be used with suggestions or without them.
TagsInput
is similar to MultiSelect, but it allows entering custom values.
Controlled
TagsInput
value must be an array of strings, other types are not supported.
onChange
function is called with an array of strings as a single argument.
Controlled search value
You can control search value with searchValue
and onSearchChange
props:
Clearable
Set clearable
prop to display the clear button in the right section. The button is not displayed
when:
- The component does not have a value
- The component is disabled
- The component is read only
Max selected values
You can limit the number of selected values with maxTags
prop. This will not allow adding more values
once the limit is reached.
Add up to 3 tags
Accept value on blur
By default, if the user types a value and blurs the input, the value is added to the list.
You can change this behavior by setting acceptValueOnBlur
to false
. In this case, the value is added
only when the user presses Enter
or clicks on a suggestion.
Allow duplicates
By default TagsInput
does not allow to add duplicate values, but you can change this behavior by
setting allowDuplicates
prop. Value is considered duplicate if it is already present in the value
array,
regardless of the case and trailing whitespace.
Split chars
By default, TagsInput
splits values by comma (,
), you can change this behavior by setting
splitChars
prop to an array of strings. All values from splitChars
cannot be included in the final value.
Values are also splitted on paste.
Example of splitting by ,
, |
and space:
With suggestions
TagsInput
can be used with suggestions, it will render suggestions list under input and allow to select
suggestions with keyboard or mouse. Note that user is not limited to suggestions, it is still possible to
enter custom values. If you want to allow values only from suggestions, use MultiSelect component instead.
Data formats
TagsInput
data
prop accepts data in one of the following formats:
Array of strings:
Array of groups with string options:
Options filtering
By default,TagsInput
filters options by checking if the option label contains input value. You can change this behavior with filter
prop.filter
function receives an object with the following properties as a single argument:options
– array of options or options groups, all options are in{ value: string; label: string; disabled?: boolean }
formatsearch
– current search querylimit
– value oflimit
prop passed toTagsInput
Example of a custom filter function that matches options by words instead of letters sequence:
Sort options
By default, options are sorted by their position in the data array. You can change this behavior
with filter
function:
Large data sets
The best strategy for large data sets is to limit the number of options that are rendered at the same time. You can do it with limit
prop. Note that if you use a custom filter
function, you need to implement your own logic to limit the number of options in filter
Example of TagsInput
with 100 000 options, 5 options are rendered at the same time:
renderOption
renderOption
callback allows you to customize option rendering. It is called with option object.
The function must return a React node.
Scrollable dropdown
By default, the options list is wrapped with ScrollArea.Autosize.
You can control dropdown max-height with maxDropdownHeight
prop if you do not change the default settings.
If you want to use native scrollbars, set withScrollArea={false}
. Note that in this case,
you will need to change dropdown styles with Styles API.
Group options
Disabled options
When option is disabled, it cannot be selected and is ignored in keyboard navigation.
Note that user can still enter disabled option as a value. If you want to prohibit certain values,
use controlled component and filter them out in onChange
function.
Combobox props
You can override Combobox props with comboboxProps
. It is useful when you need to change some of the props that are not exposed by TagsInput
, for example withinPortal
:
Change dropdown z-index
Inside Popover
To use TagsInput
inside popover, you need to set withinPortal: false
:
Control dropdown opened state
You can control dropdown opened state with dropdownOpened
prop. Additionally,
you can use onDropdownClose
and onDropdownOpen
to listen to dropdown opened state changes.
Dropdown position
By default, the dropdown is displayed below the input if there is enough space; otherwise it is displayed above the input.
You can change this behavior by setting position
and middlewares
props, which are passed down to the
underlying Popover component.
Example of dropdown that is always displayed above the input:
Dropdown animation
By default, dropdown animations are disabled. To enable them, you can set transitionProps
,
which will be passed down to the underlying Transition component.
Dropdown width
To change dropdown width, set width
prop in comboboxProps
. By default,
dropdown width is equal to the input width.
Dropdown padding
Dropdown shadow
Left and right sections
TagsInput
supports leftSection
and rightSection
props. These sections are rendered with absolute position inside the input wrapper. You can use them to display icons, input controls or any other elements.
You can use the following props to control sections styles and content:
rightSection
/leftSection
– React node to render on the corresponding side of inputrightSectionWidth
/leftSectionWidth
– controls width of the right section and padding on the corresponding side of the input. By default, it is controlled by componentsize
prop.rightSectionPointerEvents
/leftSectionPointerEvents
– controlspointer-events
property of the section. If you want to render a non-interactive element, set it tonone
to pass clicks through to the input.
Input props
TagsInput
component supports Input and Input.Wrapper components features and all input
element props. TagsInput
documentation does not include all features supported by the component – see Input documentation to learn about all available features.
Input description
Read only
Set readOnly
to make the input read only. When readOnly
is set,
TagsInput
will not show suggestions and will not call onChange
function.
Disabled
Set disabled
to disable the input. When disabled
is set,
user cannot interact with the input and TagsInput
will not show suggestions.
Error state
Invalid name
Styles API
TagsInput
supports Styles API, you can add styles to any inner element of the component withclassNames
prop. Follow Styles API documentation to learn more.
Description
Component Styles API
Hover over selectors to highlight corresponding elements
Get element ref
Accessibility
If TagsInput
is used without label
prop, it will not be announced properly by screen reader:
Set aria-label
to make the input accessible. In this case label will not be visible, but screen reader will announce it:
If label
prop is set, input will be accessible it is not required to set aria-label
:
To set aria-label
on the clear button, use clearButtonProps
. Note that it is required
only when clearable
is set.