Skip to content

feat(Button): add reset OnClick logic#7887

Merged
ArgoZhang merged 2 commits intomainfrom
fix-button
Apr 18, 2026
Merged

feat(Button): add reset OnClick logic#7887
ArgoZhang merged 2 commits intomainfrom
fix-button

Conversation

@ArgoZhang
Copy link
Copy Markdown
Member

@ArgoZhang ArgoZhang commented Apr 18, 2026

Link issues

fixes #7886

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

Reset button click callbacks and unregister async submit buttons on disposal to avoid lingering event handlers and form registrations.

New Features:

  • Add disposal-time reset of button OnClick callbacks to prevent further invocation after component is disposed.

Enhancements:

  • Change ButtonBase OnClick parameter to a parameterless EventCallback for simplified click handling.
  • Automatically unregister async submit buttons from ValidateForm when the button is disposed.
  • Replace explicit lifecycle XML comments with inheritdoc tags for ButtonBase overrides.

Copilot AI review requested due to automatic review settings April 18, 2026 08:10
@bb-auto bb-auto bot added the enhancement New feature or request label Apr 18, 2026
@bb-auto bb-auto bot added this to the v10.5.0 milestone Apr 18, 2026
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Apr 18, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR updates the ButtonBase component’s click handling and lifecycle cleanup: it simplifies the OnClick event signature and ensures click handlers and async submit registration are properly cleared during disposal, while also refactoring some lifecycle XML docs to inherit from the base class.

Sequence diagram for ButtonBase disposal and click handler reset

sequenceDiagram
    participant B as ButtonBase
    participant VF as ValidateForm
    participant T as TooltipService
    participant Base as TooltipWrapperBase

    B->>B: DisposeAsync(disposing=true)
    alt disposing is true
        alt OnClick.HasDelegate is true
            B->>B: OnClick = EventCallback.Empty
        end
        alt IsAsync is true and ValidateForm not null
            B->>VF: UnregisterAsyncSubmitButton(this)
        end
        B->>T: RemoveTooltip()
    end
    B->>Base: base.DisposeAsync(disposing)
    Base-->>B: ValueTask completed
Loading

Class diagram for updated ButtonBase click and dispose behavior

classDiagram
    class TooltipWrapperBase
    class ButtonBase {
        <<abstract>>
        +EventCallback OnClick
        +bool IsAsync
        +ValidateForm ValidateForm
        +override void OnParametersSet()
        +override Task OnAfterRenderAsync(bool firstRender)
        +virtual Task RemoveTooltip()
        +override ValueTask DisposeAsync(bool disposing)
    }

    class ValidateForm {
        +void UnregisterAsyncSubmitButton(ButtonBase button)
    }

    TooltipWrapperBase <|-- ButtonBase
    ValidateForm --> ButtonBase : managesAsyncSubmitButtons

    %% Key change: OnClick no longer generic, cleared in DisposeAsync
    ButtonBase : OnClick = EventCallback.Empty in DisposeAsync when HasDelegate
    ButtonBase : calls ValidateForm.UnregisterAsyncSubmitButton when IsAsync and ValidateForm not null
Loading

File-Level Changes

Change Details Files
Simplify ButtonBase OnClick event signature and add explicit cleanup on dispose.
  • Change OnClick from EventCallback to a parameterless EventCallback to reset click logic and decouple it from MouseEventArgs.
  • Add disposal logic in DisposeAsync to clear the OnClick delegate by setting it to EventCallback.Empty when a handler is present.
  • During disposal, if the button is configured as async and associated with a ValidateForm, unregister it via ValidateForm.UnregisterAsyncSubmitButton(this) before removing the tooltip.
  • Refactor XML documentation on OnParametersSet, OnAfterRenderAsync, and DisposeAsync to use instead of duplicated multilingual comments.
  • Remove the unused Microsoft.AspNetCore.Components.Web using directive since MouseEventArgs is no longer referenced.
src/BootstrapBlazor/Components/Button/ButtonBase.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#7886 Add logic to reset the Button component's OnClick handler when the component is disposed or otherwise cleaned up.

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

@ArgoZhang ArgoZhang merged commit 20e7174 into main Apr 18, 2026
5 of 6 checks passed
@ArgoZhang ArgoZhang deleted the fix-button branch April 18, 2026 08:11
Copy link
Copy Markdown
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 - I've left some high level feedback:

  • Changing OnClick from EventCallback<MouseEventArgs> to parameterless EventCallback is a breaking API change; consider whether this should be introduced via a new parameter (or overload) or at least ensure all consumer usages that rely on MouseEventArgs are intentionally migrated.
  • Resetting the [Parameter] OnClick inside DisposeAsync mutates an incoming parameter, which is unusual in Blazor patterns; you might prefer guarding invocations with a _disposed flag instead of overwriting the parameter to avoid surprising parent components or future maintainers.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Changing `OnClick` from `EventCallback<MouseEventArgs>` to parameterless `EventCallback` is a breaking API change; consider whether this should be introduced via a new parameter (or overload) or at least ensure all consumer usages that rely on `MouseEventArgs` are intentionally migrated.
- Resetting the `[Parameter]` `OnClick` inside `DisposeAsync` mutates an incoming parameter, which is unusual in Blazor patterns; you might prefer guarding invocations with a `_disposed` flag instead of overwriting the parameter to avoid surprising parent components or future maintainers.

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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the ButtonBase component’s click callback API and adds disposal-time cleanup intended to “reset” click handling.

Changes:

  • Changes ButtonBase.OnClick from EventCallback<MouseEventArgs> to non-generic EventCallback.
  • Adds disposal logic to clear OnClick and keeps async-submit-button unregister behavior.
  • Replaces several method XML docs with <inheritdoc/>.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// </summary>
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }
public EventCallback OnClick { get; set; }
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

Changing OnClick from EventCallback<MouseEventArgs> to non-generic EventCallback is a breaking API change and will also break existing usages inside this repo that bind parameterized handlers (e.g., OnClick="@(e => ...)" in src/BootstrapBlazor.Server/Components/Components/WinButton.razor:1 and tests like test/UnitTest/Components/ButtonTest.cs:163). If the intent is to keep supporting click handlers that accept MouseEventArgs, keep the generic type and (ideally) pass the actual MouseEventArgs through from the @onclick handler; otherwise this needs a compatibility path (e.g., add a new parameter and obsolete the old one).

Suggested change
public EventCallback OnClick { get; set; }
public EventCallback<MouseEventArgs> OnClick { get; set; }

Copilot uses AI. Check for mistakes.
Comment on lines +335 to +343
if (OnClick.HasDelegate)
{
OnClick = EventCallback.Empty;
}

if (IsAsync && ValidateForm != null)
{
ValidateForm.UnregisterAsyncSubmitButton(this);
}
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

DisposeAsync is mutating a [Parameter] (OnClick = EventCallback.Empty). This is unconventional and easy to miss, and if the goal is to break references for GC it should be done consistently (e.g., clear OnClickWithoutRender as well) and without changing the public API shape. Consider removing this assignment entirely, or at least clearing the callback(s) using default for the existing callback type rather than introducing a new EventCallback type.

Copilot uses AI. Check for mistakes.
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(Button): add reset OnClick logic

2 participants