Directory tree (multiple selection)
This example combines multiselect and an exclusive select onClick. It supports multiselect using various commands, such as Ctrl+click
Shift+Click
, Enter/Space
and Shift + Up/Down
.
- src
- node_modules
- .npmignore
- package.json
- webpack.config.js
import React from "react";
import { DiCss3, DiJavascript, DiNpm } from "react-icons/di";
import { FaList, FaRegFolder, FaRegFolderOpen } from "react-icons/fa";
import TreeView, { flattenTree } from "react-accessible-treeview";
import "./styles.css";
const folder = {
name: "",
children: [
{
name: "src",
children: [{ name: "index.js" }, { name: "styles.css" }],
},
{
name: "node_modules",
children: [
{
name: "react-accessible-treeview",
children: [{ name: "index.js" }],
},
{ name: "react", children: [{ name: "index.js" }] },
],
},
{
name: ".npmignore",
},
{
name: "package.json",
},
{
name: "webpack.config.js",
},
],
};
const data = flattenTree(folder);
function MultiSelectDirectoryTreeView() {
return (
<div>
<div className="ide">
<TreeView
data={data}
aria-label="directory tree"
togglableSelect
clickAction="EXCLUSIVE_SELECT"
multiSelect
nodeRenderer={({
element,
isBranch,
isExpanded,
getNodeProps,
level,
handleSelect,
}) => (
<div {...getNodeProps()} style={{ paddingLeft: 20 * (level - 1) }}>
{isBranch ? (
<FolderIcon isOpen={isExpanded} />
) : (
<FileIcon filename={element.name} />
)}
{element.name}
</div>
)}
/>
</div>
</div>
);
}
const FolderIcon = ({ isOpen }) =>
isOpen ? (
<FaRegFolderOpen color="e8a87c" className="icon" />
) : (
<FaRegFolder color="e8a87c" className="icon" />
);
const FileIcon = ({ filename }) => {
const extension = filename.slice(filename.lastIndexOf(".") + 1);
switch (extension) {
case "js":
return <DiJavascript color="yellow" className="icon" />;
case "css":
return <DiCss3 color="turquoise" className="icon" />;
case "json":
return <FaList color="yellow" className="icon" />;
case "npmignore":
return <DiNpm color="red" className="icon" />;
default:
return null;
}
};
export default MultiSelectDirectoryTreeView;
.ide {
background: #242322;
font-family: monospace;
font-size: 16px;
color: white;
user-select: none;
padding: 20px;
border-radius: 0.4em;
}
.ide .tree,
.ide .tree-node,
.ide .tree-node-group {
list-style: none;
margin: 0;
padding: 0;
}
.ide .tree-branch-wrapper,
.ide .tree-node__leaf {
outline: none;
outline: none;
}
.ide .tree-node {
cursor: pointer;
}
.ide .tree-node:hover {
background: rgba(255, 255, 255, 0.1);
}
.ide .tree .tree-node--focused {
background: rgba(255, 255, 255, 0.2);
}
.ide .tree .tree-node--selected {
background: rgba(48, 107, 176);
}
.ide .tree-node__branch {
display: block;
}
.ide .icon {
vertical-align: middle;
margin-right: 5px;
}