feat(ui): add ExpandAll, CollapseAll, RevealNode to TreeView (R12, R13)

This commit is contained in:
Joseph Doherty
2026-03-23 02:30:53 -04:00
parent d3a6ed5f68
commit f127efe6ea
2 changed files with 164 additions and 0 deletions

View File

@@ -163,4 +163,95 @@ else
await SelectedKeyChanged.InvokeAsync(key);
}
}
/// <summary>Expand every branch node in the tree.</summary>
public void ExpandAll()
{
if (_items is { Count: > 0 })
{
ExpandAllRecursive(_items);
}
PersistExpandedState();
StateHasChanged();
}
private void ExpandAllRecursive(IReadOnlyList<TItem> items)
{
foreach (var item in items)
{
if (HasChildrenSelector(item))
{
_expandedKeys.Add(KeySelector(item));
}
var children = ChildrenSelector(item);
if (children is { Count: > 0 })
{
ExpandAllRecursive(children);
}
}
}
/// <summary>Collapse every node in the tree.</summary>
public void CollapseAll()
{
_expandedKeys.Clear();
PersistExpandedState();
StateHasChanged();
}
/// <summary>
/// Expand all ancestors of the given key so it becomes visible.
/// Optionally select the node.
/// </summary>
public async Task RevealNode(object key, bool select = false)
{
var parentLookup = BuildParentLookup();
// If key is not in the tree at all, no-op
if (!parentLookup.ContainsKey(key))
return;
// Walk up through ancestors
var current = key;
while (parentLookup.TryGetValue(current, out var parentKey) && parentKey != null)
{
_expandedKeys.Add(parentKey);
current = parentKey;
}
if (select && Selectable)
{
await SelectedKeyChanged.InvokeAsync(key);
}
PersistExpandedState();
StateHasChanged();
}
private Dictionary<object, object?> BuildParentLookup()
{
var lookup = new Dictionary<object, object?>();
if (_items is { Count: > 0 })
{
BuildParentLookupRecursive(_items, null, lookup);
}
return lookup;
}
private void BuildParentLookupRecursive(IReadOnlyList<TItem> items, object? parentKey, Dictionary<object, object?> lookup)
{
foreach (var item in items)
{
var key = KeySelector(item);
lookup[key] = parentKey;
var children = ChildrenSelector(item);
if (children is { Count: > 0 })
{
BuildParentLookupRecursive(children, key, lookup);
}
}
}
}