Skip to content

Commit

Permalink
πŸ†• feat(Treeview): open parent nodes of filtered node after filtering (#…
Browse files Browse the repository at this point in the history
…514)

* πŸ†• feat(Treeview): open parent nodes of filtered node after filtering

* Update BComponentBase.cs
  • Loading branch information
capdiem authored Nov 27, 2023
1 parent 3bffd16 commit 148e381
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ protected async Task PreventRenderingUtil(params Func<Task>[] funcs)
}

/// <summary>
/// Run a task in microseconds.
/// Debounce a task in microseconds.
/// </summary>
/// <param name="task">A task to run.</param>
/// <param name="millisecondsDelay">Delay in milliseconds.</param>
/// <param name="cancellationToken">A cancellation token to cancel the task.</param>
protected static async Task RunTaskInMicrosecondsAsync(Func<Task> task, int millisecondsDelay, CancellationToken cancellationToken = default)
/// <param name="cancellationToken">A cancellation token to cancel the last task.</param>
protected static async Task RunTaskInMicrosecondsAsync(Func<Task> task, int millisecondsDelay, CancellationToken cancellationToken)
{
try
{
Expand All @@ -145,12 +145,12 @@ protected static async Task RunTaskInMicrosecondsAsync(Func<Task> task, int mill
}

/// <summary>
/// Run a task in microseconds.
/// Debounce a task in microseconds.
/// </summary>
/// <param name="task">A task to run.</param>
/// <param name="millisecondsDelay">Delay in milliseconds.</param>
/// <param name="cancellationToken">A cancellation token to cancel the task.</param>
protected static async Task RunTaskInMicrosecondsAsync(Action task, int millisecondsDelay, CancellationToken cancellationToken = default)
/// <param name="cancellationToken">A cancellation token to cancel the last task.</param>
protected static async Task RunTaskInMicrosecondsAsync(Action task, int millisecondsDelay, CancellationToken cancellationToken)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public partial class BTreeview<TItem, TKey> : ITreeview<TItem, TKey> where TKey
private List<TKey> _oldValue = new();
private List<TKey> _oldActive = new();
private List<TKey> _oldOpen = new();
private string? _prevSearch;
private CancellationTokenSource? _searchUpdateCts;

[Parameter, EditorRequired]
public List<TItem> Items { get; set; } = null!;
Expand Down Expand Up @@ -395,6 +397,11 @@ private bool FilterTreeItems(TItem item, ref List<TKey> excluded)
{
if (FilterTreeItem(item, Search, ItemText))
{
if (Nodes.TryGetValue(ItemKey(item), out var nodeState) && nodeState.Parent != null)
{
UpdateOpen(nodeState.Parent, true);
}

return true;
}

Expand All @@ -408,6 +415,11 @@ private bool FilterTreeItems(TItem item, ref List<TKey> excluded)
{
if (FilterTreeItems(child, ref excluded))
{
if (Nodes.TryGetValue(ItemKey(child), out var nodeState) && nodeState.Parent != null)
{
UpdateOpen(nodeState.Parent, true);
}

match = true;
}
}
Expand Down Expand Up @@ -466,7 +478,7 @@ private string CombineItemKeys(IList<TItem> list)

return keys;
}

protected override async Task OnParametersSetAsync()
{
await base.OnParametersSetAsync();
Expand Down Expand Up @@ -509,6 +521,15 @@ protected override async Task OnParametersSetAsync()
await HandleUpdate(_oldOpen, Open, UpdateOpen, EmitOpenAsync);
_oldOpen = open;
}

if (_prevSearch != Search)
{
_prevSearch = Search;

_searchUpdateCts?.Cancel();
_searchUpdateCts = new();
await RunTaskInMicrosecondsAsync(EmitOpenAsync, 300, _searchUpdateCts.Token);
}
}

protected override void OnAfterRender(bool firstRender)
Expand Down

0 comments on commit 148e381

Please sign in to comment.