Skip to main content

API reference

import TreeSelect, {
TreeNode, NodeLabel, NodeCheck, ExpandIcon,
getSelectedIDs, getSelectedNodes
} from '@attebyte/react-tree-select';
import '@attebyte/react-tree-select/style.css';

T is the type of your nodes. Every exported component and helper is generic over it.

TreeSelect​

The tree component (default export). It renders a div with role="tree", and any other div props (className, style, aria-label, onKeyDown, ...) are passed to it.

Data​

PropTypeDefaultDescription
data`T[]T`
defaultData`T[]T`
onChange(data: T[]) => void or (data: T) => voidCalled with the updated tree, in the same shape as data or defaultData, whenever a node is checked or unchecked (only when something changed). Unchanged nodes are shared with the previous tree.
uniqueIDPropertykeyof TrequiredProperty that uniquely identifies each node. IDs must be unique across the whole tree.
labelPropertykeyof TrequiredProperty that holds the node's label. Also used as the node's accessible name.
childrenPropertykeyof TrequiredProperty that holds the node's children (an array).
valuePropertykeyof TProperty that holds the checked state: true, false/missing, or null for mixed. Without it, the tree is display-only.
disabledPropertykeyof TProperty that marks a node as disabled when true.

Behavior​

PropTypeDefaultDescription
chainedSelectbooleantrueChecking a node checks its enabled descendants, parents derive their state from their children, and descendants of disabled nodes are disabled. See Selection.
defaultExpandedbooleanfalseWhether nodes start expanded. Changing it expands or collapses every node.
disabledbooleanfalseDisable every node.
expandDisabledNodesbooleantrueWhether disabled nodes can be expanded and collapsed.

Styling​

PropTypeDescription
nodeClassName / nodeStylestring / CSSPropertiesApplied to each node's container.
labelClassName / labelStylestring / CSSPropertiesApplied to each label.
inputClassName / inputStylestring / CSSPropertiesApplied to each checkbox.
iconClassName / iconStylestring / CSSPropertiesApplied to each expand icon.

Your class names are added alongside the defaults and override them, because the defaults are in a lower-priority cascade layer. See Customization.

Custom components​

PropTypeDescription
CustomNodeComponentComponentType<TreeNodeProps<T>>Replaces TreeNode.
CustomLabelComponentComponentType<NodeLabelProps<T>>Replaces NodeLabel.
CustomCheckComponentComponentType<NodeCheckProps<T>>Replaces NodeCheck.
CustomExpandIconComponentComponentType<ExpandIconProps<T>>Replaces ExpandIcon.

Standard component props​

Every part receives standardComponentProps:

PropertyTypeDescription
nodeTThe node being rendered.
expandedbooleanWhether the node is expanded.
uniqueIDPropertykeyof TAs passed to TreeSelect.
labelPropertykeyof TAs passed to TreeSelect.
valuePropertykeyof T | undefinedAs passed to TreeSelect.
childrenPropertykeyof TAs passed to TreeSelect.
disabledPropertykeyof T | undefinedAs passed to TreeSelect.

TreeNode​

The container for a node. Renders a div; other div props are passed to it.

PropTypeDescription
standardComponentPropsStandardComponentProps<T>See above.
treeItemPropsTreeItemAttributesrole, tabIndex, aria-* attributes and a focus ref. Spread onto the node's root element.
childrenReactNodeThe rendered nested nodes, provided only while the node is expanded.
expandedbooleanWhether the node is expanded.
onExpandedChange(expanded: boolean) => voidExpand or collapse the node.
onChange(node: T, value: boolean | null, options?: NodeChangeOptions) => voidSet a node's checked state, applying chained selection. Pass { toggle: true } for click-like toggling; see Changing selection from code.
levelnumberDepth of the node; root nodes are 0.
disabledbooleanDisabled by the tree, or (with chained selection) by a disabled ancestor. Check disabledProperty for the node's own state.
defaultExpanded, expandDisabledNodes, chainedSelectbooleanAs passed to TreeSelect.
labelClassName, labelStyle, inputClassName, inputStyle, iconClassName, iconStyleAs passed to TreeSelect, for the node's parts.
CheckComponent, LabelComponent, ExpandIconComponentComponentTypeThe part components to render (the defaults or your custom components).

NodeLabel​

Renders a span; other span props are passed to it.

PropTypeDescription
standardComponentPropsStandardComponentProps<T>See above.
labelstringThe label text.
hasChildrenbooleanWhether the node has children.
expandedbooleanWhether the node is expanded.
disabledbooleanWhether the node is disabled.
expandDisabledNodesbooleanWhether disabled nodes can be expanded.
iconClassName / iconStylePassed to the expand icon.
ExpandIconComponentComponentType<ExpandIconProps<T>>The expand icon to render.

NodeCheck​

Renders an <input type="checkbox">; other input props are passed to it.

PropTypeDescription
standardComponentPropsStandardComponentProps<T>See above.
checkedboolean | nulltrue when checked, null when mixed. Sets the input's indeterminate state.
expandedbooleanWhether the node is expanded.
disabledbooleanWhether the node is disabled.
myRefRef<HTMLInputElement>Ref to the input element.

ExpandIcon​

Renders a span; other span props are passed to it.

PropTypeDescription
standardComponentPropsStandardComponentProps<T>See above.
expandedbooleanWhether the node is expanded.
showbooleanWhether to render the icon (true for nodes with children).

getSelectedIDs / getSelectedNodes​

getSelectedIDs<T>(data: T[], options: SelectedIDsOptions<T>): T[keyof T][]
getSelectedNodes<T>(data: T[], options: SelectedNodesOptions<T>): T[]
OptionTypeDescription
uniqueIDPropertykeyof TProperty holding the ID (getSelectedIDs only).
childrenPropertykeyof TProperty holding the children.
valuePropertykeyof TProperty holding the checked state.
mode'all' | 'leaves' | 'topmost'Which checked nodes to return. Defaults to 'all'. See Reading the selection.

Exported types​

TreeSelectProps, SingleRootTreeSelectProps, TreeNodeProps, NodeLabelProps, NodeCheckProps, ExpandIconProps, StandardComponentProps, TreeItemAttributes, NodeChangeOptions, SelectionMode, SelectedIDsOptions, SelectedNodesOptions.