Skip to content

Commit 89283d2

Browse files
ArgoZhangdensen2014ice6
authored
feat(SweetAlert): support trigger OnCloseAsync when set IsAutoHide to true (#5040)
* refactor: 精简代码 * refactor: 增加注释 * refactor: 增加资源释放 * refactor: 支持 OnCloseAsync 回调方法 * test: 增加单元测试 * chore: bump version 9.2.4 Co-Authored-By: Alex chow <[email protected]> --------- Co-authored-by: Alex chow <[email protected]> Co-authored-by: ice6 <[email protected]>
1 parent 8407fc6 commit 89283d2

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public class SwalOption : PopupOptionBase
102102
public Func<Task>? OnConfirmAsync { get; set; }
103103

104104
/// <summary>
105-
///
105+
/// 构造函数
106106
/// </summary>
107107
public SwalOption()
108108
{

src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,16 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
8888
{
8989
if (DelayToken.IsCancellationRequested)
9090
{
91+
DelayToken.Dispose();
9192
DelayToken = new();
9293
}
9394
await Task.Delay(Delay, DelayToken.Token);
9495
await ModalContainer.Close();
96+
97+
if (OnCloseCallbackAsync != null)
98+
{
99+
await OnCloseCallbackAsync();
100+
}
95101
}
96102
catch (TaskCanceledException) { }
97103
}
@@ -100,6 +106,8 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
100106

101107
private bool AutoHideCheck() => IsAutoHide && Delay > 0;
102108

109+
private Func<Task>? OnCloseCallbackAsync = null;
110+
103111
private async Task Show(SwalOption option)
104112
{
105113
if (!IsShowDialog)
@@ -121,6 +129,8 @@ private async Task Show(SwalOption option)
121129

122130
DialogParameter = parameters;
123131

132+
OnCloseCallbackAsync = AutoHideCheck() ? option.OnCloseAsync : null;
133+
124134
// 渲染 UI 准备弹窗 Dialog
125135
await InvokeAsync(StateHasChanged);
126136
}

src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,5 @@ internal class SweetContext
1616
/// 获得/设置 弹窗任务上下文
1717
/// </summary>
1818
[NotNull]
19-
#if NET7_0_OR_GREATER
20-
public required TaskCompletionSource<bool>? ConfirmTask { get; init; }
21-
#else
22-
public TaskCompletionSource<bool>? ConfirmTask { get; set; }
23-
#endif
19+
public TaskCompletionSource<bool>? ConfirmTask { get; init; }
2420
}

test/UnitTest/Components/SwalTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,11 @@ public void Show_Ok()
271271
Content = "I am auto hide",
272272
IsAutoHide = true,
273273
ForceDelay = true,
274-
Delay = 500
274+
Delay = 500,
275+
OnCloseAsync = () =>
276+
{
277+
return Task.CompletedTask;
278+
}
275279
}));
276280
Thread.Sleep(150);
277281
// 弹窗显示

0 commit comments

Comments
 (0)