Tag field API
Import
Section titled “Import”Cimpress UI exports three tag field-related components:
TagField: main component that allows users to select multiple items from a collapsible list.TagFieldItem: component that renders a single selectable item within a tag field.TagFieldSection: component that groups related tag field items together under a section title.
import { TagField, TagFieldItem, TagFieldSection } from '@cimpress-ui/react';Controlled/uncontrolled usage
Section titled “Controlled/uncontrolled usage”This component can be used in a controlled or uncontrolled way.
In the controlled way, this component doesn’t maintain its own internal state, and its value is provided by the parent component. Use the controlled way when you need to be able to change the state of this component in other parts of your application.
In the uncontrolled way, this component maintains its own internal state, and can optionally notify the parent component when its internal state changes. Use the uncontrolled way when you don’t need to change the state of this component in other parts of your application.
import { Stack, TagField, TagFieldItem, type Selection } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [selectedKeys, setSelectedKeys] = useState<Selection>(() => new Set(['apple', 'cherry']));
return ( <Stack gap={32}> <TagField label="Favorite fruits (controlled)" selectedKeys={selectedKeys} onSelectionChange={setSelectedKeys}> <TagFieldItem id="apple">Apple</TagFieldItem> <TagFieldItem id="banana">Banana</TagFieldItem> <TagFieldItem id="cherry">Cherry</TagFieldItem> </TagField>
<TagField label="Favorite fruits (uncontrolled)" defaultSelectedKeys={['apple', 'cherry']}> <TagFieldItem id="apple">Apple</TagFieldItem> <TagFieldItem id="banana">Banana</TagFieldItem> <TagFieldItem id="cherry">Cherry</TagFieldItem> </TagField> </Stack> );}Imperative API
Section titled “Imperative API”This component exposes an imperative API through the apiRef prop. This API allows triggering behaviors that can’t be expressed by props.
import { Button, Stack, TagField, type TagFieldApi, TagFieldItem } from '@cimpress-ui/react';import { useRef } from 'react';
export default function Demo() { const apiRef = useRef<TagFieldApi>(null);
return ( <Stack gap={16}> <TagField label="Favorite fruits" defaultSelectedKeys={['apple', 'cherry']} apiRef={apiRef}> <TagFieldItem id="apple">Apple</TagFieldItem> <TagFieldItem id="banana">Banana</TagFieldItem> <TagFieldItem id="cherry">Cherry</TagFieldItem> </TagField>
<Button onPress={() => apiRef.current?.focus()}>Focus</Button> </Stack> );}Accessibility notes
Section titled “Accessibility notes”Tag field follows the ARIA combobox pattern. See the linked page for a list of available keyboard interactions.
Collapsible tag field list follows the ARIA listbox pattern. See the linked page for a list of available keyboard interactions.
The list of tags displayed next to the field is a single tab stop within the page’s tab order. Use the Tab key to navigate to the list, and the arrow keys to navigate between tags within the list. Use the Backspace or Delete key to remove tags from the list.
TagField requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Collections
Section titled “Collections”Tag field is a collection component. See our collection components guide to learn how to work with collections.
Tag field can integrate with native HTML forms. See our forms guide to learn how to work with forms.
API reference
Section titled “API reference”TagField
Section titled “TagField”Allows users to filter a collapsible list and select multiple items from it.
- Ref<HTMLDivElement>
-
The
reftype for this component.
TagFieldProps<T extends CollectionItem = CollectionItem>
- Iterable<Key, any, any>
selectedKeys
Section titled “ selectedKeys ” -
The currently selected keys in the collection (controlled).
- Iterable<Key, any, any>
defaultSelectedKeys
Section titled “ defaultSelectedKeys ” -
The initial selected keys in the collection (uncontrolled).
- (keys: Set<Key>) => void
onSelectionChange
Section titled “ onSelectionChange ” -
Handler that is called when the selection changes.
- boolean
isVirtualized
Section titled “ isVirtualized ” - Defaults to false .
Whether the list of options should be virtualized. Use this as a performance optimization for large lists.
- UIEventHandler<HTMLDivElement>
onScroll
Section titled “ onScroll ” -
Handler that is called when the list of items is scrolled.
- () => ReactNode
renderEmptyState
Section titled “ renderEmptyState ” -
Provides content to display when there are no tags in the group.
- string
-
The element's unique identifier. See MDN.
- boolean
data-cim-style-root
Section titled “ data-cim-style-root ” -
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- string
UNSAFE_className
Section titled “ UNSAFE_className ” -
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- CSSProperties
UNSAFE_style
Section titled “ UNSAFE_style ” -
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- string
label
Section titled “ label ” -
The content to display as the label.
- string
aria-label
Section titled “ aria-label ” -
Defines a string value that labels the current element.
- string
aria-labelledby
Section titled “ aria-labelledby ” -
Identifies the element (or elements) that labels the current element.
- string
aria-describedby
Section titled “ aria-describedby ” -
Identifies the element (or elements) that describes the object.
- string
aria-details
Section titled “ aria-details ” -
Identifies the element (or elements) that provide a detailed, extended description for the object.
- string
-
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN. - string
-
The name of the input element, used when submitting an HTML form. See MDN.
- string
description
Section titled “ description ” -
A description for the field. Provides a hint such as specific requirements for what to choose.
- FieldError
error
Section titled “ error ” -
An error message for the field.
- (value: TagFieldValidationValue) => string | true | string[] | undefined
validate
Section titled “ validate ” -
A function that returns an error message (or
true) if a given value is invalid. Validation errors are displayed to the user when the form is submitted. For real-time validation, use theerrorprop instead. - string
placeholder
Section titled “ placeholder ” -
The placeholder text displayed in the input field.
- ReactNode | ((item: T) => ReactNode)
children
Section titled “ children ” -
The contents of the collection.
- Iterable<T, any, any>
items
Section titled “ items ” -
The items to display in the collection.
- RefObject<TagFieldApi | null>
apiRef
Section titled “ apiRef ” -
A React ref that allows access to the imperative API of this component.
- (isOpen: boolean, menuTrigger?: MenuTriggerAction) => void
onOpenChange
Section titled “ onOpenChange ” -
Method that is called when the open state of the menu changes. Returns the new open state and the action that caused the opening of the menu.
- string
inputValue
Section titled “ inputValue ” -
The value of the ComboBox input (controlled).
- string
defaultInputValue
Section titled “ defaultInputValue ” -
The default value of the ComboBox input (uncontrolled).
- (value: string) => void
onInputChange
Section titled “ onInputChange ” -
Handler that is called when the ComboBox input value changes.
- (e: FocusEvent<HTMLInputElement>) => void
onFocus
Section titled “ onFocus ” -
Handler that is called when the element receives focus.
- (e: FocusEvent<HTMLInputElement>) => void
onBlur
Section titled “ onBlur ” -
Handler that is called when the element loses focus.
- boolean
autoFocus
Section titled “ autoFocus ” -
Whether the element should receive focus on render.
- boolean
isRequired
Section titled “ isRequired ” -
Whether user input is required on the input before form submission.
- boolean
isInvalid
Section titled “ isInvalid ” -
Whether the input value is invalid.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the input is disabled.
- boolean
isReadOnly
Section titled “ isReadOnly ” -
Whether the input can be selected but not changed by the user.
StyleProps
- Responsive<Spacing | "auto">
margin
Section titled “ margin ” -
The amount of margin applied to all edges of this component.
- Responsive<Spacing | "auto">
marginX
Section titled “ marginX ” -
The amount of margin applied to the left and right edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginY
Section titled “ marginY ” -
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin. - Responsive<Spacing | "auto">
marginTop
Section titled “ marginTop ” -
The amount of margin applied to the top edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginRight
Section titled “ marginRight ” -
The amount of margin applied to the right edge of this component. Takes priority over
marginXandmargin. - Responsive<Spacing | "auto">
marginBottom
Section titled “ marginBottom ” -
The amount of margin applied to the bottom edge of this component. Takes priority over
marginYandmargin. - Responsive<Spacing | "auto">
marginLeft
Section titled “ marginLeft ” -
The amount of margin applied to the left edge of this component. Takes priority over
marginXandmargin.
TagFieldItem
Section titled “TagFieldItem”Renders a single list item within TagField.
TagFieldItemProps<T extends CollectionItem>
- StringLikeChildren
children *
Section titled “ children * ” -
The content to display as the label.
- Key
-
The ID of the item. Has to be unique across all sections and items.
- (e: HoverEvent) => void
onHoverStart
Section titled “ onHoverStart ” -
Handler that is called when a hover interaction starts.
- (e: HoverEvent) => void
onHoverEnd
Section titled “ onHoverEnd ” -
Handler that is called when a hover interaction ends.
- boolean
isDisabled
Section titled “ isDisabled ” -
Whether the item is disabled.
TagFieldSection
Section titled “TagFieldSection”Groups list items within TagField into a section.
TagFieldSectionProps<T extends CollectionItem>
- string
title *
Section titled “ title * ” -
The content to display as the section title.
- Key
-
The ID of the section. Has to be unique across all sections and items.
- ReactNode | ((item: T) => ReactNode)
children
Section titled “ children ” -
The contents of the collection.
- Iterable<T, any, any>
items
Section titled “ items ” -
The items to display in the collection.