mui-x icon indicating copy to clipboard operation
mui-x copied to clipboard

[tree view] Handle onClick on a RichTreeView item

Open rgavrilov opened this issue 1 year ago • 4 comments

The problem in depth

I would like to configure my RichTreeView using its items property and then supply a handler that would receive a selected item.

 <RichTreeView items={treeViewItems} slots={{ item: TreeItem2 }} />

My current solution is to capture and store in a state the focused item, and then use that state from inside onClick attached to the tree item using slotProps={ item: { ...

Notice that my item has two pieces of information I need, so I have to combine them into a single string and then parse it out:

                onItemFocus={(e, itemId) => {
                    const [calculator, assetId] = itemId.split('/');
                    if (assetId) {
                        setSelectedResult(results[calculator][assetId]);
                    } else {
                        setSelectedResult(undefined);
                    }
                }}
                slotProps={{
                    item: {
                        onClick: () => {
                            if (selectedResult) {
                                modal.show(selectedResult);
                                ...

I know I can render TreeItem2 children explicitly, but that is not as neat.

Ideally, I would like to have onItemClick handler, similar to onItemFocus, that would receive the whole item from the items collection.

Your environment

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

Search keywords: RichTreeView Order ID: 52426

rgavrilov avatar Apr 19 '24 13:04 rgavrilov

@flaviendelangle should we keep this issue open to track the implementation of an onItemClick handler? Or will it be tracked on #12839?

michelengelen avatar Apr 22 '24 10:04 michelengelen

I think we should keep both for now, depending on what we decide for #12839, onItemClick might not be covered there :+1:

flaviendelangle avatar Apr 22 '24 10:04 flaviendelangle

Ok, in that case lets add this to the board as a feature request/enhancement! 👍🏼

michelengelen avatar Apr 22 '24 10:04 michelengelen

+1, getting just a string with id instead of a selected item is quite inconvenient

Cadogantes avatar Aug 27 '24 12:08 Cadogantes

@Cadogantes does this doc section solve your problem?

If not, could you describe it more in depth so that we can adress it properly?

If it does, I think we will close this issue :+1:

flaviendelangle avatar Sep 05 '24 14:09 flaviendelangle

@flaviendelangle Oh yes, this seems to be a solution! Thanks!

Cadogantes avatar Sep 05 '24 18:09 Cadogantes

This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue. Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.

[!NOTE] We value your feedback @rgavrilov! How was your experience with our support team? If you could spare a moment, we'd love to hear your thoughts in this brief Support Satisfaction survey. Your insights help us improve!

github-actions[bot] avatar Sep 06 '24 07:09 github-actions[bot]