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] Expand control at TreeItem level #9960

Closed
1 task done
OoDeLally opened this issue May 3, 2020 · 2 comments · Fixed by #12595
Closed
1 task done

[TreeView] Expand control at TreeItem level #9960

OoDeLally opened this issue May 3, 2020 · 2 comments · Fixed by #12595
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! discussion dx Related to developers' experience new feature New feature or request

Comments

@OoDeLally
Copy link

OoDeLally commented May 3, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

Currently there is no way to:

  1. Expand all TreeItem by default without knowing the TreeItem's ids in advance.
  2. Expand a TreeItem from it's own props.

Both of these features can be solved by adding defaultExpanded and expanded props to TreeItem itself, that would override whatever dictated by the root TreeView.

If this is not technically possible, it would be geat to at least have a defaultExpandAll={true} prop to TreeView.

Examples 🌈

// Here only `foo` and `bar` would be expanded.
<TreeView defaultExpanded={['foo', 'baz']}>
  <TreeItem nodeId="foo" label="foo" />
  <TreeItem nodeId="bar" label="bar" defaultExpanded={true} />
  <TreeItem nodeId="baz" label="baz" defaultExpanded={false} />
</TreeView>

Motivation 🔦

I am trying to display a dynamic tree, where nodes can be added and removed.

Because of the dynamic nature, I cannot use defaultExpanded, since this props is only read at the first mount, and never re-read (even if I manage to add subsequent ids to the array).
Neither can I use expanded (which does work dynamically), since it forces the tree to be "controlled", which I do not want.

@oliviertassinari oliviertassinari added the component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! label May 3, 2020
@oliviertassinari oliviertassinari changed the title TreeView expand control at TreeItem level [TreeView] Expand control at TreeItem level Jun 16, 2020
@trombipeti
Copy link

Hi all, after days of searching I came across this issue. I need kind of the same thing, though in my situation I don't mind if the TreeView is controlled or not.

I tried to make it work with handleExpansion from the useTreeItem() hook.
It is not a perfect solution since it expects me to pass a React.SyntheticEvent which I don't really have since the content is assembled dynamically but without user interaction (data comes from the server).

Looking at the source code, this event is only used when it is passed to onNodeToggle, which is at the and user code again. Could this event parameter be made optional?

I still feel it'd be some sort of a half-solution, because it'd be using a callback instead of a simple prop, but at least it would work.
Something like what @OoDeLally suggested would be a nicer solution.

@joserodolfofreitas joserodolfofreitas added new feature New feature or request dx Related to developers' experience labels Aug 1, 2023
@joserodolfofreitas joserodolfofreitas transferred this issue from mui/material-ui Aug 7, 2023
@flaviendelangle
Copy link
Member

flaviendelangle commented Mar 20, 2024

We decided not to implement the defaultExpanded at the Tree Item level, because it would create two sources of truth (the defaultExpanded at the Tree View level and the `defaultExpanded at the Tree Item level).
We believe that this would lead to confusion for people (what happens if you use both at the same time?)

However, we will add a new method to our apiRef object to allow people to imperatively expand an item.

Here is how it could look like on the SimpleTreeView (the new name of the TreeView component you were using):

export default function Example() {
  const isLoggedIn = useIsUserLoggedIn();
  const apiRef = useTreeViewApiRef()

  React.useEffect(() => {
    apiRef.current!.setItemExpansion('settings', true);
  }, [isLoggedIn]);

  return (
    <SimpleTreeView apiRef={apiRef}>
      <TreeItem itemId="public" label="Public information" />
      {isLoggedIn && (
        <TreeItem itemId="settings" label="Settings">
          <TreeItem itemId="account" label="My account" />
          <TreeItem itemId="other-settings" label="Other settings" />
        </TreeItem>
      )}
    </SimpleTreeView>
  );
}

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! discussion dx Related to developers' experience new feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants