Skip to content

Commit d819a17

Browse files
vnbaaijdvoituron
andauthored
[Button] Add StopPropagation + UnitTests (#2732)
Co-authored-by: Denis Voituron <[email protected]>
1 parent 07cab03 commit d819a17

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

examples/Demo/Shared/Microsoft.FluentUI.AspNetCore.Components.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,11 @@
926926
The text usually displayed in a 'tooltip' popup when the mouse is over the button.
927927
</summary>
928928
</member>
929+
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentButton.StopPropagation">
930+
<summary>
931+
Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases.
932+
</summary>
933+
</member>
929934
<member name="P:Microsoft.FluentUI.AspNetCore.Components.FluentButton.ChildContent">
930935
<summary>
931936
Gets or sets the content to be rendered inside the component.

src/Core/Components/Button/FluentButton.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ else
4040
title=@Title
4141
appearance=@Appearance.ToAttributeValue()
4242
@onclick="@OnClickHandlerAsync"
43+
@onclick:stopPropagation="@StopPropagation"
4344
@attributes="AdditionalAttributes">
4445
@if (IconStart != null)
4546
{

src/Core/Components/Button/FluentButton.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ public partial class FluentButton : FluentComponentBase, IAsyncDisposable
151151
[Parameter]
152152
public string? Title { get; set; }
153153

154+
/// <summary>
155+
/// Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases.
156+
/// </summary>
157+
[Parameter]
158+
public bool StopPropagation { get; set; } = false;
159+
154160
/// <summary>
155161
/// Gets or sets the content to be rendered inside the component.
156162
/// </summary>

tests/Core/Button/FluentButtonTests.razor

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,47 @@
5858
// Assert
5959
Assert.True(clicked);
6060
}
61+
62+
[Fact]
63+
public void FluentButton_StopPropagationFalse()
64+
{
65+
bool clickedondiv = false;
66+
bool clicked = false;
67+
68+
// Arrange
69+
// Not adding StopPropagation here explicitly because it is false by default
70+
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
71+
<FluentButton OnClick="@(e => { clicked = true; } )">
72+
My button
73+
</FluentButton>
74+
</div>);
75+
76+
// Act
77+
cut.Find("fluent-button").Click();
78+
79+
// Assert
80+
Assert.True(clickedondiv);
81+
Assert.True(clicked);
82+
}
83+
84+
[Fact]
85+
public void FluentButton_StopPropagationTrue()
86+
{
87+
bool clickedondiv = false;
88+
bool clicked = false;
89+
90+
// Arrange
91+
var cut = Render(@<div @onclick="@(e => {clickedondiv = true; })">
92+
<FluentButton StopPropagation="true" OnClick="@(e => { clicked = true; } )">
93+
My button
94+
</FluentButton>
95+
</div>);
96+
97+
// Act
98+
cut.Find("fluent-button").Click();
99+
100+
// Assert
101+
Assert.False(clickedondiv);
102+
Assert.True(clicked);
103+
}
61104
}

0 commit comments

Comments
 (0)