I’ve promised to present an improved version of the
OutlinePicker shown in this post and here it is. What did I do?
- The parameter list changed a little bit. Now there’s a special title for „No Selection“ and we do not need the
isExpanded parameter anymore since we want to expand the nodes automatically according to the selection.
- I’ve introduced the new array
nodesToExpand. I’ll come back to that a little bit later.
- Instead of a sheet the selection view is presented as a popover (
WithPopover from this post). Using that we do not need a close button anymore.
- The NodeOutlineGroup from this post is used to accept the array of expanded nodes.
- Instead of a
Form I use a
List here with a plain
listStyle. That looks better in the popover view.
- The „No Selection“ section is now separated from the list in a similar manner as a
Divider would do it in a
This is the second post of my little series about a fine-tuned version of my
OutlinePicker. Today, I’d like to propose an improved version of the already presented
NodeOutlineGroup. As stated here the
NodeOutlineGroup gives you control whether the disclosure group should be collapsed or expanded on show up of the outline group. I want to use that for my hierarchical picker. But on start of my picker the pre-selected node might be deep in the tree instead of being one of the root nodes.
So, if I set up the picker with all nodes collapsed the pre-selected node would not be visible immediately and the user might search for it for quite a long time. On the other hand, if I expand all nodes it might be quite a big tree to display on startup. The ideal solution would be that all nodes are collapsed except to those ones which have the selected node as a child or grand child.
If you want to display an outline in SwiftUI you may use the framework supplied
OutlineGroup construct. That’s based on a nested tree of
DisclosureGroup and is quite nice. But on appearance of the
OutlineGroup all outlines are collapsed. Well, in one of my projects I wanted to display the outline all expanded on show up. No way to do that with
OutlineGroup as far as I know. But then I found this nice variant (https://stackoverflow.com/questions/62832809/list-or-outlinegroup-expanded-by-default-in-swiftui) with a control of expansion: