Search & navigation
Find a node, bring it into view from code, select one or many, open files, and remember which folders were open.
Search
showSearch adds a search field under the toolbox. searchTerm sets the term from outside instead, with onSearchChange reporting what was typed into the built-in field.
<Direactree structure={structure} showSearch />
// or your own field
<input value={term} onChange={(event) => setTerm(event.target.value)} />
<Direactree structure={structure} searchTerm={term} searchMode="highlight" />
Every folder starts closed. Folders that hold a match open on their own and close again when the search ends. Try tsx, use, or .css.
{
"showSearch": true,
"searchTerm": "(built-in field: \"\")",
"searchMode": "filter",
"filterNode": "default name match"
}searchMode="filter"(default) shows only matches and the folders that hold them."highlight"keeps every row and marks matches.- Folders that hold a match open on their own, without changing the open state you go back to when the search ends. A folder can still be closed by hand during a search.
- The matching part of the name is wrapped in
<mark class="direactree-match">. filterNode(node, term)replaces the default case-insensitive name match. Wrap it inuseCallbackso large trees are not searched again on every render.- In the built-in field, ↓ or Enter moves to the first match, and Escape clears the term.
- Only nodes already in
structureare searched. Children that are not loaded yet are not.
Reveal from code
Pass a ref to get the handle. revealNode opens every ancestor, scrolls the row into view, and selects it:
const ref = useRef<DireactreeHandle>(null);
<Direactree ref={ref} structure={structure} />
ref.current?.revealNode("src/components/Button.tsx", { focus: true, block: "center" });
Each button calls ref.current.revealNode(id). The folders above it open, the row scrolls into view inside the small panel, and it gets selected.
{
"selected": [],
"expanded": []
}| Method | Use |
|---|---|
revealNode(id, { select?, focus?, block? }) | Open the way to a node, scroll to it, select it (default), and optionally focus it. block is "nearest" (default), "start", "center", or "end". Returns false when the id is not in the tree. |
expand(id), collapse(id) | Open or close one folder. |
expandAll(), collapseAll() | Open or close every folder. |
select(id | null) | Select a node, opening the way to it, or clear the selection. |
focus() | Move keyboard focus to the selected row, or the first row. |
getSelectedIds(), getExpandedIds() | Read the current selection and open folders. |
Selection
Single selection
By default one node is selected at a time. onSelectedNodeChange receives its NodePath, or null. Pass selectedId to control it; when it points into a closed folder, the folders open.
const [selectedId, setSelectedId] = useState<string | null>(null);
<Direactree
structure={structure}
selectedId={selectedId}
onSelectedNodeChange={(node) => setSelectedId(node?.id ?? null)}
/>
Multiple selection
selectionMode="multiple" adds the usual desktop gestures. onSelectionChange receives every selected id and their paths, and selectedIds makes it controlled:
| Gesture | Result |
|---|---|
| Click | Select only this row. |
| Ctrl/Cmd + click, or Ctrl/Cmd + Space | Add or remove this row. |
| Shift + click | Select the range from the last clicked row. |
| Shift + ↑/↓/Home/End | Extend the range. |
| Ctrl/Cmd + A | Select every visible row. |
| Click outside the tree | Clear the selection. |
The built-in Rename and Delete act on the focused row. Bulk actions get the ids through toolboxActions, the ref handle, or onSelectionChange.
Opening a file
onNodeActivate(node, path) runs on a double click and on Enter. On a folder it also opens or closes the folder.
<Direactree
structure={structure}
onNodeActivate={(node) => node.type === "file" && openTab(node.id)}
/>
Ctrl/Cmd+click, Shift+click, Shift+arrows, and Ctrl/Cmd+A select several rows. Double click or press Enter to open a file. Type letters to jump.
[]
Nothing yet. Use the tree.
Open folders
Direactree remembers which folders are open. TreeNode.isExpanded or defaultExpandedIds sets the first state. expandedIds with onExpandedChange makes it controlled, for example to keep it in the URL or in storage. isAllExpanded keeps every folder open while it is true.
const [expanded, setExpanded] = useState<string[]>(() => readSaved() ?? []);
<Direactree
structure={structure}
expandedIds={expanded}
onExpandedChange={(ids) => {
setExpanded(ids);
save(ids);
}}
/>
Open and close folders, then reload the page. With remember on, expandedIds comes back from localStorage.
[ "app" ]
Keyboard
The tree is one tab stop. Focus lands on the selected row, or the first row.
| Key | Action |
|---|---|
| ↓ / ↑ | Next or previous visible row. |
| → | Open a closed folder, or move to its first child. |
| ← | Close an open folder, or move to the parent. |
| Home / End | First or last visible row. |
| Enter | Select, open or close a folder, and call onNodeActivate. |
| Space | Select the row. |
| Letters | Jump to the next row whose name starts with what you typed. Type quickly to match more letters, or press one letter again to cycle. |
| F2 | Rename, when allowed. |
| Delete | Delete, when allowed. |
| Shift + F10 or the menu key | Open the context menu, when contextMenu is set. |
In the name editor, Enter saves and Escape cancels. In the delete confirmation and the context menu, Escape closes it. In a virtualized tree, rows outside the window scroll in as the keyboard reaches them.