Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Button] Add StopPropagation + UnitTests #2732

Merged
merged 3 commits into from
Sep 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,11 @@
The text usually displayed in a 'tooltip' popup when the mouse is over the button.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentButton.StopPropagation">
<summary>
Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases.
</summary>
</member>
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentButton.ChildContent">
<summary>
Gets or sets the content to be rendered inside the component.
Expand Down
1 change: 1 addition & 0 deletions src/Core/Components/Button/FluentButton.razor
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ else
title=@Title
appearance=@Appearance.ToAttributeValue()
@onclick="@OnClickHandlerAsync"
@onclick:stopPropagation="@StopPropagation"
@attributes="AdditionalAttributes">
@if (IconStart != null)
{
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Components/Button/FluentButton.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ public partial class FluentButton : FluentComponentBase, IAsyncDisposable
[Parameter]
public string? Title { get; set; }

/// <summary>
/// Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases.
/// </summary>
[Parameter]
public bool StopPropagation { get; set; } = false;

/// <summary>
/// Gets or sets the content to be rendered inside the component.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions tests/Core/Button/FluentButtonTests.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,47 @@
// Assert
Assert.True(clicked);
}

[Fact]
public void FluentButton_StopPropagationFalse()
{
bool clickedondiv = false;
bool clicked = false;

// Arrange
// Not adding StopPropagation here explicitly because it is false by default
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
<FluentButton OnClick="@(e => { clicked = true; } )">
My button
</FluentButton>
</div>);

// Act
cut.Find("fluent-button").Click();

// Assert
Assert.True(clickedondiv);
Assert.True(clicked);
}

[Fact]
public void FluentButton_StopPropagationTrue()
{
bool clickedondiv = false;
bool clicked = false;

// Arrange
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
<FluentButton StopPropagation="true" OnClick="@(e => { clicked = true; } )">
My button
</FluentButton>
</div>);

// Act
cut.Find("fluent-button").Click();

// Assert
Assert.False(clickedondiv);
Assert.True(clicked);
}
}
Loading