Skip to content

Commit

Permalink
feat(ui-commons): create TreeItemEnhanced component
Browse files Browse the repository at this point in the history
  • Loading branch information
skamril committed Sep 20, 2024
1 parent 5c77795 commit 580f974
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Box } from "@mui/material";
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";
import TreeItemEnhanced from "../../../../../common/TreeItemEnhanced";

interface Props {
name: string;
Expand All @@ -15,22 +15,12 @@ function FileTreeItem({ name, treeData, path }: Props) {
const filePath = path ? `${path}/${name}` : name;
const fileType = getFileType(treeData);
const FileIcon = getFileIcon(fileType);
const canExpand = isFolder(treeData) && Object.keys(treeData).length > 0;

////////////////////////////////////////////////////////////////
// Event handlers
////////////////////////////////////////////////////////////////

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

const handleClick = () => {
setSelectedFile({ fileType, filename: name, filePath, treeData });
};

Expand All @@ -39,7 +29,7 @@ function FileTreeItem({ name, treeData, path }: Props) {
////////////////////////////////////////////////////////////////

return (
<TreeItem
<TreeItemEnhanced
itemId={filePath}
label={
<Box sx={{ display: "flex" }}>
Expand All @@ -48,24 +38,6 @@ function FileTreeItem({ name, treeData, path }: Props) {
</Box>
}
onClick={handleClick}
sx={{
".MuiTreeItem-content": {
p: 0,
alignItems: "normal",
// Expand/collapse icon
".MuiTreeItem-iconContainer": {
alignItems: "center",
borderTopLeftRadius: "inherit",
borderBottomLeftRadius: "inherit",
"&:hover": {
background: canExpand ? "inherit" : "none",
},
},
".MuiTreeItem-label": {
py: 0.5,
},
},
}}
>
{isFolder(treeData) &&
Object.keys(treeData).map((childName) => (
Expand All @@ -76,7 +48,7 @@ function FileTreeItem({ name, treeData, path }: Props) {
treeData={treeData[childName]}
/>
))}
</TreeItem>
</TreeItemEnhanced>
);
}

Expand Down
62 changes: 62 additions & 0 deletions webapp/src/components/common/TreeItemEnhanced.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { TreeItem, type TreeItemProps } from "@mui/x-tree-view/TreeItem";
import { mergeSxProp } from "../../utils/muiUtils";
import * as RA from "ramda-adjunct";

export type TreeItemEnhancedProps = TreeItemProps;

function TreeItemEnhanced({ onClick, sx, ...rest }: TreeItemEnhancedProps) {
const canExpand = rest.children && RA.isNotEmpty(rest.children);

////////////////////////////////////////////////////////////////
// Event Handlers
////////////////////////////////////////////////////////////////

const handleClick: TreeItemEnhancedProps["onClick"] = (event) => {
const { target } = event;

// The item is not selected if the click is on the expand/collapse icon
if (
canExpand &&
target instanceof Element &&
target.closest(".MuiTreeItem-iconContainer")
) {
return;
}

onClick?.(event);
};

////////////////////////////////////////////////////////////////
// JSX
////////////////////////////////////////////////////////////////

return (
<TreeItem
{...rest}
onClick={handleClick}
sx={mergeSxProp(
{
"& > .MuiTreeItem-content": {
p: 0,
alignItems: "normal",
// Expand/collapse icon
"& > .MuiTreeItem-iconContainer": {
alignItems: "center",
borderTopLeftRadius: "inherit",
borderBottomLeftRadius: "inherit",
"&:hover": {
background: canExpand ? "inherit" : "none",
},
},
"& > .MuiTreeItem-label": {
py: 0.5,
},
},
},
sx,
)}
/>
);
}

export default TreeItemEnhanced;

0 comments on commit 580f974

Please sign in to comment.