Collection interface
A generic interface to access a readonly sequential collection of unique keyed items.
Introduction#
The interface provides a generic representation
for many types of collections. The collection is read only (immutable), and sequential (ordered). Each item
has a unique key that identifies it, which can be used to access items. It's like a combination
of an array and a map: you can iterate over items in a well defined order, and also access items by key.
Interface#
Properties
Name | Type | Description |
size | number | The number of items in the collection. |
Methods
Method | Description |
getKeys(): Iterable<Key> | Iterate over all keys in the collection. |
getItem(
(key: Key
)): T | Get an item by its key. |
getKeyBefore(
(key: Key
)): Key | null | Get the key that comes before the given key in the collection. |
getKeyAfter(
(key: Key
)): Key | null | Get the key that comes after the given key in the collection. |
getFirstKey(): Key | null | Get the first key in the collection. |
getLastKey(): Key | null | Get the last key in the collection. |
Building a collection#
The interface can be implemented in many ways.
For example, you could write a class to expose the required methods and properties for an underlying data structure
like an array or Map.
As discussed on the Collection Components page, React Spectrum implements a JSX-based
API for defining collections. This is implemented by the
hook.
useCollection
iterates over the JSX tree, and recursively builds a collection
of objects. Each node includes the value of the item
it represents, along with some metadata about its place in the collection.
These nodes are then wrapped in a class that implements the
interface, and exposes the nodes. State management hooks like
and
handle building the collection and exposing
the necessary interface to components.
When implementing collection components using react-aria hooks
like ,
and
, you'll iterate over these nodes
to render the data in the collection. The properties exposed by a node are described below.
Node interface#
Name | Type | Description |
type | string | The type of item this node represents. |
key | Key | A unique key for the node. |
value | T | The object value the node was created from. |
level | number | The level of depth this node is at in the heirarchy. |
hasChildNodes | boolean | Whether this item has children, even if not loaded yet. |
childNodes | Iterable<<T>> | The loaded children of this node. |
rendered | ReactNode | The rendered contents of this node (e.g. JSX). |
textValue | string | A string value for this node, used for features like typeahead. |
aria-label | string | An accessibility label for this node. |
index | number | The index of this node within its parent. |
wrapper | (
(element: ReactElement
)) => ReactElement | A function that should be called to wrap the rendered node. |
parentKey | Key | The key of the parent node. |
prevKey | Key | The key of the node before this node. |
nextKey | Key | The key of the node after this node. |
props | any | Additional properties specific to a particular node type. |
useCollection<T, C = <<T>>>(
props: <T>,
factory: <T, C>,
context: unknown
): any
(
(node: Iterable<<T>>,
, prev: C
| | null
)) => C
Provides state management for list-like components. Handles building a collection of items from props, and manages multiple selection state.
useListState<T>(
(props: <T>
)): <T>
Name | Type | Description |
children | <T> | The contents of the collection. |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
selectionMode |
| The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys:
)) => any | Handler that is called when the selection changes. |
Name | Type | Description |
collection | <<T>> | A collection of items in the list. |
disabledKeys | Set<Key> | A set of items that are disabled. |
selectionManager |
| A selection manager to read and update multiple selection state. |
An interface for reading and updating multiple selection state.
Properties
Name | Type | Description |
selectionMode |
| The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
selectedKeys | Set<Key> | The currently selected keys in the collection. |
isEmpty | boolean | Whether the selection is empty. |
isSelectAll | boolean | Whether all items in the collection are selected. |
Methods
Method | Description |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key
)): void | Sets the focused key. |
isSelected(
(key: Key
)): void | Returns whether a key is selected. |
extendSelection(
(toKey: Key
)): void | Extends the selection to the given key. |
toggleSelection(
(key: Key
)): void | Toggles whether the given key is selected. |
replaceSelection(
(key: Key
)): void | Replaces the selection with only the given key. |
selectAll(): void | Selects all items in the collection. |
clearSelection(): void | Removes all keys from the selection. |
toggleSelectAll(): void | Toggles between select all and an empty selection. |
Properties
Name | Type | Description |
selectionMode |
| The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys |
| The currently selected keys in the collection. |
isFocused | boolean | Whether the collection is currently focused. |
focusedKey | Key | The current focused key in the collection. |
Methods
Method | Description |
setSelectedKeys(
(keys:
| | (
(v:
)) =>
)): void | Sets the selected keys in the collection. |
setFocused(
(isFocused: boolean
)): void | Sets whether the collection is focused. |
setFocusedKey(
(key: Key
)): void | Sets the focused key. |
Provides state management for tree-like components. Handles building a collection of items from props, item expanded state, and manages multiple selection state.
useTreeState<T>(
(props: <T>
)): <T>
Name | Type | Description |
children | <T> | The contents of the collection. |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
expandedKeys | Iterable<Key> | The currently expanded keys in the collection (controlled). |
defaultExpandedKeys | Iterable<Key> | The initial expanded keys in the collection (uncontrolled). |
onExpandedChange | (
(keys: Set<Key>
)) => any | Handler that is called when items are expanded or collapsed. |
selectionMode |
| The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys:
)) => any | Handler that is called when the selection changes. |
Properties
Name | Type | Description |
collection | <<T>> | A collection of items in the tree. |
disabledKeys | Set<Key> | A set of keys for items that are disabled. |
expandedKeys | Set<Key> | A set of keys for items that are expanded. |
selectionManager |
| A selection manager to read and update multiple selection state. |
Methods
Method | Description |
toggleKey(
(key: Key
)): void | Toggles the expanded state for an item by its key. |
Provides the behavior and accessibility implementation for a listbox component. A listbox displays a list of options and allows a user to select one or more of them.
useListBox<T>(
props: <T>,
state: <T>,
ref: RefObject<HTMLElement>
):
Name | Type | Description |
children | <T> | The contents of the collection. |
isVirtualized | boolean | Whether the listbox uses virtual scrolling. |
keyboardDelegate |
| An optional keyboard delegate implementation for type to select, to override the default. |
label | ReactNode | An optional visual label for the listbox. |
autoFocus | boolean | | Whether to auto focus the listbox or an option. |
shouldFocusWrap | boolean | Whether focus should wrap around when the end/start is reached. |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
isLoading | boolean | Whether the items are currently loading. |
onLoadMore | () => any | Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. |
selectionMode |
| The type of selection that is allowed in the collection. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKeys | 'all' | Iterable<Key> | The currently selected keys in the collection (controlled). |
defaultSelectedKeys | 'all' | Iterable<Key> | The initial selected keys in the collection (uncontrolled). |
onSelectionChange | (
(keys:
)) => any | Handler that is called when the selection changes. |
id | string | The element's unique identifier. See MDN. |
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 | Type | Description |
listBoxProps | HTMLAttributes<HTMLElement> | Props for the listbox element. |
labelProps | HTMLAttributes<HTMLElement> | Props for the listbox's visual label element (if any). |
Provides the behavior and accessibility implementation for a select component. A select displays a collapsible list of options and allows a user to select one of them.
useSelect<T>(
props: <T>,
state: <T>,
ref: RefObject<HTMLElement>
):
Name | Type | Description |
children | <T> | The contents of the collection. |
keyboardDelegate |
| An optional keyboard delegate implementation for type to select, to override the default. |
isOpen | boolean | Sets the open state of the menu |
defaultOpen | boolean | Sets the default open state of the menu |
onOpenChange | (
(isOpen: boolean
)) => void | Method that is called when the open state of the menu changes |
shouldFlip | boolean | Whether the menu should automatically flip direction when space is limited |
items | Iterable<T> | Item objects in the collection. |
disabledKeys | Iterable<Key> | The item keys that are disabled. These items cannot be selected, focused, or otherwise interacted with. |
isLoading | boolean | Whether the items are currently loading. |
onLoadMore | () => any | Handler that is called when more items should be loaded, e.g. while scrolling near the bottom. |
isDisabled | boolean | Whether the input is disabled. |
isReadOnly | boolean | Whether the input can be selected but not changed by the user. |
validationState |
| Whether the input should display its "valid" or "invalid" visual styling. |
isRequired | boolean | Whether user input is required on the input before form submission.
Often paired with the necessityIndicator prop to add a visual indicator to the input. |
label | ReactNode | The content to display as the label. |
placeholder | string | Temporary text that occupies the text input when it is empty. |
disallowEmptySelection | boolean | Whether the collection allows empty selection. |
selectedKey | Key | The currently selected key in the collection (controlled). |
defaultSelectedKey | Key | The initial selected key in the collection (uncontrolled). |
onSelectionChange | (
(key: Key
)) => any | Handler that is called when the selection changes. |
id | string | The element's unique identifier. See MDN. |
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. |
Properties
Name | Type | Description |
selectedKey | Key | The key for the currently selected item. |
selectedItem | <T> | The value of the currently selected item. |
isFocused | boolean | Whether the select is currently focused. |
collection | <<T>> | A collection of items in the list. |
disabledKeys | Set<Key> | A set of items that are disabled. |
selectionManager |
| A selection manager to read and update multiple selection state. |
focusStrategy |
| Controls which item will be auto focused when the menu opens. |
isOpen | boolean | Whether the overlay is currently open. |
Methods
Method | Description |
setSelectedKey(
(key: Key
)): void | Sets the selected key. |
setFocused(
(isFocused: boolean
)): void | Sets whether the select is focused. |
toggle(
(focusStrategy:
| | null
)): void | Toggles the menu. |
open(): void | Opens the overlay. |
close(): void | Closes the overlay. |
Name | Type | Description |
labelProps | HTMLAttributes<HTMLElement> | Props for the label element. |
triggerProps |
| Props for the popup trigger element. |
valueProps | HTMLAttributes<HTMLElement> | Props for the element representing the selected value. |
menuProps | HTMLAttributes<HTMLElement> | Props for the popup. |