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] Visually select the ancestors of a selected item #13215

Open
Tracked by #12701
amit-8 opened this issue May 22, 2024 · 4 comments
Open
Tracked by #12701

[TreeView] Visually select the ancestors of a selected item #13215

amit-8 opened this issue May 22, 2024 · 4 comments
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! customization: extend Logic customizability recipe support: question Community support but can be turned into an improvement

Comments

@amit-8
Copy link

amit-8 commented May 22, 2024

The problem in depth

i am using treeview component and trying a feature where when we hover over the child the parent should also have same effect and when we click the child parent should also be selected

Your environment

`npx @mui/envinfo`
  Don't forget to mention which browser you used.
  Output from `npx @mui/envinfo` goes here.

Search keywords: treeview

@amit-8 amit-8 added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users support: unknown Support request type not verified yet. Head to https://tools-public.mui.com/prod/pages/jyhs86t labels May 22, 2024
Copy link

You have created a support request under the "Priority Support" terms, which is a paid add-on to MUI X Premium ⏰. Please validate your support key using the link below:

https://tools-public.mui.com/prod/pages/jyhs86t?repo=mui-x&issueId=13215

Do not share your support key in this issue!

Priority Support is only provided to verified customers. Once you have verified your support key, we will remove the support: unknown label and add the support: priority label to this issue. Only then the time for the SLA will start counting.

@michelengelen michelengelen added support: question Community support but can be turned into an improvement and removed support: commercial Support request from paid users support: unknown Support request type not verified yet. Head to https://tools-public.mui.com/prod/pages/jyhs86t labels May 23, 2024
@michelengelen michelengelen changed the title in treeview component when child is selected parent should also have the same effect [tree view] when child is selected parent should also have the same effect May 23, 2024
@michelengelen
Copy link
Member

Hey @amit-8
You can use the treeview public api for this. There is the getItem method with which you can get the parent item by its id. With this item you can do your modifications as needed.

@flaviendelangle should we document cases like this with a recipe as well?

@michelengelen michelengelen added status: waiting for author Issue with insufficient information customization: extend Logic customizability component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 23, 2024
@flaviendelangle
Copy link
Member

It's an interesting use-case
For me it can be done in user-land as you said, but maybe not in an optimal way.
To do it, you one way is to use multiSelect and control the selection using selectedItems and onSelectedItemsChange to select all the ancestors when selecting an item.
But since you are using multiSelect, the Tree View will allow stuff like range selection using Shift which may not be what the user want here. You may be able to opt-out of those behaviors using event.defaultMuiPrevented on the relevant event handlers, but this could be quite fragile.

The clean implementation would be to only display the ancestors as selected but not have it in the model, that way people can use multiSelect if they want or stay with single selection.
To do that in user land would require creating a custom TreeItem, control the selection and pass a prop on the item slot of the RichTreeView component (for SimpleTreeView the implementation would differ here) saying if the item is visually selected or not and re-apply manually the style if needed.

We should definitely do a recipe a see how viable each approach is and pick the best one.

@flaviendelangle flaviendelangle changed the title [tree view] when child is selected parent should also have the same effect [TreeVIew] when child is selected parent should also have the same effect May 23, 2024
@michelengelen michelengelen added recipe and removed status: waiting for author Issue with insufficient information labels May 24, 2024
@flaviendelangle flaviendelangle changed the title [TreeVIew] when child is selected parent should also have the same effect [TreeView] when child is selected parent should also have the same effect May 27, 2024
@flaviendelangle flaviendelangle changed the title [TreeView] when child is selected parent should also have the same effect [TreeView] Visually select the ancestors of a selected item May 27, 2024
@igortas
Copy link

igortas commented Aug 16, 2024

@flaviendelangle
I have similar case, where I need to have controlled selection, and myself provide which items I like to be selected, in my scenario, I like to make selected, all different items that exists in different parents that are expanded...

--- A expanded
------- D this should be selected
--- B expanded
--------D also this
--- C

What I've tried is:

selectedItems={
   selectedItems
   .filter(item => item.internalId === selectedInternalId)
   .map(item => item.treeId)
   .join(',')}

I have my own selectedItems helper arrray, I have two items with same internalId(but different treeId), but array provided like this does not makes selected the items.

I'm using <RichTreeView /> and expanded and selected props I'm controlling myself:

<RichTreeView
		selectedItems={
			selectedItems
				.filter(item => item.internalId === selectedInternalId)
				.map(item => item.treeId).join(',')}
		expandedItems={selectedItems
			.filter(item => item.expanded)
			.map(item => item.treeId)}
		items={treeViewData}
		slots={{ item: TreeViewNode }}
	/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! customization: extend Logic customizability recipe support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests

4 participants