Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>10.7.3-beta03</Version>
<Version>10.7.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
26 changes: 16 additions & 10 deletions src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -740,8 +740,11 @@ public void SetItems(List<TreeViewItem<TItem>> items)
/// </summary>
public void SetActiveItem(TItem item)
{
var val = Items.GetAllItems().FirstOrDefault(i => Equals(i.Value, item));
SetActiveItem(val);
if (Items != null)
{
var val = Items.GetAllItems().FirstOrDefault(i => Equals(i.Value, item));
SetActiveItem(val);
}
}

private static CheckboxState ToggleCheckState(CheckboxState state) => state switch
Expand Down Expand Up @@ -837,17 +840,20 @@ private async Task OnCheckStateChanged(TreeViewItem<TItem> item, CheckboxState s
/// </summary>
public void ClearCheckedItems()
{
Items.ForEach(item =>
if (Items != null)
{
item.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(item);
item.GetAllTreeSubItems().ToList().ForEach(s =>
Items.ForEach(item =>
{
s.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(s);
item.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(item);
item.GetAllTreeSubItems().ToList().ForEach(s =>
{
s.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(s);
});
});
Comment on lines +845 to 854

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Avoid the ToList() allocation and use foreach for sub-items iteration.

item.GetAllTreeSubItems().ToList().ForEach(...) creates an unnecessary list just to use List<T>.ForEach. Iterate directly over GetAllTreeSubItems() with foreach (e.g., foreach (var s in item.GetAllTreeSubItems()) { ... }) to avoid extra allocations and keep the code clearer.

Suggested change
Items.ForEach(item =>
{
s.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(s);
item.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(item);
item.GetAllTreeSubItems().ToList().ForEach(s =>
{
s.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(s);
});
});
Items.ForEach(item =>
{
item.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(item);
foreach (var s in item.GetAllTreeSubItems())
{
s.CheckedState = CheckboxState.UnChecked;
_treeNodeStateCache.ToggleCheck(s);
}
});

});
StateHasChanged();
StateHasChanged();
}
}

/// <summary>
Expand Down
Loading