From 8005e94cc198cfa977c8cceb513225129b716a36 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Tue, 5 Mar 2024 17:39:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=B6=88=E9=99=A4=20Modal=20=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E5=BB=B6=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/SweetAlert/SwalOption.cs | 7 +++--- .../Components/SweetAlert/SweetAlert.razor.cs | 11 +++++++++ .../Components/SweetAlert/SweetContext.cs | 23 +++++++++++++++++++ .../Extensions/SwalExtensions.cs | 6 ++--- 4 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs diff --git a/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs b/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs index 9e4f2751c0f..f97ab72edd2 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs @@ -15,9 +15,10 @@ public class SwalOption : PopupOptionBase internal Modal? Modal { get; set; } /// - /// 获得/设置 模态弹窗返回值任务实例 + /// 获得/设置 模态弹窗任务上下文 /// - internal TaskCompletionSource ReturnTask { get; } = new TaskCompletionSource(); + [NotNull] + internal SweetContext? ConfirmContext { get; set; } /// /// 获得/设置 是否为确认弹窗模式 此属性给模态弹窗时使用 默认为 false @@ -148,7 +149,7 @@ public async Task CloseAsync(bool returnValue = true) if (IsConfirm) { - ReturnTask.TrySetResult(returnValue); + ConfirmContext.Value = returnValue; } } } diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs index e4158565e83..0e0b87e984e 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs @@ -36,6 +36,8 @@ public partial class SweetAlert : IAsyncDisposable [NotNull] private Func? OnCloseAsync { get; set; } + private SweetContext _context = default!; + /// /// /// @@ -55,6 +57,10 @@ protected override void OnInitialized() { DelayToken.Cancel(); } + if (_context != null) + { + _context.ConfirmTask.TrySetResult(_context.Value); + } StateHasChanged(); return Task.CompletedTask; }; @@ -104,6 +110,11 @@ private Task Show(SwalOption option) Delay = option.Delay; option.Modal = ModalContainer; + if (option.IsConfirm) + { + _context = new() { ConfirmTask = new() }; + option.ConfirmContext = _context; + } var parameters = option.ToAttributes(); parameters.Add(nameof(ModalDialog.BodyTemplate), BootstrapDynamicComponent.CreateComponent(option.Parse()).Render()); diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs new file mode 100644 index 00000000000..ee94580b012 --- /dev/null +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs @@ -0,0 +1,23 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +// Website: https://www.blazor.zone or https://argozhang.github.io/ + +namespace BootstrapBlazor.Components; + +internal class SweetContext +{ + /// + /// 获得/设置 弹窗返回值 + /// + public bool Value { get; set; } + + /// + /// 获得/设置 弹窗任务上下文 + /// + [NotNull] +#if NET7_0_OR_GREATER + public required TaskCompletionSource? ConfirmTask { get; init; } +#else + public TaskCompletionSource? ConfirmTask { get; set; } +#endif +} diff --git a/src/BootstrapBlazor/Extensions/SwalExtensions.cs b/src/BootstrapBlazor/Extensions/SwalExtensions.cs index 1ba53f15bfe..fe61557d616 100644 --- a/src/BootstrapBlazor/Extensions/SwalExtensions.cs +++ b/src/BootstrapBlazor/Extensions/SwalExtensions.cs @@ -20,7 +20,7 @@ public static async Task ShowModal(this SwalService service, SwalOption op { option.IsConfirm = true; await service.Show(option, swal); - return await option.ReturnTask.Task; + return await option.ConfirmContext.ConfirmTask.Task; } /// @@ -38,7 +38,7 @@ public static async Task ShowModal(this SwalService service, SwalOption op { if (option.IsConfirm) { - option.ReturnTask.TrySetResult(false); + option.ConfirmContext.Value = false; } if (option.OnCloseAsync != null) { @@ -49,7 +49,7 @@ public static async Task ShowModal(this SwalService service, SwalOption op { if (option.IsConfirm) { - option.ReturnTask.TrySetResult(true); + option.ConfirmContext.Value = true; } if (option.OnConfirmAsync != null) { From 9c84fc9c9aeebeac846028ea0cc973b55b129785 Mon Sep 17 00:00:00 2001 From: Argo-AscioTech Date: Tue, 5 Mar 2024 17:40:16 +0800 Subject: [PATCH 2/2] chore: bump version 8.3.1-beta05 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 761d394d44f..92fa47a05ca 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 8.3.1-beta04 + 8.3.1-beta05