Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';

import { useTreeViewApiRef } from '@mui/x-tree-view/hooks';

const MUI_X_PRODUCTS = [
{
id: 'grid',
label: 'Data Grid',
children: [
{ id: 'grid-community', label: '@mui/x-data-grid' },
{ id: 'grid-pro', label: '@mui/x-data-grid-pro' },
{ id: 'grid-premium', label: '@mui/x-data-grid-premium' },
],
},
{
id: 'pickers',
label: 'Date and Time Pickers',
children: [
{ id: 'pickers-community', label: '@mui/x-date-pickers' },
{ id: 'pickers-pro', label: '@mui/x-date-pickers-pro' },
],
},
{
id: 'charts',
label: 'Charts',
children: [{ id: 'charts-community', label: '@mui/x-charts' }],
},
{
id: 'tree-view',
label: 'Tree View',
children: [{ id: 'tree-view-community', label: '@mui/x-tree-view' }],
},
];

export default function ApiMethodIsItemExpanded() {
const apiRef = useTreeViewApiRef();
const [isGridExpanded, setIsGridExpanded] = React.useState(false);
const [isSnackbarOpen, setIsSnackbarOpen] = React.useState(false);

const checkExpansion = () => {
setIsGridExpanded(apiRef.current.isItemExpanded('grid'));
setIsSnackbarOpen(true);
};

return (
<Stack spacing={2}>
<Stack spacing={2} direction="row">
<Button onClick={checkExpansion}>
Check if the the Data Grid is expanded
</Button>
<Snackbar
open={isSnackbarOpen}
autoHideDuration={3000}
onClose={() => setIsSnackbarOpen(false)}
message={`Data Grid is ${isGridExpanded ? 'expanded' : 'collapsed'}`}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
/>
</Stack>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView items={MUI_X_PRODUCTS} apiRef={apiRef} />
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import Snackbar from '@mui/material/Snackbar';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import { TreeViewBaseItem } from '@mui/x-tree-view/models';
import { useTreeViewApiRef } from '@mui/x-tree-view/hooks';

const MUI_X_PRODUCTS: TreeViewBaseItem[] = [
{
id: 'grid',
label: 'Data Grid',
children: [
{ id: 'grid-community', label: '@mui/x-data-grid' },
{ id: 'grid-pro', label: '@mui/x-data-grid-pro' },
{ id: 'grid-premium', label: '@mui/x-data-grid-premium' },
],
},
{
id: 'pickers',
label: 'Date and Time Pickers',
children: [
{ id: 'pickers-community', label: '@mui/x-date-pickers' },
{ id: 'pickers-pro', label: '@mui/x-date-pickers-pro' },
],
},
{
id: 'charts',
label: 'Charts',
children: [{ id: 'charts-community', label: '@mui/x-charts' }],
},
{
id: 'tree-view',
label: 'Tree View',
children: [{ id: 'tree-view-community', label: '@mui/x-tree-view' }],
},
];

export default function ApiMethodIsItemExpanded() {
const apiRef = useTreeViewApiRef();
const [isGridExpanded, setIsGridExpanded] = React.useState<boolean>(false);
const [isSnackbarOpen, setIsSnackbarOpen] = React.useState(false);

const checkExpansion = () => {
setIsGridExpanded(apiRef.current!.isItemExpanded('grid'));
setIsSnackbarOpen(true);
};

return (
<Stack spacing={2}>
<Stack spacing={2} direction="row">
<Button onClick={checkExpansion}>
Check if the the Data Grid is expanded
</Button>
<Snackbar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of this demo, but maybe we could try containing the snackbar inside the demo container 🤔
image
Also, if the snackbar is open and we click the button again, the snackbar closes. To me the toggle behavior is weird, so maybe we could disable the button while the snackbar is open, WDYT? 🎉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clicking anywhere closes the snackbar, it's not specific to the button 👍

open={isSnackbarOpen}
autoHideDuration={3000}
onClose={() => setIsSnackbarOpen(false)}
message={`Data Grid is ${isGridExpanded ? 'expanded' : 'collapsed'}`}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
/>
</Stack>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView items={MUI_X_PRODUCTS} apiRef={apiRef} />
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Stack spacing={2} direction="row">
<Button onClick={checkExpansion}>
Check if the the Data Grid is expanded
</Button>
<Snackbar
open={isSnackbarOpen}
autoHideDuration={3000}
onClose={() => setIsSnackbarOpen(false)}
message={`Data Grid is ${isGridExpanded ? 'expanded' : 'collapsed'}`}
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
/>
</Stack>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView items={MUI_X_PRODUCTS} apiRef={apiRef} />
</Box>
13 changes: 13 additions & 0 deletions docs/data/tree-view/rich-tree-view/expansion/expansion.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@ apiRef.current.setItemExpansion({
```

{{"demo": "ApiMethodSetItemExpansion.js"}}

### Check if an item is expanded

Use the `isItemExpanded()` API method to check the expansion of an item.

```ts
apiRef.current.setItemExpansion(
// The id of the item to check
itemId,
);
```

{{"demo": "ApiMethodIsItemExpanded.js"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import IconButton from '@mui/material/IconButton';
import CachedIcon from '@mui/icons-material/Cached';
import {
randomInt,
randomName,
randomId,
randomBoolean,
} from '@mui/x-data-grid-generator';
import { RichTreeViewPro } from '@mui/x-tree-view-pro/RichTreeViewPro';
import {
TreeItemContent,
TreeItemIconContainer,
TreeItemGroupTransition,
TreeItemLabel,
TreeItemRoot,
TreeItemCheckbox,
} from '@mui/x-tree-view/TreeItem';

import { TreeItemIcon } from '@mui/x-tree-view/TreeItemIcon';
import { TreeItemProvider } from '@mui/x-tree-view/TreeItemProvider';
import { TreeItemDragAndDropOverlay } from '@mui/x-tree-view/TreeItemDragAndDropOverlay';
import { useTreeItem } from '@mui/x-tree-view/useTreeItem';

const LATENCY_MS = 300;

const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
const { id, itemId, label, disabled, children, ...other } = props;

const {
getContextProviderProps,
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
getDragAndDropOverlayProps,
status,
publicAPI,
} = useTreeItem({
id,
itemId,
children,
label,
disabled,
rootRef: ref,
});

const refreshChildren = (event) => {
event.defaultMuiPrevented = true;
publicAPI.updateItemChildren(itemId);
};

return (
<TreeItemProvider {...getContextProviderProps()}>
<TreeItemRoot {...getRootProps(other)}>
<TreeItemContent {...getContentProps()}>
<TreeItemIconContainer {...getIconContainerProps()}>
<TreeItemIcon status={status} />
</TreeItemIconContainer>
<Box sx={{ flexGrow: 1, display: 'flex', gap: 1 }}>
<TreeItemCheckbox {...getCheckboxProps()} />
<TreeItemLabel {...getLabelProps()} />
{status.expandable && status.expanded && (
<IconButton
size="small"
onClick={refreshChildren}
title="Refetch children"
>
<CachedIcon fontSize="small" />
</IconButton>
)}
</Box>
<TreeItemDragAndDropOverlay {...getDragAndDropOverlayProps()} />
</TreeItemContent>
{children && <TreeItemGroupTransition {...getGroupTransitionProps()} />}
</TreeItemRoot>
</TreeItemProvider>
);
});

export default function ApiMethodUpdateItemChildren() {
const fetchData = async () => {
const length = randomInt(5, 10);
const rows = Array.from({ length }, () => ({
id: randomId(),
label: randomName({}, {}),
...(randomBoolean() ? { childrenCount: length } : {}),
}));

return new Promise((resolve) => {
setTimeout(() => {
resolve(rows);
}, LATENCY_MS);
});
};

return (
<Box sx={{ width: '300px' }}>
<RichTreeViewPro
items={[]}
dataSource={{
getChildrenCount: (item) => item?.childrenCount,
getTreeItems: fetchData,
}}
slots={{ item: CustomTreeItem }}
/>
</Box>
);
}
Loading
Loading