Skip to content
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

[TreeView] Add selectItem and getItemDOMElement methods to the public API #13485

Merged
merged 17 commits into from
Jul 2, 2024
Merged
5 changes: 3 additions & 2 deletions docs/data/tree-view/rich-tree-view/expansion/expansion.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ Use the `setItemExpansion` API method to change the expansion of an item.
apiRef.current.setItemExpansion(
// The DOM event that triggered the change
event,
// The ID of the item to expand or collapse
// The id of the item to expand or collapse
Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to unify a bit the wording for those boolean parameters throughout the Tree View codebase
And to always have the same descriptions for the params in the JSDoc and in the doc section

itemId,
// `true` if the item should be expanded, `false` if it should be collapsed
// If `true` the item will be expanded
// If `false` the item will be collapsed
isExpanded,
);
```
Expand Down
2 changes: 1 addition & 1 deletion docs/data/tree-view/rich-tree-view/focus/focus.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Use the `focusItem` API method to focus a specific item.
apiRef.current.focusItem(
// The DOM event that triggered the change
event,
// The ID of the item to focus
// The id of the item to focus
itemId,
);
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';

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

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 ApiMethodGetItemDOMElement() {
const apiRef = useTreeViewApiRef();
const handleScrollToChartsCommunity = (event) => {
apiRef.current.focusItem(event, 'charts-community');
apiRef.current
.getItemDOMElement('charts-community')
?.scrollIntoView({ block: 'center' });
};

return (
<Stack spacing={2}>
<div>
<Button onClick={handleScrollToChartsCommunity}>
Focus and scroll to charts community item
</Button>
</div>
<Box sx={{ height: 200, minWidth: 250, overflowY: 'scroll' }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid', 'pickers', 'charts', 'tree-view']}
/>
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import { TreeViewBaseItem } from '@mui/x-tree-view/models';
import { useTreeViewApiRef } from '@mui/x-tree-view/hooks/useTreeViewApiRef';

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 ApiMethodGetItemDOMElement() {
const apiRef = useTreeViewApiRef();
const handleScrollToChartsCommunity = (event: React.SyntheticEvent) => {
apiRef.current!.focusItem(event, 'charts-community');
apiRef
.current!.getItemDOMElement('charts-community')
?.scrollIntoView({ block: 'center' });
};

return (
<Stack spacing={2}>
<div>
<Button onClick={handleScrollToChartsCommunity}>
Focus and scroll to charts community item
</Button>
</div>
<Box sx={{ height: 200, minWidth: 250, overflowY: 'scroll' }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid', 'pickers', 'charts', 'tree-view']}
/>
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<Button onClick={handleScrollToChartsCommunity}>
Focus and scroll to charts community item
</Button>
</div>
<Box sx={{ height: 200, minWidth: 250, overflowY: 'scroll' }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid', 'pickers', 'charts', 'tree-view']}
/>
</Box>
15 changes: 14 additions & 1 deletion docs/data/tree-view/rich-tree-view/items/items.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,22 @@ Use the `getItem` API method to get an item by its ID.

```ts
const item = apiRef.current.getItem(
// The ID of the item to retrieve
// The id of the item to retrieve
itemId,
);
```

{{"demo": "ApiMethodGetItem.js", "defaultCodeOpen": false}}

### Get an item's DOM element by ID

Use the `getItemDOMElement` API method to get an item's DOM element by its ID.

```ts
const itemElement = apiRef.current.getItemDOMElement(
// The id of the item to get the DOM element of
itemId,
);
```

{{"demo": "ApiMethodGetItemDOMElement.js", "defaultCodeOpen": false}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';

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

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 ApiMethodSelectItem() {
const apiRef = useTreeViewApiRef();
const handleSelectGridPro = (event) => {
apiRef.current?.selectItem({ event, itemId: 'grid-pro' });
};

return (
<Stack spacing={2}>
<div>
<Button onClick={handleSelectGridPro}>Select grid pro item</Button>
</div>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid']}
/>
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';
import { TreeViewBaseItem } from '@mui/x-tree-view/models';
import { RichTreeView } from '@mui/x-tree-view/RichTreeView';
import { useTreeViewApiRef } from '@mui/x-tree-view/hooks/useTreeViewApiRef';

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 ApiMethodSelectItem() {
const apiRef = useTreeViewApiRef();
const handleSelectGridPro = (event: React.SyntheticEvent) => {
apiRef.current?.selectItem({ event, itemId: 'grid-pro' });
};

return (
<Stack spacing={2}>
<div>
<Button onClick={handleSelectGridPro}>Select grid pro item</Button>
</div>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid']}
/>
</Box>
</Stack>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div>
<Button onClick={handleSelectGridPro}>Select grid pro item</Button>
</div>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid']}
/>
</Box>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as React from 'react';
import Box from '@mui/material/Box';
import Stack from '@mui/material/Stack';
import Button from '@mui/material/Button';

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

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 ApiMethodSelectItemKeepExistingSelection() {
const apiRef = useTreeViewApiRef();
const handleSelectGridPro = (event) => {
apiRef.current?.selectItem({
event,
itemId: 'grid-pro',
keepExistingSelection: true,
});
};

return (
<Stack spacing={2}>
<div>
<Button onClick={handleSelectGridPro}>Select grid pro item</Button>
</div>
<Box sx={{ minHeight: 352, minWidth: 250 }}>
<RichTreeView
items={MUI_X_PRODUCTS}
apiRef={apiRef}
defaultExpandedItems={['grid']}
multiSelect
defaultSelectedItems={['grid-premium']}
/>
</Box>
</Stack>
);
}
Loading