Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Jun 24, 2025

Link issues

fixes #6271
fixes #6293

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

Prevent tab items from re-rendering on close by stabilizing component keys and isolating content in wrapper divs.

Bug Fixes:

  • Fix unnecessary rerender of TabItemContent after closing a tab.

Enhancements:

  • Use Guid-based keys to control TabItemContent updates.
  • Wrap each tab’s content in a div with id and dynamic classes to toggle visibility instead of rebuilding the component.
  • Extract GetTabItemClassString helper to centralize CSS class logic.
  • Replace LINQ FirstOrDefault with List.Find for consistency in locating the active tab.

@bb-auto bb-auto bot added the bug Something isn't working label Jun 24, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Jun 24, 2025

Reviewer's Guide

This PR prevents unintended rerendering of TabItems upon closing by stabilizing component keys, wrapping each TabItem in its own container with explicit keys and class names, and streamlining CSS and lookup logic.

Class diagram for updated Tab and TabItemContent components

classDiagram
    class Tab {
        +List<TabItem> TabItems
        +RenderTabItem(TabItem item)
        +static GetTabItemClassString(TabItem item) string
    }
    class TabItemContent {
        -Guid _key
        +Render()
        -RenderContent()
        -BuildRenderTree(RenderTreeBuilder builder)
        -RenderItemContent(RenderFragment? content)
    }
    Tab --> TabItemContent : uses
    TabItemContent --> TabItem : uses
    class TabItem {
        +string Id
        +bool IsActive
        +RenderFragment? ChildContent
    }
Loading

Flow diagram for TabItem rendering and rerender prevention

flowchart TD
    A[TabItems collection] -->|Find active| B[TabItem]
    B --> C{IsActive?}
    C -- Yes --> D[Wrap in div with key and class]
    D --> E[RenderTabItem]
    C -- No --> F[Skip or render with d-none class]
    E --> G[TabItemContent]
    G --> H[Render content with stable key]
    H --> I[Display TabItem content]
    F --> J[Hidden TabItem]
Loading

File-Level Changes

Change Details Files
Stabilize TabItemContent identity and simplify its CSS assignment
  • Switched _key type from object to Guid and regenerate on each Render
  • Removed dynamic ClassString property and CssBuilder dependency
  • Hard-coded class attribute to "tabs-body-content-wrap" for consistent styling
src/BootstrapBlazor/Components/Tab/TabItemContent.cs
Wrap TabItems in dedicated containers with explicit key, id, and class
  • Replaced direct @RenderTabItem calls with a
    wrapper carrying id and class
  • Added @key on each wrapper to maintain stable rendering across operations
  • Unified markup for active and inactive items under consistent structure
src/BootstrapBlazor/Components/Tab/Tab.razor
Extract and reuse CSS logic and refine active‐item lookup
  • Introduced GetTabItemClassString helper using CssBuilder for class concatenation
  • Replaced inline class determination with the new helper in markup
  • Changed FirstOrDefault to Find when locating the active TabItem in OnRefreshAsync
src/BootstrapBlazor/Components/Tab/Tab.razor.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#6293 Prevent TabItem components from re-rendering after they are closed.

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 added this to the 9.7.0 milestone Jun 24, 2025
@ArgoZhang ArgoZhang merged commit 72186d3 into main Jun 24, 2025
2 of 3 checks passed
@ArgoZhang ArgoZhang deleted the refactor-tab branch June 24, 2025 10:13
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

  • Regenerating a new Guid for _key on every Render() will remount the content each cycle—if you only want to force a remount on close, consider updating _key only when Item.IsActive actually changes.
  • You’re using both @key="@item" on the wrapper and builder.SetKey(_key) inside TabItemContent; choosing a single stable key (for example, item.Id) will simplify reconciliation and make the intent clearer.
  • GetTabItemClassString is declared to return string? but always returns a non-null string—removing the nullable annotation will better reflect its actual behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Regenerating a new Guid for `_key` on every `Render()` will remount the content each cycle—if you only want to force a remount on close, consider updating `_key` only when `Item.IsActive` actually changes.
- You’re using both `@key="@item"` on the wrapper and `builder.SetKey(_key)` inside `TabItemContent`; choosing a single stable key (for example, `item.Id`) will simplify reconciliation and make the intent clearer.
- `GetTabItemClassString` is declared to return `string?` but always returns a non-null string—removing the nullable annotation will better reflect its actual behavior.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Components/Tab/Tab.razor:36` </location>
<code_context>
                     foreach (var item in TabItems)
                     {
-                        @RenderTabItem(item)
+                        <div @key="@item" id="@item.Id" class="@GetTabItemClassString(item)">
+                            @RenderTabItem(item)
+                        </div>
</code_context>

<issue_to_address>
Adding @key to the div may affect Blazor's diffing behavior.

If item is a complex object, property changes may not trigger updates as expected. Using a unique, stable value like item.Id as the key is recommended.
</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.

foreach (var item in TabItems)
{
@RenderTabItem(item)
<div @key="@item" id="@item.Id" class="@GetTabItemClassString(item)">
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: Adding @key to the div may affect Blazor's diffing behavior.

If item is a complex object, property changes may not trigger updates as expected. Using a unique, stable value like item.Id as the key is recommended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(Tab): should not rerender tabitem after close bug(Tab): TabItem Content Glide

2 participants