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> );}
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.
API reference
Section titled “API reference”TagField
Section titled “TagField”Allows users to filter a collapsible list and select multiple items from it.
- ref Ref<HTMLDivElement>
-
The
ref
type for this component.
TagFieldProps<T extends CollectionItem = CollectionItem>
- selectedKeys Iterable<Key, any, any>
-
The currently selected keys in the collection (controlled).
- defaultSelectedKeys Iterable<Key, any, any>
-
The initial selected keys in the collection (uncontrolled).
- onSelectionChange (keys: Set<Key>) => void
-
Handler that is called when the selection changes.
- isVirtualized boolean
-
Whether the list of options should be virtualized. Use this as a performance optimization for large lists.
- renderEmptyState () => ReactNode
-
Provides content to display when there are no tags in the group.
- id string
-
The element's unique identifier. See MDN.
- data-cim-style-root boolean
-
Use this attribute to "claim" the component tree for exclusive Cimpress UI usage.
- UNSAFE_className string
-
Sets the CSS className for the element. Only use as a last resort. Use style props instead.
See styling guide.
- UNSAFE_style CSSProperties
-
Sets the CSS style for the element. Only use as a last resort. Use style props instead.
See styling guide.
- label string
-
The content to display as the label.
- aria-label string
-
Defines a string value that labels the current element.
- aria-labelledby string
-
Identifies the element (or elements) that labels the current element.
- aria-describedby string
-
Identifies the element (or elements) that describes the object.
- aria-details string
-
Identifies the element (or elements) that provide a detailed, extended description for the object.
- name string
-
The name of the input element, used when submitting an HTML form. See MDN.
- description string
-
A description for the field. Provides a hint such as specific requirements for what to choose.
- error FieldError
-
An error message for the field.
- validate (value: TagFieldValidationValue) => undefined | string | true | string[]
-
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 theerror
prop instead. - placeholder string
-
The placeholder text displayed in the input field.
- children ReactNode | ((item: T) => ReactNode)
-
The contents of the collection.
- items Iterable<T, any, any>
-
The items to display in the collection.
- onOpenChange (isOpen: boolean, menuTrigger?: MenuTriggerAction) => void
-
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.
- inputValue string
-
The value of the ComboBox input (controlled).
- defaultInputValue string
-
The default value of the ComboBox input (uncontrolled).
- onInputChange (value: string) => void
-
Handler that is called when the ComboBox input value changes.
- onFocus (e: FocusEvent<HTMLInputElement>) => void
-
Handler that is called when the element receives focus.
- onBlur (e: FocusEvent<HTMLInputElement>) => void
-
Handler that is called when the element loses focus.
- autoFocus boolean
-
Whether the element should receive focus on render.
- isRequired boolean
-
Whether user input is required on the input before form submission.
- isInvalid boolean
-
Whether the input value is invalid.
- isDisabled boolean
-
Whether the input is disabled.
- isReadOnly boolean
-
Whether the input can be selected but not changed by the user.
- onScroll (e: UIEvent<HTMLDivElement>) => void
-
Handler that is called when a user scrolls. See MDN.
StyleProps
- margin Responsive<Spacing | "auto">
-
The amount of margin applied to all edges of this component.
- marginX Responsive<Spacing | "auto">
-
The amount of margin applied to the left and right edges of this component. Takes priority over
margin
. - marginY Responsive<Spacing | "auto">
-
The amount of margin applied to the top and bottom edges of this component. Takes priority over
margin
. - marginTop Responsive<Spacing | "auto">
-
The amount of margin applied to the top edge of this component. Takes priority over
marginY
andmargin
. - marginRight Responsive<Spacing | "auto">
-
The amount of margin applied to the right edge of this component. Takes priority over
marginX
andmargin
. - marginBottom Responsive<Spacing | "auto">
-
The amount of margin applied to the bottom edge of this component. Takes priority over
marginY
andmargin
. - marginLeft Responsive<Spacing | "auto">
-
The amount of margin applied to the left edge of this component. Takes priority over
marginX
andmargin
.
TagFieldItem
Section titled “TagFieldItem”Renders a single list item within TagField
.
TagFieldItemProps<T extends CollectionItem>
- children * StringLikeChildren
-
The content to display as the label.
- onHoverStart (e: HoverEvent) => void
-
Handler that is called when a hover interaction starts.
- onHoverEnd (e: HoverEvent) => void
-
Handler that is called when a hover interaction ends.
- id Key
-
The unique id of the item.
- isDisabled boolean
-
Whether the item is disabled.
TagFieldSection
Section titled “TagFieldSection”Groups list items within TagField
into a section.
TagFieldSectionProps<T extends CollectionItem>
- title * string
-
The content to display as the section title.
- id Key
-
The ID of the section. Has to be unique across all sections and items.
- children ReactNode | ((item: T) => ReactNode)
-
The contents of the collection.
- items Iterable<T, any, any>
-
The items to display in the collection.