Skip to content

Commit

Permalink
MudMenu: Add IsOpenChanged event (#7463)
Browse files Browse the repository at this point in the history
* MudMenu added OnOpenedChanged (#7462)

* MudMenu: renamed IsOpen changed event

* MudMenu: added IsOpenChanged tests
  • Loading branch information
ronimizy authored Sep 6, 2023
1 parent 0652cb6 commit b3be3ce
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@namespace MudBlazor.UnitTests.TestComponents

<MudPopoverProvider></MudPopoverProvider>

<MudMenu
@ref="Menu"
IsOpenChanged="@(o => _ = o ? TrueInvocationCount++ : FalseInvocationCount++)"/>


@code {

public MudMenu Menu { get; set; }

public int FalseInvocationCount { get; private set; }

public int TrueInvocationCount { get; private set; }

}
22 changes: 21 additions & 1 deletion src/MudBlazor.UnitTests/Components/MenuTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Bunit;
using FluentAssertions;
Expand Down Expand Up @@ -258,5 +259,24 @@ public void OpenMenu_CloseMenuOnClick_CheckStillOpen()
comp.FindAll("div.mud-list-item")[3].Click();
comp.FindAll("div.mud-popover-open").Count.Should().Be(1);
}

[Test]
public async Task IsOpenChanged_InvokedWhenOpened_CheckTrueInvocationCountIsOne()
{
var comp = Context.RenderComponent<MenuIsOpenChangedTest>();
await Context.Renderer.Dispatcher.InvokeAsync(() => comp.Instance.Menu.OpenMenu(EventArgs.Empty));
comp.Instance.TrueInvocationCount.Should().Be(1);
comp.Instance.FalseInvocationCount.Should().Be(0);
}

[Test]
public async Task IsOpenChanged_InvokedWhenClosed_CheckTrueInvocationCountIsOneClickFalseInvocationCountIsOne()
{
var comp = Context.RenderComponent<MenuIsOpenChangedTest>();
await Context.Renderer.Dispatcher.InvokeAsync(() => comp.Instance.Menu.OpenMenu(EventArgs.Empty));
await Context.Renderer.Dispatcher.InvokeAsync(() => comp.Instance.Menu.CloseMenu());
comp.Instance.TrueInvocationCount.Should().Be(1);
comp.Instance.FalseInvocationCount.Should().Be(1);
}
}
}
9 changes: 9 additions & 0 deletions src/MudBlazor/Components/Menu/MudMenu.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,13 @@ public bool PositionAtCurser
[Parameter]
[Category(CategoryTypes.Menu.PopupBehavior)]
public RenderFragment ChildContent { get; set; }

/// <summary>
/// Fired when the menu IsOpen property changes.
/// </summary>
[Parameter]
[Category(CategoryTypes.Menu.PopupBehavior)]
public EventCallback<bool> IsOpenChanged { get; set; }

public string PopoverStyle { get; set; }

Expand All @@ -266,6 +273,7 @@ public void CloseMenu()
_isMouseOver = false;
PopoverStyle = null;
StateHasChanged();
IsOpenChanged.InvokeAsync(_isOpen);
}

/// <summary>
Expand All @@ -281,6 +289,7 @@ public void OpenMenu(EventArgs args)
if (PositionAtCursor) SetPopoverStyle((MouseEventArgs)args);
_isOpen = true;
StateHasChanged();
IsOpenChanged.InvokeAsync(_isOpen);
}

// Sets the popover style ONLY when there is an activator
Expand Down

0 comments on commit b3be3ce

Please sign in to comment.