From 6cb350dcbb749529cf04a4b3d455d8767c1fcba2 Mon Sep 17 00:00:00 2001 From: Chris Sainty Date: Mon, 28 Mar 2022 11:49:39 +0100 Subject: [PATCH] Added Confirmed property to ModalResult and added payload to Cancelled ModalResult (#391) --- src/Blazored.Modal/BlazoredModalInstance.razor | 2 +- src/Blazored.Modal/BlazoredModalInstance.razor.cs | 8 ++++++-- src/Blazored.Modal/Services/ModalResult.cs | 8 ++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Blazored.Modal/BlazoredModalInstance.razor b/src/Blazored.Modal/BlazoredModalInstance.razor index 0117e6c4..fe1fa3ba 100644 --- a/src/Blazored.Modal/BlazoredModalInstance.razor +++ b/src/Blazored.Modal/BlazoredModalInstance.razor @@ -53,7 +53,7 @@ else

@Title

@if (!HideCloseButton) { - } diff --git a/src/Blazored.Modal/BlazoredModalInstance.razor.cs b/src/Blazored.Modal/BlazoredModalInstance.razor.cs index 46f48482..26b5ad30 100644 --- a/src/Blazored.Modal/BlazoredModalInstance.razor.cs +++ b/src/Blazored.Modal/BlazoredModalInstance.razor.cs @@ -1,13 +1,11 @@ using Blazored.Modal.Services; using Microsoft.AspNetCore.Components; -using Microsoft.JSInterop; using System.Diagnostics.CodeAnalysis; namespace Blazored.Modal; public partial class BlazoredModalInstance : IDisposable { - [Inject] private IJSRuntime JsRuntime { get; set; } = default!; [CascadingParameter] private BlazoredModal Parent { get; set; } = default!; [CascadingParameter] private ModalOptions GlobalModalOptions { get; set; } = default!; @@ -108,6 +106,12 @@ public async Task CloseAsync(ModalResult modalResult) /// public async Task CancelAsync() => await CloseAsync(ModalResult.Cancel()); + + /// + /// Closes the modal returning the specified in a cancelled ModalResult. + /// + public async Task CancelAsync(TPayload payload) + => await CloseAsync(ModalResult.Cancel(payload)); private void ConfigureInstance() { diff --git a/src/Blazored.Modal/Services/ModalResult.cs b/src/Blazored.Modal/Services/ModalResult.cs index a4f60836..aedfd1b3 100644 --- a/src/Blazored.Modal/Services/ModalResult.cs +++ b/src/Blazored.Modal/Services/ModalResult.cs @@ -5,6 +5,7 @@ public class ModalResult public object? Data { get; } public Type? DataType { get; } public bool Cancelled { get; } + public bool Confirmed => !Cancelled; private ModalResult(object? data, Type? resultType, bool cancelled) { @@ -16,12 +17,15 @@ private ModalResult(object? data, Type? resultType, bool cancelled) public static ModalResult Ok(T result) => Ok(result, typeof(T)); - public static ModalResult Ok(T result, Type? modalType) - => new(result, modalType, false); + public static ModalResult Ok(T result, Type? dataType) + => new(result, dataType, false); public static ModalResult Ok() => new(null, null, false); public static ModalResult Cancel() => new(null, null, true); + + public static ModalResult Cancel(T payload) + => new(payload, null, true); } \ No newline at end of file