[Feature Request] Create `TreeViewController`
Discussed in https://github.com/bdlukaa/fluent_ui/discussions/1085
Originally posted by pamtbaau July 15, 2024 I've two apps that use a TreeView. In both apps the TreeView needs to be dynamically updated by:
- Adding/removing TreeViewItems
- Replacing the value of a TreeViewItem (values are immutable objects)
- Updating the content of the TreeViewItem depending on the replaced value.
I've tried two approaches:
- Updating the underlaying tree of values (to-do's, or financial accounts/categories) and then rebuild the entire tree.
Issues
- Requires storing and maintaining which TreeViewItem is
selectedand/orexpandedoutside the TreeView. - Requires recreating entire tree after each insert/delete/update of underlaying values and apply selected/expanded properties.
- Requires storing and maintaining which TreeViewItem is
- Adding/removing TreeviewItems to/from the
childrenproperty of the parent. Issues- The
contentandvalueof a TreeViewItem are final and cannot be updated. This requires creating a new TreeViewItem and replacing the outdated one.
- The
Both approaches feel awkward...
Questions:
- Is there something I've overlooked and can help in updating a tree dynamically?
- Any other approach I could try?
Current Solution
The current only way to dynamically update a TreeView is by making it lazy and providing the items outside of it. When the items are loaded, just update the variable that holds that data and setState.
Expected Solution
To give more finer control to the tree view, a TreeViewController should be created. It would hold information about the attached TreeView. Additionally, it would give much more breath to the tree view widget itself, moving the logic to a contained place.
With the controller, one could:
- Look for items (
controller.getItemFromValue('item_value')); - Add items (
controller.appendItem( ... )); - Remove items (
controller.removeItem( ... ));
Additional Context
- https://bdlukaa.github.io/fluent_ui/#/navigation/tree_view
- https://learn.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.controls.treeview
- https://learn.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.controls.treeviewitem
I had the same thought. In my case, I want the TreeViewItem to expand when clicked, but I found the implementation to be quite cumbersome. I believe there should be a TreeViewController that could help us manage TreeView updates more efficiently.