Skip to content

Commit

Permalink
[TreeView] Add support for checkbox selection (mui#11452)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviendelangle authored and DungTiger committed Jul 23, 2024
1 parent 53c522c commit 3f89cf7
Show file tree
Hide file tree
Showing 49 changed files with 930 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TreeItem2GroupTransition,
TreeItem2Label,
TreeItem2Root,
TreeItem2Checkbox,
} from '@mui/x-tree-view/TreeItem2';
import { TreeItem2Icon } from '@mui/x-tree-view/TreeItem2Icon';
import { TreeItem2Provider } from '@mui/x-tree-view/TreeItem2Provider';
Expand Down Expand Up @@ -46,6 +47,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand All @@ -69,6 +71,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
>
{label[0]}
</Avatar>
<TreeItem2Checkbox {...getCheckboxProps()} />
<TreeItem2Label {...getLabelProps()} />
</Box>
</CustomTreeItemContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TreeItem2GroupTransition,
TreeItem2Label,
TreeItem2Root,
TreeItem2Checkbox,
} from '@mui/x-tree-view/TreeItem2';
import { TreeItem2Icon } from '@mui/x-tree-view/TreeItem2Icon';
import { TreeItem2Provider } from '@mui/x-tree-view/TreeItem2Provider';
Expand Down Expand Up @@ -56,6 +57,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand All @@ -79,6 +81,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
>
{(label as string)[0]}
</Avatar>
<TreeItem2Checkbox {...getCheckboxProps()} />
<TreeItem2Label {...getLabelProps()} />
</Box>
</CustomTreeItemContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import { treeItemClasses } from '@mui/x-tree-view/TreeItem';
import { unstable_useTreeItem2 as useTreeItem2 } from '@mui/x-tree-view/useTreeItem2';
import {
TreeItem2Checkbox,
TreeItem2Content,
TreeItem2IconContainer,
TreeItem2Label,
Expand Down Expand Up @@ -211,6 +212,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand Down Expand Up @@ -242,7 +244,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
<TreeItem2IconContainer {...getIconContainerProps()}>
<TreeItem2Icon status={status} />
</TreeItem2IconContainer>

<TreeItem2Checkbox {...getCheckboxProps()} />
<CustomLabel
{...getLabelProps({ icon, expandable: expandable && status.expanded })}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
UseTreeItem2Parameters,
} from '@mui/x-tree-view/useTreeItem2';
import {
TreeItem2Checkbox,
TreeItem2Content,
TreeItem2IconContainer,
TreeItem2Label,
Expand Down Expand Up @@ -247,6 +248,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand Down Expand Up @@ -278,7 +280,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
<TreeItem2IconContainer {...getIconContainerProps()}>
<TreeItem2Icon status={status} />
</TreeItem2IconContainer>

<TreeItem2Checkbox {...getCheckboxProps()} />
<CustomLabel
{...getLabelProps({ icon, expandable: expandable && status.expanded })}
/>
Expand Down
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';

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 CheckboxMultiSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<RichTreeView multiSelect checkboxSelection items={MUI_X_PRODUCTS} />
</Box>
);
}
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, TreeViewBaseItem } from '@mui/x-tree-view';

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 CheckboxMultiSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<RichTreeView multiSelect checkboxSelection items={MUI_X_PRODUCTS} />
</Box>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<RichTreeView multiSelect checkboxSelection items={MUI_X_PRODUCTS} />
41 changes: 41 additions & 0 deletions docs/data/tree-view/rich-tree-view/selection/CheckboxSelection.js
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';

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 CheckboxSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<RichTreeView checkboxSelection items={MUI_X_PRODUCTS} />
</Box>
);
}
41 changes: 41 additions & 0 deletions docs/data/tree-view/rich-tree-view/selection/CheckboxSelection.tsx
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, TreeViewBaseItem } from '@mui/x-tree-view';

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 CheckboxSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<RichTreeView checkboxSelection items={MUI_X_PRODUCTS} />
</Box>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<RichTreeView checkboxSelection items={MUI_X_PRODUCTS} />
10 changes: 10 additions & 0 deletions docs/data/tree-view/rich-tree-view/selection/selection.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ Use the `disableSelection` prop if you don't want your items to be selectable:

{{"demo": "DisableSelection.js"}}

## Checkbox selection

To activate checkbox selection set `checkboxSelection={true}`:

{{"demo": "CheckboxSelection.js"}}

This is also compatible with multi selection:

{{"demo": "CheckboxMultiSelection.js"}}

## Controlled selection

Use the `selectedItems` prop to control the selected items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
TreeItem2GroupTransition,
TreeItem2Label,
TreeItem2Root,
TreeItem2Checkbox,
} from '@mui/x-tree-view/TreeItem2';
import { TreeItem2Icon } from '@mui/x-tree-view/TreeItem2Icon';
import { TreeItem2Provider } from '@mui/x-tree-view/TreeItem2Provider';
Expand All @@ -25,6 +26,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand All @@ -37,6 +39,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(props, ref) {
<TreeItem2IconContainer {...getIconContainerProps()}>
<TreeItem2Icon status={status} />
</TreeItem2IconContainer>
<TreeItem2Checkbox {...getCheckboxProps()} />
<Box sx={{ flexGrow: 1, display: 'flex', gap: 1 }}>
<Avatar
sx={(theme) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TreeItem2GroupTransition,
TreeItem2Label,
TreeItem2Root,
TreeItem2Checkbox,
} from '@mui/x-tree-view/TreeItem2';
import { TreeItem2Icon } from '@mui/x-tree-view/TreeItem2Icon';
import { TreeItem2Provider } from '@mui/x-tree-view/TreeItem2Provider';
Expand All @@ -35,6 +36,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
getRootProps,
getContentProps,
getIconContainerProps,
getCheckboxProps,
getLabelProps,
getGroupTransitionProps,
status,
Expand All @@ -47,6 +49,7 @@ const CustomTreeItem = React.forwardRef(function CustomTreeItem(
<TreeItem2IconContainer {...getIconContainerProps()}>
<TreeItem2Icon status={status} />
</TreeItem2IconContainer>
<TreeItem2Checkbox {...getCheckboxProps()} />
<Box sx={{ flexGrow: 1, display: 'flex', gap: 1 }}>
<Avatar
sx={(theme) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView';
import { TreeItem } from '@mui/x-tree-view/TreeItem';

export default function CheckboxMultiSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<SimpleTreeView multiSelect checkboxSelection>
<TreeItem itemId="grid" label="Data Grid">
<TreeItem itemId="grid-community" label="@mui/x-data-grid" />
<TreeItem itemId="grid-pro" label="@mui/x-data-grid-pro" />
<TreeItem itemId="grid-premium" label="@mui/x-data-grid-premium" />
</TreeItem>
<TreeItem itemId="pickers" label="Date and Time Pickers">
<TreeItem itemId="pickers-community" label="@mui/x-date-pickers" />
<TreeItem itemId="pickers-pro" label="@mui/x-date-pickers-pro" />
</TreeItem>
<TreeItem itemId="charts" label="Charts">
<TreeItem itemId="charts-community" label="@mui/x-charts" />
</TreeItem>
<TreeItem itemId="tree-view" label="Tree View">
<TreeItem itemId="tree-view-community" label="@mui/x-tree-view" />
</TreeItem>
</SimpleTreeView>
</Box>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView';
import { TreeItem } from '@mui/x-tree-view/TreeItem';

export default function CheckboxMultiSelection() {
return (
<Box sx={{ height: 264, flexGrow: 1, maxWidth: 400 }}>
<SimpleTreeView multiSelect checkboxSelection>
<TreeItem itemId="grid" label="Data Grid">
<TreeItem itemId="grid-community" label="@mui/x-data-grid" />
<TreeItem itemId="grid-pro" label="@mui/x-data-grid-pro" />
<TreeItem itemId="grid-premium" label="@mui/x-data-grid-premium" />
</TreeItem>
<TreeItem itemId="pickers" label="Date and Time Pickers">
<TreeItem itemId="pickers-community" label="@mui/x-date-pickers" />
<TreeItem itemId="pickers-pro" label="@mui/x-date-pickers-pro" />
</TreeItem>
<TreeItem itemId="charts" label="Charts">
<TreeItem itemId="charts-community" label="@mui/x-charts" />
</TreeItem>
<TreeItem itemId="tree-view" label="Tree View">
<TreeItem itemId="tree-view-community" label="@mui/x-tree-view" />
</TreeItem>
</SimpleTreeView>
</Box>
);
}
Loading

0 comments on commit 3f89cf7

Please sign in to comment.