Skip to content

Commit 9cfd787

Browse files
authored
feat(ICloseable): add ICloseable interface (#7644)
* feat(IClosable): add IClosable interface * refactor: 更新单词拼写 * refactor: 更正变量名 * refactor: 移除标签 * test: 移除 AddMockEnvironment 服务 * test: 消除警告信息 * test: 更新单元测试 * doc: 更新 xml 注释 * refactor: 更新逻辑 * chore: bump version 10.3.2-beta02
1 parent 1700fbb commit 9cfd787

File tree

11 files changed

+64
-41
lines changed

11 files changed

+64
-41
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.3.2-beta01</Version>
4+
<Version>10.3.2-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Dialog/Dialog.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@inherits BootstrapComponentBase
33

44
<Modal @ref="_modal" IsBackdrop="_isBackdrop" IsKeyboard="@_isKeyboard" IsFade="_isFade"
5-
OnShownAsync="_onShownAsync" OnCloseAsync="_onCloseAsync"
5+
OnShownAsync="_onShownAsync" OnClosingAsync="_onClosingAsync" OnCloseAsync="_onCloseAsync"
66
class="@ClassString">
77
@for (var index = 0; index < DialogParameters.Keys.Count; index++)
88
{

src/BootstrapBlazor/Components/Dialog/Dialog.razor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public partial class Dialog : IDisposable
1919
private Modal? _modal = null;
2020
private Func<Task>? _onShownAsync = null;
2121
private Func<Task>? _onCloseAsync = null;
22+
private Func<Task<bool>>? _onClosingAsync = null;
2223

2324
private readonly Dictionary<Dictionary<string, object>, (bool IsKeyboard, bool IsBackdrop, Func<Task>? OnCloseCallback)> DialogParameters = [];
2425
private Dictionary<string, object>? _currentParameter;
@@ -87,6 +88,8 @@ private async Task Show(DialogOption option)
8788
}
8889
};
8990

91+
_onClosingAsync = option.OnClosingAsync;
92+
9093
_isKeyboard = option.IsKeyboard;
9194
_isBackdrop = option.IsBackdrop;
9295
_isFade = option.IsFade;

src/BootstrapBlazor/Components/Dialog/DialogOption.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace BootstrapBlazor.Components;
99
/// <para lang="zh">Dialog 对话框组件</para>
1010
/// <para lang="en">Dialog component</para>
1111
/// </summary>
12-
public class DialogOption
12+
public class DialogOption : ICloseable
1313
{
1414
/// <summary>
1515
/// <para lang="zh">获得/设置 关联的 Modal 实例</para>
@@ -192,11 +192,15 @@ public class DialogOption
192192
public string? CloseButtonText { get; set; }
193193

194194
/// <summary>
195-
/// <para lang="zh">获得/设置 关闭对话框回调方法</para>
196-
/// <para lang="en">Gets or sets the callback method for closing the dialog</para>
195+
/// <inheritdoc cref="ICloseable.OnCloseAsync"/>
197196
/// </summary>
198197
public Func<Task>? OnCloseAsync { get; set; }
199198

199+
/// <summary>
200+
/// <inheritdoc cref="ICloseable.OnClosingAsync"/>
201+
/// </summary>
202+
public Func<Task<bool>>? OnClosingAsync { get; set; }
203+
200204
/// <summary>
201205
/// <para lang="zh">获得/设置 是否自动关闭对话框(保存成功后) 默认值为 true</para>
202206
/// <para lang="en">Gets or sets whether to automatically close the dialog after saving successfully, default is true</para>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// <para lang="zh">可关闭接口</para>
10+
/// <para lang="en">the closable interface</para>
11+
/// </summary>
12+
public interface ICloseable
13+
{
14+
/// <summary>
15+
/// <para lang="zh">获得/设置 弹出窗口关闭时的回调委托</para>
16+
/// <para lang="en">Gets or sets the callback delegate when the popup is closed</para>
17+
/// </summary>
18+
Func<Task>? OnCloseAsync { get; set; }
19+
20+
/// <summary>
21+
/// <para lang="zh">关闭之前回调方法 返回 true 时关闭弹窗 返回 false 时阻止关闭弹窗</para>
22+
/// <para lang="en">Callback Method Before Closing. Return true to close, false to prevent closing</para>
23+
/// </summary>
24+
Func<Task<bool>>? OnClosingAsync { get; set; }
25+
}

src/BootstrapBlazor/Components/Modal/Modal.razor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace BootstrapBlazor.Components;
1111
/// <para lang="zh">Modal 组件</para>
1212
/// <para lang="en">Modal component</para>
1313
/// </summary>
14-
public partial class Modal
14+
public partial class Modal : ICloseable
1515
{
1616
[Inject]
1717
[NotNull]
@@ -77,15 +77,13 @@ public partial class Modal
7777
public Func<Task>? OnShownAsync { get; set; }
7878

7979
/// <summary>
80-
/// <para lang="zh">获得/设置 弹出窗口关闭时的回调委托</para>
81-
/// <para lang="en">Gets or sets the callback delegate when the popup is closed</para>
80+
/// <inheritdoc cref="ICloseable.OnCloseAsync"/>
8281
/// </summary>
8382
[Parameter]
8483
public Func<Task>? OnCloseAsync { get; set; }
8584

8685
/// <summary>
87-
/// <para lang="zh">关闭之前回调方法 返回 true 时关闭弹窗 返回 false 时阻止关闭弹窗</para>
88-
/// <para lang="en">Callback Method Before Closing. Return true to close, false to prevent closing</para>
86+
/// <inheritdoc cref="ICloseable.OnClosingAsync"/>
8987
/// </summary>
9088
[Parameter]
9189
public Func<Task<bool>>? OnClosingAsync { get; set; }

src/BootstrapBlazor/Extensions/PropertyInfoExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static bool IsStatic(this PropertyInfo p)
2626

2727
/// <summary>
2828
/// <para lang="zh">判断属性是否可以写入扩展方法</para>
29-
/// <para lang="en">Determines whether the property can be written to extension method</para>
29+
/// <para lang="en">Determines whether the property can be written to</para>
3030
/// </summary>
3131
/// <param name="p"></param>
3232
public static bool IsCanWrite(this PropertyInfo p) => p.CanWrite && !p.IsInit();

src/BootstrapBlazor/Utils/BootstrapDynamicComponent.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ public RenderFragment<TModel> RenderEditTemplate<TModel>(string? modelParameterN
6262
{
6363
var index = 0;
6464
builder.OpenComponent(index++, componentType);
65+
modelParameterName ??= "Model";
66+
var modelProperty = componentType.GetPropertyByName(modelParameterName);
67+
if (modelProperty.HasParameterAttribute(typeof(TModel)))
68+
{
69+
builder.AddAttribute(index++, modelParameterName, model);
70+
}
6571
if (parameters != null)
6672
{
67-
modelParameterName ??= "Model";
68-
var modelProperty = componentType.GetPropertyByName(modelParameterName);
69-
if (modelProperty.HasParameterAttribute(typeof(TModel)))
70-
{
71-
builder.AddAttribute(index++, modelParameterName, model);
72-
}
7373
foreach (var p in parameters)
7474
{
7575
builder.AddAttribute(index++, p.Key, p.Value);

test/UnitTest/Components/DialogTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,22 @@ await cut.InvokeAsync(() => dialog.ShowCloseDialog<MockValidateFormDialog>("Clos
583583
await cut.InvokeAsync(() => dialog.ShowExceptionDialog(null, new Exception("Test")));
584584
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
585585
#endregion
586+
587+
// OnClosing
588+
var closing = false;
589+
await cut.InvokeAsync(() => dialog.Show(new DialogOption()
590+
{
591+
OnClosingAsync = () =>
592+
{
593+
closing = true;
594+
return Task.FromResult(false);
595+
}
596+
}));
597+
598+
// 由于返回 false 所以关窗方法被阻止
599+
await cut.InvokeAsync(() => modal.Instance.Close());
600+
Assert.True(closing);
601+
await cut.InvokeAsync(() => modal.Instance.CloseCallback());
586602
}
587603

588604
private class MockValidateFormDialog : ComponentBase

test/UnitTest/Core/TestBase.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ public TestBase()
1313
{
1414
Context = new BunitContext();
1515
Context.JSInterop.Mode = JSRuntimeMode.Loose;
16-
17-
Context.Services.AddMockEnvironment();
1816
}
1917

2018
public void Dispose()
2119
{
22-
#pragma warning disable CA2012
2320
// 由于 bUnit 2.0 继承了 IAsyncDisposable 接口,因此此处调用 DisposeAsync 方法
24-
Context.DisposeAsync();
25-
#pragma warning restore CA2012
21+
Context.DisposeAsync().AsTask().Wait();
2622
GC.SuppressFinalize(this);
2723
}
2824
}

0 commit comments

Comments
 (0)