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

[tree view] Allow callback form of useRef #14267

Open
joshkel opened this issue Aug 20, 2024 · 3 comments
Open

[tree view] Allow callback form of useRef #14267

joshkel opened this issue Aug 20, 2024 · 3 comments
Labels
component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! new feature New feature or request typescript

Comments

@joshkel
Copy link
Contributor

joshkel commented Aug 20, 2024

Summary

The apiRef for SimpleTreeView currently must be a MutableRefObject. If it supported any React ref - a callback instead of a mutable object - it would be more consistent with React and more flexible for developers.

Examples

const callback = (api: TreeViewPublicAPI) => { /* ... */ };
return <SimpleTreeView apiRef={ref}>{items}</SimpleTreeView>;

Motivation

Our component renders several smaller tree views, separated by subheaders. It would be helpful to have access to all of the tree views' APIs from the rendering component. A callback ref can be used to maintain a list, but there's no obvious way to make this work with a MutableRefObject.

See also #6234, which requests the same functionality for the data grid.

Search keywords: useRef

@joshkel joshkel added new feature New feature or request status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 20, 2024
@michelengelen michelengelen changed the title Allow callback form of useRef [tree view] Allow callback form of useRef Aug 20, 2024
@michelengelen
Copy link
Member

This seems like a viable proposal.
@flaviendelangle should we add this to the board, or add waiting for 👍🏼 for now?

@michelengelen michelengelen added typescript component: tree view TreeView, TreeItem. This is the name of the generic UI component, not the React module! labels Aug 20, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Aug 20, 2024

This looks strange:

Why is it not using https://react.dev/reference/react/useImperativeHandle? I did a fix back in the days: #990.

What the data grid does seems better:

React.useImperativeHandle(inputApiRef, () => publicApiRef.current, [publicApiRef]);

@michelengelen michelengelen removed the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Aug 21, 2024
@flaviendelangle
Copy link
Member

@flaviendelangle should we add this to the board, or add waiting for 👍🏼 for now?

I would wait to see how much traction we have yes.
I'm curious because I don't recall anyone asking for it on the grid, do they support it?

@oliviertassinari we could indeed align with the initialization logic of the grid

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! new feature New feature or request typescript
Projects
None yet
Development

No branches or pull requests

4 participants