Command palette

@yasinsensoy/native

React Native · Components

TreeView

Tree view: file explorer, category tree, permission list.

10 exports

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

app
(site)
page.tsx
layout.tsxM
globals.css
components
ui
button.tsxButton
dialog.tsx2
city-picker.tsx
tree-view.tsx
header.tsx
footer.tsxLocked
package.json

Code

import { TreeNode, TreeView } from "@yasinsensoy/native";

const [items, setItems] = useState<TreeNode[]>(files);

<TreeView
  label="Project files"
  items={items}
  className="h-96" selectionMode="multiple" search draggable stickyParents
  onItemsChange={setItems}
  onRename={(node, name) => setItems((i) => updateNode(i, node.id, { label: name }))}
  onDelete={(nodes) => setItems((i) => removeNodes(i, nodes.map((n) => n.id)))}
  onAction={(node) => open(node)}
  contextMenu={(node, api) => [
    { label: "Rename", shortcut: "F2", onSelect: () => api.rename(node.id) },
    { label: "Delete", tone: "danger", shortcut: "Delete", onSelect: () => remove(node) },
  ]}
/>

Events

  1. Events appear here when you interact with the preview.

Checkboxes: a three-state permission tree, submitted in a form

Sales
View orders
Create orders
Issue refundsNeeds approval
Catalog
Products
View
Edit
DeleteAdmins only
Pricing
Reports

2 permissions selected (leaves only)

Lazy loading: folders from the server, error and retry

100k nodes: virtualization, sticky parent nodes, search

Region 1
Branch 1.1
Record 1.1.1
Record 1.1.2
Record 1.1.3
Record 1.1.4
Record 1.1.5
Record 1.1.6
Record 1.1.7
Record 1.1.8
Record 1.1.9
Record 1.1.10
Record 1.1.11
Record 1.1.12
Record 1.1.13
Record 1.1.14
Record 1.1.15
Record 1.1.16
Record 1.1.17
Record 1.1.18
Record 1.1.19
Record 1.1.20
Record 1.1.21
Record 1.1.22
Record 1.1.23
Record 1.1.24
Record 1.1.25
Record 1.1.26
Record 1.1.27
Record 1.1.28
Record 1.1.29

101,100 nodes, all expanded.

Import

import { TreeView } from "@yasinsensoy/native";

TreeView

component

Tree 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.
TreeView({ items: rootItems, label, size, indent: indentProp, guides, expanded: expandedProp, defaultExpanded, onExpandedChange, expandOnClick, selectionMode, selected: selectedProp, defaultSelected, onSelectedChange, onAction, checkboxes, checked: checkedProp, defaultChecked, onCheckedChange, checkMode, checkOutput, loadChildren, query: queryProp, defaultQuery, onQueryChange, search, filterMode, filter, contextMenu, renderLabel, empty, virtualize, height, apiRef, style, }: TreeViewProps<T>): JSX.Element

Examples

<TreeView label="Files" items={tree} defaultExpanded={["src"]} search
  onAction={(node) => open(node.id)} />
<TreeView label="Permissions" items={permissions} checkboxes selectionMode="none"
  checked={allowed} onCheckedChange={setAllowed} checkOutput="leaves" />

TreeAction

interface

Small action button at the end of a TreeNode row.

NameTypeDefaultDescription
iconIconPropButton icon.
labelstringButton name (screen reader).
onSelect(node: TreeNode<T>) => voidCalled with the node on tap.
tone?"neutral" | "danger""neutral"Color.

TreeNode

interface

A node inside TreeView (same fields as web; no drag and drop or renaming).

NameTypeDefaultDescription
idstringUnique id within the tree.
labelReactNodeDisplay name.
textValue?stringPlain text for search when the name is not text.
icon?IconPropIcon before the name.
expandedIcon?IconPropIcon while the node is expanded (e.g. an open folder).
description?ReactNodeMuted description under the name.
badge?ReactNodeSmall badge at the end of the row (e.g. a count).
badgeTone?Tone"neutral"Badge color.
children?readonly TreeNode<T>[]Child nodes.
hasChildren?booleanChildren will come later via loadChildren.
disabled?booleanDisabled node: cannot be selected or checked.
selectable?booleantrueSelectable?
checkable?booleantrueShow 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?TYour own data.

Selection mode: none, single or multiple.

type TreeSelectionMode = "none" | "single" | "multiple"

Row height and font size.

type TreeViewSize = "sm" | "md" | "lg"

TreeRowState

interface

Row state passed to renderLabel.

NameTypeDescription
levelnumberDepth (root is 1).
expandedbooleanExpanded?
selectedbooleanSelected?
checkedCheckStateCheckbox state.
matchedbooleanA search match?

TreeViewApi

interface

TreeView apiRef methods (same as web; no drag and drop or renaming).

NameTypeDescription
expand(ids: string | readonly string[]) => voidExpands nodes.
collapse(ids: string | readonly string[]) => voidCollapses nodes.
toggle(id: string) => voidExpands or collapses a node.
expandAll() => voidExpands all known nodes.
collapseAll() => voidCollapses all nodes.
reveal(id: string) => voidExpands the node's ancestors and scrolls to its row.
select(ids: readonly string[]) => voidChanges the selection.
reload(id: string) => voidReloads the node's children (loadChildren).
getNode(id: string) => TreeNode<T> | undefinedNode 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

interface

TreeView props (same names as web).

NameTypeDefaultDescription
itemsreadonly TreeNode<T>[]Nodes.
labelstringScreen reader name of the tree (e.g. "Files").
size?TreeViewSize"md"Row height.
indent?number16Indent per level (px).
guides?booleantrueVertical 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[]) => voidWhen the expanded nodes change.
expandOnClick?booleantrueShould 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>[]) => voidWhen the selection changes.
onAction?(node: TreeNode<T>) => voidWhen a leaf node is tapped (e.g. to open a file; double click on web).
checkboxes?booleanfalseCheckboxes.
checked?readonly string[]Checked nodes (controlled).
defaultChecked?readonly string[]Initially checked nodes.
onCheckedChange?(ids: string[]) => voidWhen 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?stringSearch text (controlled).
defaultQuery?stringInitial search text.
onQueryChange?(query: string) => voidWhen the search text changes.
search?boolean | stringfalseSearch 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) => booleanYour 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) => ReactNodeYour own content instead of the name.
empty?ReactNodeShown when there are no rows.
virtualize?boolean | "auto""auto"Renders only visible rows (FlatList); "auto": with more than 100 rows.
height?numberHeight of the virtual list; fills the container if omitted.
apiRef?Ref<TreeViewApi<T>>Tree API.
style?StyleProp<ViewStyle>Container style.

State of the checkbox.

type CheckState = "checked" | "unchecked" | "mixed"

Which 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").
type CheckOutput = "all" | "leaves" | "top"