Skip to content

Commit e1858d6

Browse files
authored
feat(Drawer): add CloseAsync method (#7506)
* feat(Drawer): add CloseAsync method * test: 更新单元测试 * test: 更新单元测试
1 parent f6db501 commit e1858d6

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -57,6 +57,7 @@ private void RenderDrawer(RenderTreeBuilder builder, DrawerOption option)
5757
builder.AddAttribute(1, "class", option.Class);
5858
}
5959
builder.AddMultipleAttributes(2, GetParameters(option));
60+
builder.AddComponentReferenceCapture(3, obj => option.Drawer = obj as Drawer);
6061
builder.CloseComponent();
6162
}
6263

src/BootstrapBlazor/Components/Drawer/DrawerOption.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -84,4 +84,20 @@ public class DrawerOption
8484
/// 获得/设置 z-index 参数值 默认 null 未设置
8585
/// </summary>
8686
public int? ZIndex { get; set; }
87+
88+
/// <summary>
89+
/// 获得/设置 <see cref="Drawer"/> 实例
90+
/// </summary>
91+
internal Drawer? Drawer { get; set; }
92+
93+
/// <summary>
94+
/// 关闭抽屉弹窗方法
95+
/// </summary>
96+
public async Task CloseAsync()
97+
{
98+
if (Drawer != null)
99+
{
100+
await Drawer.Close();
101+
}
102+
}
87103
}

test/UnitTest/Components/DrawerTest.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,6 @@ public void BodyScroll_Ok()
207207
cut.Contains("data-bb-scroll=\"true\"");
208208
}
209209

210-
[Fact]
211-
public async Task Close_Ok()
212-
{
213-
Context.JSInterop.Setup<bool>("execute", matcher => true).SetResult(true);
214-
var cut = Context.Render<Drawer>();
215-
await cut.InvokeAsync(() => cut.Instance.Close());
216-
}
217-
218210
class MockContent : ComponentBase
219211
{
220212
[CascadingParameter(Name = "BodyContext")]

test/UnitTest/Services/DrawerServiceTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ public async Task Show_Ok()
5353
await service.Show(type);
5454
button = cut.Find("button");
5555
await cut.InvokeAsync(() => button.Click());
56+
57+
// 测试 Option 关闭方法
58+
Context.JSInterop.Setup<bool>("execute", matcher => true).SetResult(true);
59+
var closed = false;
60+
option.OnCloseAsync = () =>
61+
{
62+
closed = true;
63+
return Task.CompletedTask;
64+
};
65+
await service.Show(option);
66+
await cut.InvokeAsync(option.CloseAsync);
67+
Assert.True(closed);
5668
}
5769

5870
private static RenderFragment RenderContent() => builder =>

0 commit comments

Comments
 (0)