Skip to content

Commit

Permalink
Add OnBeforeClose hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelRoozekrans committed Nov 15, 2023
1 parent 8c39c9b commit 6666fca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Blazored.Modal/BlazoredModalInstance.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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);
}
}
}

0 comments on commit 6666fca

Please sign in to comment.