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

MudBaseButton: Stop activation of IActivatable components when ClickPropagation is disabled #9626

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
MudBaseButton: Stop activation of IActivatable components when
`ClickPropagation` is disabled
  • Loading branch information
igotinfected committed Aug 13, 2024
commit 5693231a76efd5db86f4e09d3d0a04e66b38ac6f
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<DummyActivatable>
<MudFab id="fab-button"
ClickPropagation="@ClickPropagation" />
<MudButton id="button-button"
ClickPropagation="@ClickPropagation" />
<MudIconButton id="icon-button"
Icon="Icon.Material.Filled.Add" ClickPropagation="@ClickPropagation" />
</DummyActivatable>

@code {
[Parameter]
public bool ClickPropagation { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@using MudBlazor.Interfaces
@namespace MudBlazor.UnitTests

<CascadingValue Value="@((IActivatable)this)"
IsFixed="true">
@ChildContent
</CascadingValue>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MudBlazor 2021
// MudBlazor licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using MudBlazor.Interfaces;

namespace MudBlazor.UnitTests;

public partial class DummyActivatable : ComponentBase, IActivatable
{
#nullable enable
[Parameter]
public RenderFragment? ChildContent { get; set; }

public int ActivationCount { get; private set; }

public void Activate(object activator, MouseEventArgs args) => ActivationCount++;
}
36 changes: 35 additions & 1 deletion src/MudBlazor.UnitTests/Components/ButtonsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void MudButtonShouldRenderAnAnchorIfLinkIsSetAndIsNotDisabled()
comp.Markup
.Should()
.Contain("rel=\"noopener\"");
//it is an anchor and not contains stopPropagation
//it is an anchor and not contains stopPropagation
comp.Markup
.Replace(" ", string.Empty)
.Should()
Expand Down Expand Up @@ -480,5 +480,39 @@ public async Task ButtonsOnClickErrorContentCaughtException()
await comp.InvokeAsync(comp.Instance.Recover);
alertTextFunc.Should().Throw<ComponentNotFoundException>();
}

[Test]
public void Buttons_should_activate_parent_activatables_when_click_propagation_is_enabled()
{
var comp = Context.RenderComponent<ButtonActivationTest>(parameters => parameters
.Add(param => param.ClickPropagation, true));
var dummyActivatable = comp.FindComponent<DummyActivatable>();

comp.Find("#fab-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(1);

comp.Find("#button-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(2);

comp.Find("#icon-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(3);
}

[Test]
public void Buttons_should_not_activate_parent_activatables_when_click_propagation_is_disabled()
{
var comp = Context.RenderComponent<ButtonActivationTest>(parameters => parameters
.Add(param => param.ClickPropagation, false));
var dummyActivatable = comp.FindComponent<DummyActivatable>();

comp.Find("#fab-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(0);

comp.Find("#button-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(0);

comp.Find("#icon-button").Click();
dummyActivatable.Instance.ActivationCount.Should().Be(0);
}
}
}
7 changes: 6 additions & 1 deletion src/MudBlazor/Base/MudBaseButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,12 @@ protected virtual async Task OnClickHandler(MouseEventArgs ev)
if (GetDisabledState())
return;
await OnClick.InvokeAsync(ev);
Activatable?.Activate(this, ev);

// Don't activate activatable parents if click propagation is disabled
if (ClickPropagation)
{
Activatable?.Activate(this, ev);
}
}

protected override void OnInitialized()
Expand Down
Loading