Select API
Import
Section titled “Import”Cimpress UI exports three select-related components:
Select: main component that allows users to select one item from a collapsible list.SelectItem: component that renders a single selectable item within a select.SelectSection: component that groups related select items together under a section title.
import { Select, SelectItem, SelectSection } 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 { Select, SelectItem, Stack, type Key } from '@cimpress-ui/react';import { useState } from 'react';
export default function Demo() { const [selectedKey, setSelectedKey] = useState<Key | null>('apple');
return ( <Stack gap={32}> <Select label="Favorite fruit (controlled)" selectedKey={selectedKey} onSelectionChange={setSelectedKey}> <SelectItem id="apple">Apple</SelectItem> <SelectItem id="banana">Banana</SelectItem> <SelectItem id="cherry">Cherry</SelectItem> </Select>
<Select label="Favorite fruit (uncontrolled)" defaultSelectedKey="apple"> <SelectItem id="apple">Apple</SelectItem> <SelectItem id="banana">Banana</SelectItem> <SelectItem id="cherry">Cherry</SelectItem> </Select> </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, Select, type SelectApi, SelectItem, Stack } from '@cimpress-ui/react';import { useRef } from 'react';
export default function Demo() { const apiRef = useRef<SelectApi>(null);
return ( <Stack gap={16}> <Select label="Favorite fruit" defaultSelectedKey="apple" apiRef={apiRef}> <SelectItem id="apple">Apple</SelectItem> <SelectItem id="banana">Banana</SelectItem> <SelectItem id="cherry">Cherry</SelectItem> </Select>
<Button onPress={() => apiRef.current?.focus()}>Focus</Button> </Stack> );}Accessibility notes
Section titled “Accessibility notes”Collapsible select list can be opened by focusing on the select trigger and pressing Enter, Space, Up Arrow, or Down Arrow.
Collapsible select list follows the ARIA listbox pattern. See the linked page for a list of available keyboard interactions.
Select requires a textual label to remain accessible to assistive technologies. See our accessibility guide for more details.
Collections
Section titled “Collections”Select is a collection component. See our collection components guide to learn how to work with collections.
Select can integrate with native HTML forms. See our forms guide to learn how to work with forms.
API reference
Section titled “API reference”Select
Section titled “Select”Allows users to select one item from a collapsible list.
- Ref<HTMLDivElement>
-
The
reftype for this component.
SelectProps<T extends CollectionItem = CollectionItem>
- Key | null
selectedKey
Section titled “ selectedKey ” -
The currently selected key in the collection (controlled).
- Key
defaultSelectedKey
Section titled “ defaultSelectedKey ” -
The initial selected key in the collection (uncontrolled).
- (key: Key | null) => 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.
- 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: Key) => 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. - 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.
- boolean
UNSTABLE_isLoading
Section titled “ UNSTABLE_isLoading ” -
Whether items are currently being loaded.
- () => void
UNSTABLE_onLoadMore
Section titled “ UNSTABLE_onLoadMore ” -
A callback function that is called when more items should be loaded.
- RefObject<SelectApi | null>
apiRef
Section titled “ apiRef ” -
A React ref that allows access to the imperative API of this component.
- (isOpen: boolean) => void
onOpenChange
Section titled “ onOpenChange ” -
Method that is called when the open state of the menu changes.
- string
autoComplete
Section titled “ autoComplete ” -
Describes the type of autocomplete functionality the input should provide if any. See MDN.
- (e: FocusEvent<Element>) => void
onFocus
Section titled “ onFocus ” -
Handler that is called when the element receives focus.
- (e: FocusEvent<Element>) => 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.
- string
placeholder
Section titled “ placeholder ” - Defaults to 'Select an item' (localized) .
Temporary text that occupies the select when it is empty.
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.
SelectItem
Section titled “SelectItem”Renders a single list item within Select.
SelectItemProps
- 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.
SelectSection
Section titled “SelectSection”Groups list items within Select into a section.
SelectSectionProps<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.