@sinm/react-file-tree
v1.1.1
Published
react file tree
Downloads
231
Readme
react-file-tree
Install
yarn add @sinm/react-file-tree
Usage
- Render tree
import { FileTree } from '@sinm/react-file-tree';
// default style
import '@sinm/react-file-tree/styles.css';
const [tree, setTree] = useState(defaultTree);
<FileTree tree={tree}>
- Toggle expanded
import { utils } from "@sinm/react-file-tree";
const toggleExpanded: FileTreeProps["onItemClick"] = (treeNode) => {
setTree((tree) =>
utils.assignTreeNode(tree, treeNode.uri, { expanded: !treeNode.expanded })
);
};
<FileTree tree={tree} onItemClick={toggleExpanded} />;
import FileItemWithFileIcon from '@sinm/react-file-tree/lib/FileItemWithFileIcon';
// import { getFileIconClass } from '@sinm/react-file-tree/lib/FileItemWithFileIcon';
import '@sinm/react-file-tree/icons.css';
const itemRenderer = (treeNode: TreeNode) => <FileItemWithFileIcon treeNode={treeNode} />
<FileTree tree={tree} itemRenderer={itemRenderer} />
- Sort tree items
import orderBy from "lodash/orderBy";
// directory first and filename dict sort
const sorter = (treeNodes: TreeNode[]) =>
orderBy(
treeNodes,
[
(node) => (node.type === "directory" ? 0 : 1),
(node) => utils.getFileName(node.uri),
],
["asc", "asc"]
);
<FileTree tree={tree} sorter={sorter} />
- Load tree from server
// backend
import { getTreeNode } from "@sinm/react-file-tree/lib/node";
app.get("/root", async (req, res, next) => {
try {
const tree = await getTreeNode("."); // build tree for current directory
res.send(tree);
} catch (err) {
next(err);
}
});
// frontend
useEffect(() => {
fetch("/root")
.then((res) => res.json())
// expand root node
.then((tree) => Object.assign(tree, { expanded: true }))
.then(setTree);
}, []);
- activated style
.file-tree__tree-item.activated {
background: rgba(0, 0, 0, 0.1);
}
<FileTree tree={tree} activatedUri={uri}>
Demo
git clone https://github.com/pansinm/react-file-tree.git
cd react-file-tree
yarn
git submodule update --init --recursive
yarn start