React Native · Components
TreeView
Tree view: file explorer, category tree, permission list.
Live example
The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.
Preview
Code
Events
- Events appear here when you interact with the preview.
Checkboxes: a three-state permission tree, submitted in a form
Lazy loading: folders from the server, error and retry
100k nodes: virtualization, sticky parent nodes, search
101,100 nodes, all expanded.
Import
TreeView
componentTree view: file explorer, category tree, permission list. Calculations are the same as web (@yasinsensoy/core/tree): flattening, diacritic-insensitive search, tri-state checkboxes, lazy loading.
- Touch: Tapping a parent toggles and selects it; tapping a leaf selects it and calls
onAction. A long press opens the menu. - Long trees: With more than 100 rows only the visible ones are rendered.
Examples
TreeAction
interfaceSmall action button at the end of a TreeNode row.
| Name | Type | Default | Description |
|---|---|---|---|
icon | IconProp | Button icon. | |
label | string | Button name (screen reader). | |
onSelect | (node: TreeNode<T>) => void | Called with the node on tap. | |
tone? | "neutral" | "danger" | "neutral" | Color. |
TreeNode
interfaceA node inside TreeView (same fields as web; no drag and drop or renaming).
| Name | Type | Default | Description |
|---|---|---|---|
id | string | Unique id within the tree. | |
label | ReactNode | Display name. | |
textValue? | string | Plain text for search when the name is not text. | |
icon? | IconProp | Icon before the name. | |
expandedIcon? | IconProp | Icon while the node is expanded (e.g. an open folder). | |
description? | ReactNode | Muted description under the name. | |
badge? | ReactNode | Small badge at the end of the row (e.g. a count). | |
badgeTone? | Tone | "neutral" | Badge color. |
children? | readonly TreeNode<T>[] | Child nodes. | |
hasChildren? | boolean | Children will come later via loadChildren. | |
disabled? | boolean | Disabled node: cannot be selected or checked. | |
selectable? | boolean | true | Selectable? |
checkable? | boolean | true | Show a checkbox? |
actions? | readonly TreeAction<T>[] | Action buttons at the end of the row. | |
menu? | readonly MenuEntry[] | Menu opened on long press; takes precedence over the tree's contextMenu. | |
data? | T | Your own data. |
Selection mode: none, single or multiple.
TreeViewSize
typeRow height and font size.
TreeRowState
interfaceRow state passed to renderLabel.
| Name | Type | Description |
|---|---|---|
level | number | Depth (root is 1). |
expanded | boolean | Expanded? |
selected | boolean | Selected? |
checked | CheckState | Checkbox state. |
matched | boolean | A search match? |
TreeViewApi
interfaceTreeView apiRef methods (same as web; no drag and drop or renaming).
| Name | Type | Description |
|---|---|---|
expand | (ids: string | readonly string[]) => void | Expands nodes. |
collapse | (ids: string | readonly string[]) => void | Collapses nodes. |
toggle | (id: string) => void | Expands or collapses a node. |
expandAll | () => void | Expands all known nodes. |
collapseAll | () => void | Collapses all nodes. |
reveal | (id: string) => void | Expands the node's ancestors and scrolls to its row. |
select | (ids: readonly string[]) => void | Changes the selection. |
reload | (id: string) => void | Reloads the node's children (loadChildren). |
getNode | (id: string) => TreeNode<T> | undefined | Node from an id. |
getPath | (id: string) => string[] | Id path from the root to the node (including itself). |
getItems | () => TreeNode<T>[] | The whole tree including loaded children. |
TreeViewProps
interfaceTreeView props (same names as web).
| Name | Type | Default | Description |
|---|---|---|---|
items | readonly TreeNode<T>[] | Nodes. | |
label | string | Screen reader name of the tree (e.g. "Files"). | |
size? | TreeViewSize | "md" | Row height. |
indent? | number | 16 | Indent per level (px). |
guides? | boolean | true | Vertical lines showing the levels. |
expanded? | readonly string[] | Expanded nodes (controlled). | |
defaultExpanded? | readonly string[] | "all" | Initially expanded nodes; "all" expands all known nodes. | |
onExpandedChange? | (ids: string[]) => void | When the expanded nodes change. | |
expandOnClick? | boolean | true | Should tapping a parent row toggle it? With false only the chevron does. |
selectionMode? | TreeSelectionMode | "single" | Selection mode. |
selected? | readonly string[] | Selected nodes (controlled). | |
defaultSelected? | readonly string[] | Initially selected nodes; their ancestors expand automatically. | |
onSelectedChange? | (ids: string[], nodes: TreeNode<T>[]) => void | When the selection changes. | |
onAction? | (node: TreeNode<T>) => void | When a leaf node is tapped (e.g. to open a file; double click on web). | |
checkboxes? | boolean | false | Checkboxes. |
checked? | readonly string[] | Checked nodes (controlled). | |
defaultChecked? | readonly string[] | Initially checked nodes. | |
onCheckedChange? | (ids: string[]) => void | When the checks change. | |
checkMode? | "cascade" | "independent" | "cascade" | "cascade": a parent is linked to its children; "independent": independent. |
checkOutput? | CheckOutput | "all" | Which nodes the checked value contains. |
loadChildren? | (node: TreeNode<T>, context: { signal: AbortSignal; }) => Promise<readonly TreeNode<T>[] | undefined | void> | A hasChildren node fetches its children when expanded; on error "Try again" appears. | |
query? | string | Search text (controlled). | |
defaultQuery? | string | Initial search text. | |
onQueryChange? | (query: string) => void | When the search text changes. | |
search? | boolean | string | false | Search box above the tree: true or a placeholder text. |
filterMode? | "filter" | "highlight" | "filter" | Whether non-matching branches are hidden or only highlighted. |
filter? | (node: TreeNode<T>, query: string) => boolean | Your own match rule; defaults to a name search ignoring Turkish letters and diacritics. | |
contextMenu? | readonly MenuEntry[] | ((node: TreeNode<T>, api: TreeViewApi<T>) => readonly MenuEntry[]) | Menu opened on long press (the node's menu takes precedence). | |
renderLabel? | (node: TreeNode<T>, state: TreeRowState) => ReactNode | Your own content instead of the name. | |
empty? | ReactNode | Shown when there are no rows. | |
virtualize? | boolean | "auto" | "auto" | Renders only visible rows (FlatList); "auto": with more than 100 rows. |
height? | number | Height of the virtual list; fills the container if omitted. | |
apiRef? | Ref<TreeViewApi<T>> | Tree API. | |
style? | StyleProp<ViewStyle> | Container style. |
CheckState
typeState of the checkbox.
CheckOutput
typeWhich nodes the TreeView checked value contains.
"all": all checked nodes (including parents)."leaves": only checked leaves (e.g. selected files)."top": the topmost fully checked nodes; their descendants aren't listed (the shortest list for saying "all of this folder").