Skip to content

Tags

Tag input component with autocomplete suggestions and free-form entry.

Also known as: tag input, chip input, token input, keyword input

View in Storybook

When to use

  • Entering multiple tags/keywords with autocomplete suggestions. Use freeSolo for custom tags.
  • For selecting from a fixed list, use MultiSelect.

Import

tsx
import { Tags } from '@vacano/ui'

Usage

tsx
const [tags, setTags] = useState<string[]>([])

<Tags
  value={tags}
  onChange={setTags}
  placeholder="Add tags..."
/>

With Suggestions

tsx
const options = [
  { value: 'react', label: 'React' },
  { value: 'typescript', label: 'TypeScript' },
  { value: 'javascript', label: 'JavaScript' },
]

<Tags
  value={tags}
  options={options}
  onChange={setTags}
  placeholder="Select or type tags"
/>

Free Solo Mode

Allow creating tags not in the options list (enabled by default):

tsx
<Tags
  value={tags}
  options={options}
  onChange={setTags}
  freeSolo
  placeholder="Type to add custom tags"
/>

Disable free solo to restrict to options only:

tsx
<Tags
  value={tags}
  options={options}
  onChange={setTags}
  freeSolo={false}
  placeholder="Select from options only"
/>

Create Key

Choose which key creates a new tag:

tsx
// Tab key creates tag (default)
<Tags createKey="Tab" value={tags} onChange={setTags} />

// Enter key creates tag
<Tags createKey="Enter" value={tags} onChange={setTags} />

With Label

tsx
<Tags
  label="Skills"
  value={tags}
  options={options}
  onChange={setTags}
/>

Custom Empty Message

tsx
<Tags
  value={tags}
  options={options}
  onChange={setTags}
  emptyMessage="No matching tags found"
/>

Variants

tsx
<Tags variant="normal" value={tags} onChange={setTags} />
<Tags variant="error" value={tags} onChange={setTags} />

With Message

tsx
<Tags label="Tags" variant="error" value={tags} onChange={setTags} message="At least one tag is required" />
<Tags label="Keywords" value={tags} onChange={setTags} message="Add up to 5 keywords" />

Disabled

tsx
<Tags disabled value={['react', 'vue']} onChange={() => {}} />

Portal Rendering

For use inside containers with overflow: hidden:

tsx
<Tags
  value={tags}
  options={options}
  onChange={setTags}
  portalRenderNode={document.body}
/>

Props

PropTypeDefaultDescription
valuestring[]requiredSelected tag values
onChange(value: string[]) => voidrequiredChange handler
optionsTagsOption[][]Suggestion options
labelstring-Label text
placeholderstring'Add tag...'Input placeholder
variant'normal' | 'error''normal'Visual variant
disabledbooleanfalseDisable input
messagestring-Message text below input (error, hint)
freeSolobooleantrueAllow creating custom tags not in the options list
createKey'Tab' | 'Enter''Tab'Key to create tag
emptyMessageReactNode'No options'Empty state message when no suggestions match
portalRenderNodeHTMLElement | null-Portal target for dropdown
classNamestring-CSS class name for the root element
classnamesTagsClassNames-Custom class names for inner elements
refRef<HTMLDivElement>-Ref to the root container element
data-test-idstring-Test identifier attribute

Option Type

tsx
type TagsOption = {
  value: string
  label: string
}

ClassNames

KeyDescription
triggerContainer element
chipTag chip
inputText input
dropdownSuggestions dropdown
optionSuggestion option
emptyEmpty state

Released under the MIT License.