-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[tree view] Expose the methods to manually refetch the children of an item #19248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
585d03b
[tree view] Expose the methods to manually refetch the children of an…
flaviendelangle e712549
Improve DX
flaviendelangle 42a17f0
Fix CI
flaviendelangle b9d16c8
Fix
flaviendelangle d50c27a
Merge branch 'master' into refetch-children
flaviendelangle 27edef6
Fix TS
flaviendelangle 933e596
Fix
flaviendelangle fb3b277
Merge branch 'master' into refetch-children
flaviendelangle f45e641
Review: Nora
flaviendelangle 1e31e5d
Review: Nora
flaviendelangle 0e8ccf0
Fix
flaviendelangle 890bb83
Fix CI
flaviendelangle cc8b95c
Merge branch 'master' into refetch-children
flaviendelangle fdd064f
Merge branch 'master' into refetch-children
flaviendelangle 7df848f
Merge remote-tracking branch 'origin/refetch-children' into refetch-c…
flaviendelangle 6ce25a6
Merge branch 'master' into refetch-children
flaviendelangle c97672c
Merge branch 'master' into refetch-children
flaviendelangle a81e04b
Fix
flaviendelangle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
docs/data/tree-view/rich-tree-view/expansion/ApiMethodIsItemExpanded.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
69 changes: 69 additions & 0 deletions
69
docs/data/tree-view/rich-tree-view/expansion/ApiMethodIsItemExpanded.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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> | ||
); | ||
} |
15 changes: 15 additions & 0 deletions
15
docs/data/tree-view/rich-tree-view/expansion/ApiMethodIsItemExpanded.tsx.preview
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
docs/data/tree-view/rich-tree-view/lazy-loading/ApiMethodUpdateItemChildren.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 🤔

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? 🎉
There was a problem hiding this comment.
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 👍