-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
flaviendelangle
merged 17 commits into
mui:master
from
flaviendelangle:select-item-apiRef
Jul 2, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
dcac920
[TreeView] Add selectItem and getItemDOMElement to public API
flaviendelangle 1a0c99d
Fix doc
flaviendelangle 362e19d
Fix
flaviendelangle d492cda
Fix
flaviendelangle 4231ec0
Fix TS
flaviendelangle d601018
Merge branch 'master' into select-item-apiRef
flaviendelangle 74801f1
Add tests
flaviendelangle c1f6dbd
Fix
flaviendelangle 095235c
Fix tests
flaviendelangle b3a172a
Fix tests
flaviendelangle 6fedea2
Fix tests
flaviendelangle f0ea877
Merge branch 'master' into select-item-apiRef
flaviendelangle 440c967
Review: Nora
flaviendelangle 8bad822
Fix
flaviendelangle db2b0b0
Fix
flaviendelangle 47f5326
Fix
flaviendelangle f7a4bbe
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
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
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
64 changes: 64 additions & 0 deletions
64
docs/data/tree-view/rich-tree-view/items/ApiMethodGetItemDOMElement.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,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> | ||
); | ||
} |
64 changes: 64 additions & 0 deletions
64
docs/data/tree-view/rich-tree-view/items/ApiMethodGetItemDOMElement.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,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> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
docs/data/tree-view/rich-tree-view/items/ApiMethodGetItemDOMElement.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,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> |
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
59 changes: 59 additions & 0 deletions
59
docs/data/tree-view/rich-tree-view/selection/ApiMethodSelectItem.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,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> | ||
); | ||
} |
59 changes: 59 additions & 0 deletions
59
docs/data/tree-view/rich-tree-view/selection/ApiMethodSelectItem.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,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> | ||
); | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/data/tree-view/rich-tree-view/selection/ApiMethodSelectItem.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,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> |
65 changes: 65 additions & 0 deletions
65
docs/data/tree-view/rich-tree-view/selection/ApiMethodSelectItemKeepExistingSelection.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,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> | ||
); | ||
} |
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 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