adobe / react-spectrum

A collection of libraries and tools that help you build adaptive, accessible, and robust user experiences.
https://react-spectrum.adobe.com
Apache License 2.0
12.14k stars 1.06k forks source link

[TreeView] Expose TreeState to TreeItem? #6442

Open nathanforce opened 1 month ago

nathanforce commented 1 month ago

Provide a general summary of the feature here

I was surprised to see that reading UNSTABLE_TreeStateContext from within my TreeItem component does not let me access the tree state. I'm guessing this has something to do with the collection building, but not sure how to best proceed. My ultimate intent is to allow parent checkboxes to toggle all of their children, if that helps.

image

๐Ÿค” Expected Behavior?

const state = useContext(UNSTABLE_TreeStateContext); should return the state, allowing me to leverage the selectionManager.

๐Ÿ˜ฏ Current Behavior

const state = useContext(UNSTABLE_TreeStateContext); returns null

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

Parent checkbox selection

๐Ÿ’ป Examples

No response

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

LFDanLu commented 1 month ago

Can you try accessing the TreeStateContext from within UNSTABLE_TreeItemContent/the checkbox instead? I imagine you should be able to override the onChange for the Checkbox at that point to reach your desired behavior then. However, if you want interactions on the Tree row itself to also follow the same behavior, that might be a bit trickier, might have to drop down to the hooks level to fully customize that behavior

nathanforce commented 1 month ago

It looks like I'm able to access the state via context from within my Checkbox component. Thanks!

Do you know if there is appetite to include this parent selection behavior out of the box? It feels pretty standard for a tree component and might be easier implemented internally.

LFDanLu commented 1 month ago

Yeah, I think supporting that kind of parent selection behavior makes sense, there is work slated for having the parent be in a "indeterminate" state if a subset of its children are selected already so something like that seems like a natural progression. Would probably become an option on the Tree itself. Would you like to modify this issue to reflect this parent selection instead? Would be good to have this publicly tracked as well