Skip to content

Commit

Permalink
Added Confirmed property to ModalResult and added payload to Cancelle…
Browse files Browse the repository at this point in the history
…d ModalResult (#391)
  • Loading branch information
chrissainty authored Mar 28, 2022
1 parent 072afe4 commit 6cb350d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Blazored.Modal/BlazoredModalInstance.razor
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ else
<h3 class="blazored-modal-title">@Title</h3>
@if (!HideCloseButton)
{
<button type="button" class="blazored-modal-close" aria-label="close" @onclick="CancelAsync" @attributes="@_closeBtnAttributes">
<button type="button" class="blazored-modal-close" aria-label="close" @onclick="() => CancelAsync()" @attributes="@_closeBtnAttributes">
<span>&times;</span>
</button>
}
Expand Down
8 changes: 6 additions & 2 deletions src/Blazored.Modal/BlazoredModalInstance.razor.cs
Original file line number Diff line number Diff line change
@@ -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!;

Expand Down Expand Up @@ -108,6 +106,12 @@ public async Task CloseAsync(ModalResult modalResult)
/// </summary>
public async Task CancelAsync()
=> await CloseAsync(ModalResult.Cancel());

/// <summary>
/// Closes the modal returning the specified <paramref name="payload"/> in a cancelled ModalResult.
/// </summary>
public async Task CancelAsync<TPayload>(TPayload payload)
=> await CloseAsync(ModalResult.Cancel(payload));

private void ConfigureInstance()
{
Expand Down
8 changes: 6 additions & 2 deletions src/Blazored.Modal/Services/ModalResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -16,12 +17,15 @@ private ModalResult(object? data, Type? resultType, bool cancelled)
public static ModalResult Ok<T>(T result)
=> Ok(result, typeof(T));

public static ModalResult Ok<T>(T result, Type? modalType)
=> new(result, modalType, false);
public static ModalResult Ok<T>(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>(T payload)
=> new(payload, null, true);
}

0 comments on commit 6cb350d

Please sign in to comment.