Skip to content

feat(TreeView): add Items check#8170

Merged
ArgoZhang merged 2 commits into
mainfrom
feat-tv
Jun 29, 2026
Merged

feat(TreeView): add Items check#8170
ArgoZhang merged 2 commits into
mainfrom
feat-tv

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Jun 29, 2026

Copy link
Copy Markdown
Member

Link issues

fixes #8169

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Add null-safety checks around TreeView Items usage to prevent errors when no items are set.

Bug Fixes:

  • Avoid potential null reference errors when setting the active item in TreeView without initialized Items.
  • Avoid potential null reference errors when clearing checked items in TreeView when Items is null.

@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 29, 2026
@bb-auto bb-auto Bot added this to the v10.7.0 milestone Jun 29, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

TreeView APIs that operate on Items are now null-safe by guarding against Items being null before accessing or mutating them, preventing potential null reference exceptions when no Items collection is set.

Sequence diagram for TreeView.SetActiveItem null-safe behavior

sequenceDiagram
    participant Caller
    participant TreeView
    participant Items

    Caller->>TreeView: SetActiveItem(item)
    alt Items is not null
        TreeView->>Items: GetAllItems()
        Items-->>TreeView: IEnumerable<TreeViewItem<TItem>>
        TreeView->>TreeView: SetActiveItem(val)
    else Items is null
        TreeView->>TreeView: [no-op]
    end
Loading

Sequence diagram for TreeView.ClearCheckedItems null-safe behavior

sequenceDiagram
    participant Caller
    participant TreeView
    participant Items
    participant TreeNodeStateCache

    Caller->>TreeView: ClearCheckedItems()
    alt Items is not null
        TreeView->>Items: ForEach(item)
        loop each item and subitem
            TreeView->>item: CheckedState = UnChecked
            TreeView->>TreeNodeStateCache: ToggleCheck(item)
            TreeView->>item: GetAllTreeSubItems()
            TreeView->>TreeNodeStateCache: ToggleCheck(subitem)
        end
        TreeView->>TreeView: StateHasChanged()
    else Items is null
        TreeView->>TreeView: [no-op]
    end
Loading

File-Level Changes

Change Details Files
Make SetActiveItem null-safe when Items is not initialized.
  • Add a null check for the Items collection before calling GetAllItems() and SetActiveItem on the found node
src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs
Make ClearCheckedItems null-safe and scope state updates inside the null check.
  • Guard the Items.ForEach loop and subtree iteration with a null check on Items
  • Move StateHasChanged invocation inside the Items null-check block so it only runs when changes were applied
src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8169 Add a safety/null check for the TreeView Items collection in methods that operate on Items (to avoid errors when Items is null).

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Auto approved by bb-auto

@sourcery-ai sourcery-ai Bot left a comment

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.

Hey - I've found 1 issue, and left some high level feedback:

  • In both SetActiveItem and ClearCheckedItems, consider using an early return when Items is null to reduce nesting and keep the methods’ main logic at the top level.
  • Review whether ClearCheckedItems should still call StateHasChanged when Items is null, as the current guard prevents UI updates even if other state might have changed.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In both SetActiveItem and ClearCheckedItems, consider using an early return when Items is null to reduce nesting and keep the methods’ main logic at the top level.
- Review whether ClearCheckedItems should still call StateHasChanged when Items is null, as the current guard prevents UI updates even if other state might have changed.

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs" line_range="845-854" />
<code_context>
         {
-            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);
</code_context>
<issue_to_address>
**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.

```suggestion
            Items.ForEach(item =>
            {
                item.CheckedState = CheckboxState.UnChecked;
                _treeNodeStateCache.ToggleCheck(item);

                foreach (var s in item.GetAllTreeSubItems())
                {
                    s.CheckedState = CheckboxState.UnChecked;
                    _treeNodeStateCache.ToggleCheck(s);
                }
            });
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +845 to 854
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);
});
});

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);
}
});

@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.79%. Comparing base (c3e9449) to head (20d1e3d).

Files with missing lines Patch % Lines
...tstrapBlazor/Components/TreeView/TreeView.razor.cs 87.50% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8170      +/-   ##
==========================================
- Coverage   98.79%   98.79%   -0.01%     
==========================================
  Files         766      766              
  Lines       34224    34230       +6     
  Branches     4699     4701       +2     
==========================================
+ Hits        33811    33816       +5     
- Partials      413      414       +1     
Flag Coverage Δ
BB 98.79% <87.50%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang merged commit 68ff235 into main Jun 29, 2026
6 of 7 checks passed
@ArgoZhang ArgoZhang deleted the feat-tv branch June 29, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(TreeView): add Items check

1 participant