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

Add OnBeforeClose hook. #541

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/Blazored.Modal/BlazoredModalInstance.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public async Task CloseAsync()
/// <param name="modalResult"></param>
public async Task CloseAsync(ModalResult modalResult)
{
//If we have a OnBeforeClose we try to invoke it here
if (Options.OnBeforeClose != null)
{
await Options.OnBeforeClose(modalResult);
}

// Fade out the modal, and after that actually remove it
if (AnimationType is ModalAnimationType.FadeInOut)
{
Expand Down
6 changes: 5 additions & 1 deletion src/Blazored.Modal/Configuration/ModalOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Blazored.Modal;
using Blazored.Modal.Services;

namespace Blazored.Modal;

public class ModalOptions
{
Expand All @@ -14,4 +16,6 @@ public class ModalOptions
public ModalAnimationType? AnimationType { get; set; }
public bool? UseCustomLayout { get; set; }
public bool? ActivateFocusTrap { get; set; }

public Func<ModalResult,Task>? OnBeforeClose { get; set; }
}
27 changes: 26 additions & 1 deletion tests/Blazored.Modal.Tests/ModalOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Blazored.Modal.Services;
using System.Threading.Tasks;
using Blazored.Modal.Services;
using Blazored.Modal.Tests.Assets;
using Bunit;
using Microsoft.AspNetCore.Components;
Expand Down Expand Up @@ -266,5 +267,29 @@ public void ModalDisplaysCustomSizeClassWhenSizeIsCustom()
// Assert
Assert.NotNull(cut.Find(".my-custom-size"));
}

[Fact]
public void ModalTriggersOnBeforeCloseWhenModalCloses()
{
// Arrange
var onBeforeCloseTriggered = false;
var options = new ModalOptions
{
OnBeforeClose = result =>
{
onBeforeCloseTriggered = result != null;
return Task.CompletedTask;
}
};
var modalService = Services.GetService<IModalService>();
var cut = RenderComponent<BlazoredModal>(CascadingValue(modalService!));

// Act
modalService.Show<TestComponent>("", options);
cut.Find(".bm-close").Click();

// Assert
Assert.True(onBeforeCloseTriggered);
}
}
}
Loading