- Spec files
npm-react-aria
Describes: pkg:npm/react-aria@3.43.x
- Description
- Comprehensive library of unstyled React hooks providing accessible UI primitives with full WAI-ARIA compliance and internationalization support.
- Author
- tessl
- Last updated
selection-controls.md docs/
1# Selection Controls23Components for selecting from lists of options, including listboxes, comboboxes, menus, and select dropdowns. All selection controls provide keyboard navigation, screen reader support, and proper ARIA semantics.45## Capabilities67### ListBox89Provides listbox behavior for selecting from a list of options with keyboard navigation and multi-selection support.1011```typescript { .api }12/**13* Provides listbox behavior and accessibility14* @param props - Listbox configuration15* @param state - Selection state management16* @param ref - Ref to the listbox element17* @returns Listbox props and state18*/19function useListBox<T>(props: AriaListBoxProps<T>, state: ListState<T>, ref: RefObject<Element>): ListBoxAria;2021/**22* Provides listbox section behavior for grouping options23* @param props - Section configuration24* @param ref - Ref to the section element25* @returns Section props26*/27function useListBoxSection<T>(props: AriaListBoxSectionProps<T>, ref: RefObject<Element>): ListBoxSectionAria;2829/**30* Provides individual option behavior within a listbox31* @param props - Option configuration32* @param state - Listbox state33* @param ref - Ref to the option element34* @returns Option props and state35*/36function useOption<T>(props: AriaOptionProps, state: ListState<T>, ref: RefObject<Element>): OptionAria;3738interface AriaListBoxProps<T> {39/** Item objects in the collection */40items?: Iterable<T>;41/** Selection mode */42selectionMode?: 'none' | 'single' | 'multiple';43/** Disable selection */44disallowEmptySelection?: boolean;45/** Currently selected keys */46selectedKeys?: 'all' | Iterable<Key>;47/** Default selected keys (uncontrolled) */48defaultSelectedKeys?: 'all' | Iterable<Key>;49/** Handler called when selection changes */50onSelectionChange?: (keys: Selection) => void;51/** Currently focused key */52focusedKey?: Key;53/** Default focused key (uncontrolled) */54defaultFocusedKey?: Key;55/** Handler called when focus changes */56onFocusChange?: (key: Key) => void;57/** Whether the listbox is disabled */58isDisabled?: boolean;59/** Handler called when an option is activated */60onAction?: (key: Key) => void;61/** Handler called when scrolling should occur */62onScroll?: (e: UIEvent<Element>) => void;63/** Whether to auto-focus the listbox */64autoFocus?: boolean | FocusStrategy;65/** Handler called when the listbox should scroll to an item */66shouldFocusWrap?: boolean;67/** Whether virtualized scrolling is used */68isVirtualized?: boolean;69/** Layout delegate for virtualized scrolling */70layoutDelegate?: LayoutDelegate;71}7273interface ListBoxAria {74/** Props for the listbox element */75listBoxProps: DOMAttributes<Element>;76}77```7879### ComboBox8081Provides combobox behavior combining a text input with a listbox popup for autocomplete functionality.8283```typescript { .api }84/**85* Provides combobox behavior and accessibility86* @param props - Combobox configuration87* @param state - Combobox state management88* @param ref - Ref to the combobox element89* @returns Combobox props and state90*/91function useComboBox<T>(props: AriaComboBoxProps<T>, state: ComboBoxState<T>, ref: RefObject<Element>): ComboBoxAria;9293interface AriaComboBoxProps<T> extends AriaComboBoxOptions<T> {94/** Current input value */95inputValue?: string;96/** Default input value (uncontrolled) */97defaultInputValue?: string;98/** Handler called when input value changes */99onInputChange?: (value: string) => void;100/** Whether the popup is open */101isOpen?: boolean;102/** Default open state (uncontrolled) */103defaultOpen?: boolean;104/** Handler called when open state changes */105onOpenChange?: (isOpen: boolean) => void;106/** Currently selected key */107selectedKey?: Key | null;108/** Default selected key (uncontrolled) */109defaultSelectedKey?: Key | null;110/** Handler called when selection changes */111onSelectionChange?: (key: Key | null) => void;112/** Disable selection */113disallowEmptySelection?: boolean;114/** Items in the collection */115items?: Iterable<T>;116/** Function to get display text for an item */117itemText?: (item: T) => string;118/** Handler called when an item is selected */119onAction?: (key: Key) => void;120/** Whether the combobox allows custom values */121allowsCustomValue?: boolean;122/** Menu trigger behavior */123menuTrigger?: 'focus' | 'input' | 'manual';124/** Whether the combobox is disabled */125isDisabled?: boolean;126/** Whether the combobox is read-only */127isReadOnly?: boolean;128/** Whether the combobox is required */129isRequired?: boolean;130/** Validation state */131validationState?: 'valid' | 'invalid';132/** Auto-complete behavior */133completionMode?: 'complete' | 'list' | 'both';134}135136interface ComboBoxAria {137/** Props for the combobox label */138labelProps: DOMAttributes<Element>;139/** Props for the text input element */140inputProps: InputHTMLAttributes<HTMLInputElement>;141/** Props for the listbox element */142listBoxProps: DOMAttributes<Element>;143/** Props for the popup button */144buttonProps: ButtonHTMLAttributes<HTMLButtonElement>;145/** Props for the description element */146descriptionProps: DOMAttributes<Element>;147/** Props for the error message element */148errorMessageProps: DOMAttributes<Element>;149}150```151152### Select153154Provides select dropdown behavior with keyboard navigation and accessibility.155156```typescript { .api }157/**158* Provides select behavior and accessibility159* @param props - Select configuration160* @param state - Select state management161* @param ref - Ref to the select element162* @returns Select props and state163*/164function useSelect<T>(props: AriaSelectProps<T>, state: SelectState<T>, ref: RefObject<Element>): SelectAria;165166/**167* Provides hidden select behavior for form integration168* @param props - Hidden select configuration169* @param ref - Ref to the select element170* @returns Select element properties171*/172function useHiddenSelect<T>(props: AriaHiddenSelectProps, ref: RefObject<HTMLSelectElement>): HiddenSelectProps;173174/**175* Hidden select component for form integration176* @param props - Hidden select configuration177* @returns Hidden select element178*/179function HiddenSelect<T>(props: HiddenSelectProps): JSX.Element;180181interface AriaSelectProps<T> extends AriaSelectOptions<T> {182/** Currently selected key */183selectedKey?: Key | null;184/** Default selected key (uncontrolled) */185defaultSelectedKey?: Key | null;186/** Handler called when selection changes */187onSelectionChange?: (key: Key | null) => void;188/** Whether the select is open */189isOpen?: boolean;190/** Default open state (uncontrolled) */191defaultOpen?: boolean;192/** Handler called when open state changes */193onOpenChange?: (isOpen: boolean) => void;194/** Items in the collection */195items?: Iterable<T>;196/** Disable empty selection */197disallowEmptySelection?: boolean;198/** Whether the select is disabled */199isDisabled?: boolean;200/** Whether the select is required */201isRequired?: boolean;202/** Validation state */203validationState?: 'valid' | 'invalid';204/** Auto-focus behavior */205autoFocus?: boolean;206/** Name for form integration */207name?: string;208}209210interface SelectAria {211/** Props for the select label */212labelProps: DOMAttributes<Element>;213/** Props for the trigger button */214triggerProps: ButtonHTMLAttributes<HTMLButtonElement>;215/** Props for the value element */216valueProps: DOMAttributes<Element>;217/** Props for the listbox */218listBoxProps: DOMAttributes<Element>;219/** Props for the description element */220descriptionProps: DOMAttributes<Element>;221/** Props for the error message element */222errorMessageProps: DOMAttributes<Element>;223}224```225226### Menu227228Provides menu behavior with hierarchical navigation and action handling.229230```typescript { .api }231/**232* Provides menu behavior and accessibility233* @param props - Menu configuration234* @param state - Menu state management235* @param ref - Ref to the menu element236* @returns Menu props and state237*/238function useMenu<T>(props: AriaMenuProps<T>, state: TreeState<T>, ref: RefObject<Element>): MenuAria;239240/**241* Provides menu trigger behavior for opening menus242* @param props - Menu trigger configuration243* @param state - Menu trigger state244* @param ref - Ref to the trigger element245* @returns Menu trigger props246*/247function useMenuTrigger<T>(props: AriaMenuTriggerProps, state: MenuTriggerState, ref: RefObject<Element>): MenuTriggerAria;248249/**250* Provides individual menu item behavior251* @param props - Menu item configuration252* @param state - Menu state253* @param ref - Ref to the menu item element254* @returns Menu item props and state255*/256function useMenuItem<T>(props: AriaMenuItemProps, state: TreeState<T>, ref: RefObject<Element>): MenuItemAria;257258/**259* Provides menu section behavior for grouping items260* @param props - Section configuration261* @param ref - Ref to the section element262* @returns Section props263*/264function useMenuSection<T>(props: AriaMenuSectionProps<T>, ref: RefObject<Element>): MenuSectionAria;265266/**267* Provides submenu trigger behavior268* @param props - Submenu trigger configuration269* @param state - Menu state270* @param ref - Ref to the trigger element271* @returns Submenu trigger props272*/273function useSubmenuTrigger<T>(props: AriaSubmenuTriggerProps, state: TreeState<T>, ref: RefObject<Element>): SubmenuTriggerAria;274275interface AriaMenuProps<T> extends AriaMenuOptions<T> {276/** Items in the menu */277items?: Iterable<T>;278/** Disabled keys */279disabledKeys?: Iterable<Key>;280/** Handler called when an item is selected */281onAction?: (key: Key) => void;282/** Handler called when menu should close */283onClose?: () => void;284/** Auto-focus behavior */285autoFocus?: boolean | FocusStrategy;286/** Whether focus should wrap */287shouldFocusWrap?: boolean;288}289290interface MenuAria {291/** Props for the menu element */292menuProps: DOMAttributes<Element>;293}294295interface AriaMenuTriggerProps {296/** Type of menu trigger */297type?: 'menu' | 'listbox' | 'tree' | 'grid' | 'dialog';298/** Whether the menu is disabled */299isDisabled?: boolean;300/** Trigger on which events */301trigger?: 'press' | 'longPress';302}303304interface MenuTriggerAria {305/** Props for the menu trigger element */306menuTriggerProps: ButtonHTMLAttributes<HTMLButtonElement>;307/** Props for the menu element */308menuProps: DOMAttributes<Element>;309}310```311312### GridList313314Provides grid list behavior for two-dimensional selection and navigation.315316```typescript { .api }317/**318* Provides grid list behavior and accessibility319* @param props - Grid list configuration320* @param state - Grid list state321* @param ref - Ref to the grid element322* @returns Grid list props and state323*/324function useGridList<T>(props: AriaGridListProps<T>, state: ListState<T>, ref: RefObject<Element>): GridListAria;325326/**327* Provides grid list item behavior328* @param props - Grid list item configuration329* @param state - Grid list state330* @param ref - Ref to the item element331* @returns Grid list item props and state332*/333function useGridListItem<T>(props: AriaGridListItemOptions<T>, state: ListState<T>, ref: RefObject<Element>): GridListItemAria;334335/**336* Provides selection checkbox behavior for grid list items337* @param props - Selection checkbox configuration338* @param state - Grid list state339* @param ref - Ref to the checkbox element340* @returns Selection checkbox props341*/342function useGridListSelectionCheckbox<T>(props: AriaGridSelectionCheckboxProps, state: ListState<T>, ref: RefObject<Element>): GridSelectionCheckboxAria;343344interface AriaGridListProps<T> extends AriaGridListOptions<T> {345/** Items in the grid */346items?: Iterable<T>;347/** Selection mode */348selectionMode?: 'none' | 'single' | 'multiple';349/** Selected keys */350selectedKeys?: 'all' | Iterable<Key>;351/** Default selected keys (uncontrolled) */352defaultSelectedKeys?: 'all' | Iterable<Key>;353/** Handler called when selection changes */354onSelectionChange?: (keys: Selection) => void;355/** Disabled keys */356disabledKeys?: Iterable<Key>;357/** Handler called when an item is activated */358onAction?: (key: Key) => void;359/** Whether the grid is disabled */360isDisabled?: boolean;361/** Auto-focus behavior */362autoFocus?: boolean | FocusStrategy;363}364365interface GridListAria {366/** Props for the grid list element */367gridProps: DOMAttributes<Element>;368}369```370371### Keyboard Delegates372373```typescript { .api }374/**375* Keyboard navigation delegate for list components376*/377class ListKeyboardDelegate {378/** Get the key above the current key */379getKeyAbove(key: Key): Key | null;380/** Get the key below the current key */381getKeyBelow(key: Key): Key | null;382/** Get the key to the left of the current key */383getKeyLeftOf(key: Key): Key | null;384/** Get the key to the right of the current key */385getKeyRightOf(key: Key): Key | null;386/** Get the first key */387getFirstKey(): Key | null;388/** Get the last key */389getLastKey(): Key | null;390/** Get the key for a character */391getKeyForSearch(search: string, fromKey?: Key): Key | null;392}393```394395## Types396397```typescript { .api }398type Selection = 'all' | Set<Key>;399400type SelectionMode = 'none' | 'single' | 'multiple';401402type FocusStrategy = 'first' | 'last';403404interface ListState<T> {405/** Collection of items */406collection: Collection<Node<T>>;407/** Set of selected keys */408selectedKeys: Selection;409/** Currently focused key */410focusedKey: Key | null;411/** Whether the collection allows empty selection */412disallowEmptySelection: boolean;413/** Selection mode */414selectionMode: SelectionMode;415/** Set of disabled keys */416disabledKeys: Set<Key>;417/** Select an item */418setSelectedKeys(keys: Selection): void;419/** Toggle selection of an item */420toggleSelection(key: Key): void;421/** Replace selection with a single item */422replaceSelection(key: Key): void;423/** Select all items */424selectAll(): void;425/** Clear all selection */426clearSelection(): void;427/** Set the focused key */428setFocusedKey(key: Key): void;429}430431interface ComboBoxState<T> extends ListState<T> {432/** Current input value */433inputValue: string;434/** Set the input value */435setInputValue(value: string): void;436/** Currently selected item */437selectedItem: Node<T> | null;438/** Currently selected key */439selectedKey: Key | null;440/** Set the selected key */441setSelectedKey(key: Key | null): void;442/** Whether the popup is open */443isOpen: boolean;444/** Set the open state */445setOpen(isOpen: boolean): void;446/** Toggle the open state */447toggle(): void;448/** Open the popup */449open(): void;450/** Close the popup */451close(): void;452/** Commit the current input value */453commit(): void;454/** Revert to the selected item's text */455revert(): void;456}457458interface SelectState<T> {459/** Whether the select is open */460isOpen: boolean;461/** Set the open state */462setOpen(isOpen: boolean): void;463/** Currently selected key */464selectedKey: Key | null;465/** Set the selected key */466setSelectedKey(key: Key | null): void;467/** Currently selected item */468selectedItem: Node<T> | null;469/** Collection of items */470collection: Collection<Node<T>>;471/** Toggle the open state */472toggle(): void;473/** Open the select */474open(): void;475/** Close the select */476close(): void;477}478```