Tree View

View as Markdown

Nested rows you open and close, such as files and folders. Children can load when opened.

  • src
  • billing
  • invoice.ts7 KB
  • proration.ts3 KB
  • tax-rates-by-jurisdiction.generated.ts1.2 MB
  • main.ts1 KB
  • package.json2 KB
  • README.md6 KB

src/billing/invoice.ts

When to use

Nested rows you open and close, such as files and folders. Children can load when opened. Use Page Tree instead when the tree holds pages to drag, rename and add to.

Usage

import { TreeView } from "./ui/uicontext/components/TreeView.ts"
const Message = UiSchema.TaggedEnum({	ChangedExpanded: { expanded: Schema.Array(Schema.String) },	Selected: { id: Schema.String },})const nodes = [	{ id: "docs", parent: null, label: "docs" },	{ id: "docs/setup.md", parent: "docs", label: "setup.md" },	{ id: "README.md", parent: null, label: "README.md" },]export const Files = (h: Html.H<typeof Message.Type>, expanded: ReadonlyArray<string>, selected: string | null) =>	TreeView(h, {		props: { label: "Files", nodes, expanded, selected },		onEmit: {			RequestedExpanded: ({ expanded }) => Message.ChangedExpanded({ expanded }),			RequestedSelect: ({ id }) => Message.Selected({ id }),		},	})

Examples

Children that load when opened

hasChildren draws a folder before its children arrive; opening it shows Loading… until they do.

  • Gift cards6

API reference

Props

TreeView props
NameTypeDescription
labelrequiredstringWhat the tree holds, for screen readers: "Files", "Categories".
nodesrequiredTreeNode[]Every row, flat, in order among its siblings.
expandedrequiredstring[]The open folders. The caller keeps them and stores what RequestedExpanded asks for.
selectedrequiredstring | nullThe selected row, or null. The caller keeps it.
plainbooleanDraw no folder and page marks: a tree of plain words.

Events

TreeView events
EventPayloadDescription
RequestedExpanded{ expanded: string[] }The open folders asked for: store them and pass them back as expanded.
RequestedSelect{ id: string }A row was picked with a click, Enter or Space.

Keyboard and accessibility

KeysDoes
Down arrow Up arrowMove between rows
Right arrowOpen a folder, or move into it
Left arrowClose it, or move to its folder
Home EndGo to the first or the last row
Enter SpaceSelect the row
*Open every folder beside this one
a letterJump to the next row that starts with what’s typed

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/tree-view

Source