shadcn.io is an independent project built on shadcn/ui
On this page5 sections
Installation
Usage
import { JsonTreeView } from "@/components/ui/json-tree-view";const data = {
name: "John Doe",
age: 30,
email: "john.doe@example.com",
address: {
street: "123 Main St",
city: "Anytown",
state: "CA",
zip: "12345",
},
};
const Example = () => (
<div className="w-full max-w-md">
<JsonTreeView data={data} defaultExpandedDepth={1} />
</div>
);Demos
Data Types
Expand Depth
Map Set
FAQ
How do I choose how much of the tree is open?
Set defaultExpandedDepth. With 1, the top level's children are open and deeper levels stay closed. The Expand Depth demo shows the values side by side.
Which values can it show?
Strings, numbers, booleans, null, arrays and objects, and also Maps and Sets, as the Map Set demo shows. Strings, numbers, booleans and null each get their own color from your theme's tokens.
How do I render a value my own way?
Pass renderValue. It is handed to the tree and receives each value's node, so you can return your own output, such as a link or a swatch, in place of the raw text.
Is it fast with a large JSON document?
It mounts lazily: branches are rendered when opened, and unmounted again when closed (lazyMount and unmountOnExit default to true). A closed branch of thousands of rows costs nothing until the user opens it.
API Reference
JsonTreeView
dataunknown
The data to show.
canRename?(node: T, indexPath: IndexPath) → boolean
- Type
(node: T, indexPath: IndexPath) → boolean- Parameters
node– TindexPath– IndexPath- Returns
boolean
checkedValue?string[]collapseStringsAfterLength?number
How many characters of a string show in a preview.
- Default
30
defaultCheckedValue?string[]defaultExpandedDepth?number
How many levels open at first; the root value is level 1.
defaultExpandedValue?string[]defaultSelectedValue?string[]expandedValue?string[]expandOnClick?boolean
Whether pressing a branch's row expands or collapses it. Its indicator always does.
- Default
true
fileIcons?Record<string, React.JSX.ElementType | null>
Custom extension icons.
- Type
Record<string, React.JSX.ElementType | null>
groupArraysAfterLength?number
Arrays longer than this split into groups of this many items.
- Default
100
lazyMount?boolean
Mount a branch's children only once it's first expanded. A branch's
children always mount when it expands; unmountOnExit keeps them after.
- Default
true
loadChildren?(details: LoadChildrenDetails<T>) → Promise<T[]>
Loads a branch's children the first time it expands while it has none.
- Type
(details: LoadChildrenDetails<T>) → Promise<T[]>- Parameters
details– LoadChildrenDetails<T>- Returns
Promise<T[]>
maxPreviewItems?number
How many entries an object, array, map or set lists in its preview.
- Default
3
onBeforeRename?(details: RenameCompleteDetails) → boolean
- Type
(details: RenameCompleteDetails) → boolean- Parameters
details– { indexPath: IndexPath; label: string; value: string }- Returns
boolean
onCheckedChange?(details: CheckedChangeDetails) → void
Called when the checked state changes.
- Type
(details: CheckedChangeDetails) → void- Parameters
details– { checkedValue: string[] }- Returns
void
onExpandedChange?(details: ExpandedChangeDetails<T>) → void
- Type
(details: ExpandedChangeDetails<T>) → void- Parameters
details– ExpandedChangeDetails<T>- Returns
void
onFocusChange?(details: FocusChangeDetails<T>) → void
- Type
(details: FocusChangeDetails<T>) → void- Parameters
details– FocusChangeDetails<T>- Returns
void
onLoadChildrenComplete?(details: { collection: TreeCollection<T> }) → void
The collection with the loaded children in place, to keep as the new one.
- Type
(details: { collection: TreeCollection<T> }) → void- Parameters
details– { collection: TreeCollection<T> }- Returns
void
onLoadChildrenError?(details: { nodes: NodeWithError<T>[] }) → void
- Type
(details: { nodes: NodeWithError<T>[] }) → void- Parameters
details– { nodes: NodeWithError<T>[] }- Returns
void
onRenameComplete?(details: RenameCompleteDetails) → void
- Type
(details: RenameCompleteDetails) → void- Parameters
details– { indexPath: IndexPath; label: string; value: string }- Returns
void
onRenameStart?(details: RenameStartDetails<T>) → void
- Type
(details: RenameStartDetails<T>) → void- Parameters
details– RenameStartDetails<T>- Returns
void
onSelectionChange?(details: SelectionChangeDetails<T>) → void
- Type
(details: SelectionChangeDetails<T>) → void- Parameters
details– SelectionChangeDetails<T>- Returns
void
quotesOnKeys?boolean
Whether keys show in quotes.
renderValue?RenderValueselectedValue?string[]selectionMode?"single" | "multiple"
How many nodes can be selected.
- Type
"single" | "multiple"- Default
"single"
showNonenumerable?boolean
Show properties that aren't enumerable, like a map's entries.
- Default
true
typeahead?boolean
Whether typing moves to the next node whose name starts with the text.
- Default
true
unmountOnExit?boolean
Unmount a branch's children when it collapses.
- Default
true
render?ReactElement | ((props: object, state: object) => ReactElement)
Renders your own element in place of the root div.
- Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string
Merged with the part's classes.