Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Aug 4, 2025

Link issues

fixes #6544

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

Implement a new ToggleButton component with associated demo and unit tests, and introduce a Table sortable test skeleton

New Features:

  • Add ToggleButton component with state toggling, async callback support, and reactive CSS classes

Enhancements:

  • Include ToggleButton demo in the sample Buttons page
  • Add a Table sortable behavior test skeleton

Tests:

  • Add unit tests for ToggleButton interactions
  • Add Table_Sortable test in TableTest.cs

@bb-auto bb-auto bot added the enhancement New feature or request label Aug 4, 2025
@bb-auto bb-auto bot added this to the 9.9.0 milestone Aug 4, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 4, 2025

Reviewer's Guide

Adds a new ToggleButton component with active/inactive state management, asynchronous and event callbacks, complete with unit tests and a server sample demo, and includes a sortable Table unit test.

Sequence diagram for ToggleButton click and state change

sequenceDiagram
    actor User
    participant ToggleButton
    User->>ToggleButton: Clicks button
    ToggleButton->>ToggleButton: OnClickButton()
    ToggleButton->>ToggleButton: HandlerClick()
    ToggleButton->>ToggleButton: Toggle IsActive
    alt IsActiveChanged set
        ToggleButton->>IsActiveChanged: InvokeAsync(IsActive)
    end
    alt OnToggleAsync set
        ToggleButton->>OnToggleAsync: Invoke(IsActive)
    end
Loading

Class diagram for the new ToggleButton component

classDiagram
    class ToggleButton {
        +Func<bool, Task>? OnToggleAsync
        +bool IsActive
        +EventCallback<bool> IsActiveChanged
        -string? ToggleClassName
        -Task OnClickButton()
        -Task HandlerClick()
    }
    ToggleButton --|> ButtonBase
Loading

File-Level Changes

Change Details Files
Implement ToggleButton component
  • Define Razor markup with active CSS class, loading icon, and child content cascading
  • Add parameters IsActive, IsActiveChanged, OnToggleAsync and manage click flow with async loading, disable logic, and callback invocations
src/BootstrapBlazor/Components/Button/ToggleButton.razor
src/BootstrapBlazor/Components/Button/ToggleButton.razor.cs
Add unit tests for ToggleButton and sortable Table
  • Add ToogleButton_Ok test verifying click toggles state and triggers all callbacks
  • Add Table_Sortable test validating cascaded ISortableList on Table component
test/UnitTest/Components/ButtonTest.cs
test/UnitTest/Components/TableTest.cs
Add ToggleButton demo in server samples
  • Insert DemoBlock for ToggleButton in Buttons.razor with title, introduction, and sample usage
src/BootstrapBlazor.Server/Components/Samples/Buttons.razor

Assessment against linked issues

Issue Objective Addressed Explanation
#6544 Implement a ToggleButton component in the codebase.

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

sourcery-ai[bot]
sourcery-ai bot previously approved these changes Aug 4, 2025
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:

  • The Table_Sortable test has no assertions; please validate sortable behavior to ensure the feature works as expected.
  • The ToggleButton test method 'ToogleButton_Ok' has a typo in its name and does not assert the IsActiveChanged callback flag; please rename it and add the missing assertion.
  • Consider adding tests to verify the ToggleButton's visual state (e.g., CSS 'active' class) and toggling back to inactive to cover full toggle behavior.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The Table_Sortable test has no assertions; please validate sortable behavior to ensure the feature works as expected.
- The ToggleButton test method 'ToogleButton_Ok' has a typo in its name and does not assert the IsActiveChanged callback flag; please rename it and add the missing assertion.
- Consider adding tests to verify the ToggleButton's visual state (e.g., CSS 'active' class) and toggling back to inactive to cover full toggle behavior.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Components/Button/ToggleButton.razor.cs:35` </location>
<code_context>
+        .AddClass("active", IsActive)
+        .Build();
+
+    private async Task OnClickButton()
+    {
+        if (IsAsync)
</code_context>

<issue_to_address>
Consider refactoring by merging the two methods, extracting repeated loading/disabled logic, and consolidating callback invocations into helpers.

Here’s one way to collapse both methods into a single, linear flow, pull‐out the repeated “loading/disabled” toggle into its own helper, and share the callback invocation logic:

```csharp
private async Task OnClickButton()
{
    var nextState = !IsActive;
    if (IsAsync)
        SetLoading(true);

    IsActive = nextState;

    await InvokeAllCallbacks(nextState);

    if (IsAsync)
        SetLoading(false);
}

private void SetLoading(bool isLoading)
{
    IsAsyncLoading = isLoading;
    IsDisabled      = isLoading
                      ? true
                      : IsKeepDisabled;
    // optionally StateHasChanged() here if you need an immediate re-render
}

private async Task InvokeAllCallbacks(bool newState)
{
    if (OnClickWithoutRender != null)
    {
        if (!IsAsync) 
            IsNotRender = true;
        await OnClickWithoutRender();
    }

    if (OnClick.HasDelegate)
        await OnClick.InvokeAsync();

    if (IsActiveChanged.HasDelegate)
        await IsActiveChanged.InvokeAsync(newState);

    if (OnToggleAsync != null)
        await OnToggleAsync(newState);
}
```

What this does:

1. Merges `OnClickButton` + `HandlerClick` into one clear sequence.  
2. Extracts the “loading/disabled” block into `SetLoading(bool)`.  
3. Extracts all callback invocations into `InvokeAllCallbacks(bool)`.  

You retain the exact same state transitions and callback order, but with much less nesting or duplication.
</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.

@ArgoZhang ArgoZhang merged commit a65e246 into main Aug 4, 2025
4 checks passed
@ArgoZhang ArgoZhang deleted the feat-button-toggle branch August 4, 2025 03:37
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(ToggleButton): add ToggleButton component

2 participants