Open royassis opened 1 day ago
Hey @royassis π
Thanks for opening the issue. I think I will need an exact reproduction case for what you are trying to do π€ You should technically be able to conditionally set a loading state for your items doing something like this. I simplified the demo a bit, but the way you do it depends on your specific example
Thanks @noraleonte !
Works like a charm. Got any idea why the snippet I added that use useTreeItem2
and TreeItem2Provider
won't work?
Happy to help @royassis π
Not sure why your snippet didn't work π€ You should be able to do the same thing even if you build the custom item with useTreeItem2
and TreeItem2Provider
π€ I think it depends on how you update the state (the logic is missing from your snippet)
Here's an example with the file explorer demo :)
Hey @noraleonte
I updated the snippet to use the same logic. Sadly it still won't work. I'm probably missing something. I use this though:
<CustomLabel
{...getLabelProps({
icon,
expandable: expandable && status.expanded,
loading: isLoading(props.itemId)
})}
/>
@royassis Would you be able to provide a CodeSandbox or Stackblitz example with what you are trying to achieve exactly? Otherwise I'm not sure I can figure out the problem π
@noraleonte sure thing, i'll send a link to CodeSandbox
@noraleonte https://codesandbox.io/p/sandbox/mui-tree-test-3llkm5
src\components\steps\AccordionContent\FileExplorer\CustomTreeItem2.jsx
@noraleonte could it be that the props spreading is the issue here?
@michelengelen it shouldn't be a problem π€
@royassis In this case I think the RichTreeView is using a different TreeItem, not th eone with the spinning. TreeView.tsx
line 126
That should fix it I think π€
Hey @noraleonte it does, at least I think it does.
Look at line 146 in CustomTreeItem.jsx
Steps to reproduce
loading
, on each tree element. Which is boolean.import * as React from "react"; import clsx from "clsx"; import { animated, useSpring } from "@react-spring/web"; import { styled, alpha } from "@mui/material/styles";
import Box from "@mui/material/Box"; import Collapse from "@mui/material/Collapse"; import Typography from "@mui/material/Typography"; import ArticleIcon from "@mui/icons-material/Article"; import DeleteIcon from "@mui/icons-material/Delete"; import FolderOpenIcon from "@mui/icons-material/FolderOpen"; import FolderRounded from "@mui/icons-material/FolderRounded"; import ImageIcon from "@mui/icons-material/Image"; import PictureAsPdfIcon from "@mui/icons-material/PictureAsPdf"; import VideoCameraBackIcon from "@mui/icons-material/VideoCameraBack"; import { treeItemClasses } from "@mui/x-tree-view/TreeItem"; import { useTreeItem2 } from "@mui/x-tree-view/useTreeItem2"; import { TreeItem2Checkbox, TreeItem2Content, TreeItem2IconContainer, TreeItem2Label, TreeItem2Root, } from "@mui/x-tree-view/TreeItem2"; import { TreeItem2Icon } from "@mui/x-tree-view/TreeItem2Icon"; import { TreeItem2Provider } from "@mui/x-tree-view/TreeItem2Provider"; import { TreeItem2DragAndDropOverlay } from "@mui/x-tree-view/TreeItem2DragAndDropOverlay";
import CircularProgress from '@mui/material/CircularProgress';
function DotIcon() { return ( <Box sx={{ width: 6, height: 6, borderRadius: "70%", bgcolor: "warning.main", display: "inline-block", verticalAlign: "middle", zIndex: 1, mx: 1, }} /> ); }
const StyledTreeItemRoot = styled(TreeItem2Root)(({ theme }) => ({ color: theme.palette.grey[400], position: "relative",
}, ...theme.applyStyles("light", { color: theme.palette.grey[800], }), }));
const CustomTreeItemContent = styled(TreeItem2Content)(({ theme }) => ({ flexDirection: "row-reverse", borderRadius: theme.spacing(0.7), marginBottom: theme.spacing(0.5), marginTop: theme.spacing(0.5), padding: theme.spacing(0.5), paddingRight: theme.spacing(1), fontWeight: 500,
}, "&:hover": { backgroundColor: alpha(theme.palette.primary.main, 0.1), color: "white", ...theme.applyStyles("light", { color: theme.palette.primary.main, }), },
}, }));
const AnimatedCollapse = animated(Collapse);
function TransitionComponent(props) { const style = useSpring({ to: { opacity: props.in ? 1 : 0, transform:
translate3d(0,${props.in ? 0 : 20}px,0)
, }, });return <AnimatedCollapse style={style} {...props} />; }
const StyledTreeItemLabelText = styled(Typography)({ color: "inherit", fontFamily: "General Sans", fontWeight: 500, });
function CustomLabel({ icon: Icon, expandable, children, ...other }) { console.log("other.loading",other.loading) return ( <TreeItem2Label {...other} sx={{ display: "flex", alignItems: "center", }}
); }
const isExpandable = (reactChildren) => { if (Array.isArray(reactChildren)) { return reactChildren.length > 0 && reactChildren.some(isExpandable); } return Boolean(reactChildren); };
const getIconFromFileType = (fileType) => { switch (fileType) { case "file": return ImageIcon; case "pdf": return PictureAsPdfIcon; case "doc": return ArticleIcon; case "video": return VideoCameraBackIcon; case "directory": return FolderRounded; case "pinned": return FolderOpenIcon; case "trash": return DeleteIcon; default: return ArticleIcon; } };
export const CustomTreeItem = React.forwardRef( function CustomTreeItem(props, ref) { const { id, itemId, label, disabled, children, ...other } = props;
} );
loading
property for some of the items.Current behavior
loading
property is not updating and spinner is not showing.Expected behavior
loading
property is updating and spinner is showing.Context
I am using the Rich Tree View. I want to display a spinner when I am loading additional tree children from my backend. I want to render a spinner on a parent tree element when it is loading.
Your environment
``` System: OS: Windows 11 10.0.22631 Binaries: Node: 20.13.1 - C:\Program Files\nodejs\node.EXE npm: 10.8.3 - C:\Program Files\nodejs\npm.CMD pnpm: Not Found Browsers: Chrome: Not Found Edge: Chromium (127.0.2651.74) npmPackages: @emotion/react: ^11.13.3 => 11.13.3 @emotion/styled: ^11.13.0 => 11.13.0 @mui/core-downloads-tracker: 6.1.1 @mui/icons-material: ^6.1.1 => 6.1.1 @mui/material: ^6.1.1 => 6.1.1 @mui/private-theming: 6.1.1 @mui/styled-engine: 6.1.1 @mui/system: 6.1.1 @mui/types: 7.2.17 @mui/utils: 6.1.1 @mui/x-internals: 7.18.0 @mui/x-tree-view: ^7.18.0 => 7.18.0 @types/react: 18.3.8 react: ^18.3.1 => 18.3.1 react-dom: ^18.3.1 => 18.3.1 typescript: 4.9.5 ```npx @mui/envinfo
Using Chrome
Search keywords: update treeview richtreeview rich tree