Skip to content

Commit

Permalink
feat(ui-debug): allow to expand/collapse the tree without selecting
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Sep 17, 2024
1 parent 552f535 commit 7cfa296
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Box } from "@mui/material";
import { TreeItem } from "@mui/x-tree-view/TreeItem";
import { TreeItem, type TreeItemProps } from "@mui/x-tree-view/TreeItem";
import { TreeData, getFileType, getFileIcon, isFolder } from "../utils";
import DebugContext from "../DebugContext";
import { useContext } from "react";
Expand All @@ -20,7 +20,15 @@ function FileTreeItem({ name, treeData, path }: Props) {
// Event handlers
////////////////////////////////////////////////////////////////

const handleClick = () => {
const handleClick: TreeItemProps["onClick"] = ({ target }) => {
// The item is not selected if the click is on the expand/collapse icon
if (
target instanceof Element &&
target.closest(".MuiTreeItem-iconContainer")
) {
return;
}

setSelectedFile({ fileType, filename: name, filePath, treeData });
};

Expand All @@ -38,6 +46,12 @@ function FileTreeItem({ name, treeData, path }: Props) {
</Box>
}
onClick={handleClick}
sx={{
// Expand/collapse icon
".MuiTreeItem-iconContainer:hover": {
backgroundColor: "rgb(0 0 0 / 50%)",
},
}}
>
{isFolder(treeData) &&
Object.keys(treeData).map((childName) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { SimpleTreeView } from "@mui/x-tree-view/SimpleTreeView";
import ArrowRightIcon from "@mui/icons-material/ArrowRight";
import ArrowDropDownIcon from "@mui/icons-material/ArrowDropDown";
import FileTreeItem from "./FileTreeItem";
import { getParentPaths, type TreeFolder } from "../utils";

interface Props {
data: TreeFolder;
// `currentPath` must not be undefined to make `SimpleTreeView` controlled
// `currentPath` must not be `undefined` to make `SimpleTreeView` controlled
currentPath: string | null;
expandedItems: string[];
setExpandedItems: React.Dispatch<React.SetStateAction<string[]>>;
Expand Down Expand Up @@ -42,6 +44,10 @@ function Tree(props: Props) {
: expandedItems
}
onExpandedItemsChange={handleExpandedItemsChange}
slots={{
expandIcon: ArrowRightIcon,
collapseIcon: ArrowDropDownIcon,
}}
>
{Object.keys(data).map((filename) => (
<FileTreeItem
Expand Down

0 comments on commit 7cfa296

Please sign in to comment.