-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Improve Tree View selection doc (#13105)
Signed-off-by: Michel Engelen <[email protected]> Signed-off-by: Gene Arch <[email protected]> Signed-off-by: Flavien DELANGLE <[email protected]> Co-authored-by: Michel Engelen <[email protected]> Co-authored-by: Olivier Tassinari <[email protected]> Co-authored-by: Gene Arch <[email protected]> Co-authored-by: Rom Grk <[email protected]> Co-authored-by: Nora <[email protected]>
- Loading branch information
1 parent
8515a27
commit dd9ea2b
Showing
12 changed files
with
449 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
99 changes: 99 additions & 0 deletions
99
docs/data/tree-view/rich-tree-view/selection/ParentChildrenSelectionRelationship.js
This file contains 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,99 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
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' }], | ||
}, | ||
]; | ||
|
||
function getItemDescendantsIds(item) { | ||
const ids = []; | ||
item.children?.forEach((child) => { | ||
ids.push(child.id); | ||
ids.push(...getItemDescendantsIds(child)); | ||
}); | ||
|
||
return ids; | ||
} | ||
|
||
export default function ParentChildrenSelectionRelationship() { | ||
const [selectedItems, setSelectedItems] = React.useState([]); | ||
const toggledItemRef = React.useRef({}); | ||
const apiRef = useTreeViewApiRef(); | ||
|
||
const handleItemSelectionToggle = (event, itemId, isSelected) => { | ||
toggledItemRef.current[itemId] = isSelected; | ||
}; | ||
|
||
const handleSelectedItemsChange = (event, newSelectedItems) => { | ||
setSelectedItems(newSelectedItems); | ||
|
||
// Select / unselect the children of the toggled item | ||
const itemsToSelect = []; | ||
const itemsToUnSelect = {}; | ||
Object.entries(toggledItemRef.current).forEach(([itemId, isSelected]) => { | ||
const item = apiRef.current.getItem(itemId); | ||
if (isSelected) { | ||
itemsToSelect.push(...getItemDescendantsIds(item)); | ||
} else { | ||
getItemDescendantsIds(item).forEach((descendantId) => { | ||
itemsToUnSelect[descendantId] = true; | ||
}); | ||
} | ||
}); | ||
|
||
const newSelectedItemsWithChildren = Array.from( | ||
new Set( | ||
[...newSelectedItems, ...itemsToSelect].filter( | ||
(itemId) => !itemsToUnSelect[itemId], | ||
), | ||
), | ||
); | ||
|
||
setSelectedItems(newSelectedItemsWithChildren); | ||
|
||
toggledItemRef.current = {}; | ||
}; | ||
|
||
return ( | ||
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}> | ||
<RichTreeView | ||
multiSelect | ||
checkboxSelection | ||
apiRef={apiRef} | ||
items={MUI_X_PRODUCTS} | ||
selectedItems={selectedItems} | ||
onSelectedItemsChange={handleSelectedItemsChange} | ||
onItemSelectionToggle={handleItemSelectionToggle} | ||
/> | ||
</Box> | ||
); | ||
} |
106 changes: 106 additions & 0 deletions
106
docs/data/tree-view/rich-tree-view/selection/ParentChildrenSelectionRelationship.tsx
This file contains 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,106 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
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' }], | ||
}, | ||
]; | ||
|
||
function getItemDescendantsIds(item: TreeViewBaseItem) { | ||
const ids: string[] = []; | ||
item.children?.forEach((child) => { | ||
ids.push(child.id); | ||
ids.push(...getItemDescendantsIds(child)); | ||
}); | ||
|
||
return ids; | ||
} | ||
|
||
export default function ParentChildrenSelectionRelationship() { | ||
const [selectedItems, setSelectedItems] = React.useState<string[]>([]); | ||
const toggledItemRef = React.useRef<{ [itemId: string]: boolean }>({}); | ||
const apiRef = useTreeViewApiRef(); | ||
|
||
const handleItemSelectionToggle = ( | ||
event: React.SyntheticEvent, | ||
itemId: string, | ||
isSelected: boolean, | ||
) => { | ||
toggledItemRef.current[itemId] = isSelected; | ||
}; | ||
|
||
const handleSelectedItemsChange = ( | ||
event: React.SyntheticEvent, | ||
newSelectedItems: string[], | ||
) => { | ||
setSelectedItems(newSelectedItems); | ||
|
||
// Select / unselect the children of the toggled item | ||
const itemsToSelect: string[] = []; | ||
const itemsToUnSelect: { [itemId: string]: boolean } = {}; | ||
Object.entries(toggledItemRef.current).forEach(([itemId, isSelected]) => { | ||
const item = apiRef.current!.getItem(itemId); | ||
if (isSelected) { | ||
itemsToSelect.push(...getItemDescendantsIds(item)); | ||
} else { | ||
getItemDescendantsIds(item).forEach((descendantId) => { | ||
itemsToUnSelect[descendantId] = true; | ||
}); | ||
} | ||
}); | ||
|
||
const newSelectedItemsWithChildren = Array.from( | ||
new Set( | ||
[...newSelectedItems, ...itemsToSelect].filter( | ||
(itemId) => !itemsToUnSelect[itemId], | ||
), | ||
), | ||
); | ||
|
||
setSelectedItems(newSelectedItemsWithChildren); | ||
|
||
toggledItemRef.current = {}; | ||
}; | ||
|
||
return ( | ||
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}> | ||
<RichTreeView | ||
multiSelect | ||
checkboxSelection | ||
apiRef={apiRef} | ||
items={MUI_X_PRODUCTS} | ||
selectedItems={selectedItems} | ||
onSelectedItemsChange={handleSelectedItemsChange} | ||
onItemSelectionToggle={handleItemSelectionToggle} | ||
/> | ||
</Box> | ||
); | ||
} |
9 changes: 9 additions & 0 deletions
9
docs/data/tree-view/rich-tree-view/selection/ParentChildrenSelectionRelationship.tsx.preview
This file contains 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,9 @@ | ||
<RichTreeView | ||
multiSelect | ||
checkboxSelection | ||
apiRef={apiRef} | ||
items={MUI_X_PRODUCTS} | ||
selectedItems={selectedItems} | ||
onSelectedItemsChange={handleSelectedItemsChange} | ||
onItemSelectionToggle={handleItemSelectionToggle} | ||
/> |
41 changes: 41 additions & 0 deletions
41
docs/data/tree-view/rich-tree-view/selection/SingleSelectTreeView.js
This file contains 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,41 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { RichTreeView } from '@mui/x-tree-view/RichTreeView'; | ||
|
||
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 SingleSelectTreeView() { | ||
return ( | ||
<Box sx={{ minHeight: 200, flexGrow: 1, maxWidth: 400 }}> | ||
<RichTreeView items={MUI_X_PRODUCTS} /> | ||
</Box> | ||
); | ||
} |
42 changes: 42 additions & 0 deletions
42
docs/data/tree-view/rich-tree-view/selection/SingleSelectTreeView.tsx
This file contains 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,42 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { RichTreeView } from '@mui/x-tree-view/RichTreeView'; | ||
import { TreeViewBaseItem } from '@mui/x-tree-view/models'; | ||
|
||
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 SingleSelectTreeView() { | ||
return ( | ||
<Box sx={{ minHeight: 200, flexGrow: 1, maxWidth: 400 }}> | ||
<RichTreeView items={MUI_X_PRODUCTS} /> | ||
</Box> | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
docs/data/tree-view/rich-tree-view/selection/SingleSelectTreeView.tsx.preview
This file contains 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 @@ | ||
<RichTreeView items={MUI_X_PRODUCTS} /> |
This file contains 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
Oops, something went wrong.