Skip to content

Commit 96c3db5

Browse files
authored
feat(DrawerOption): add BodyContext parameter (#5112)
* feat(Drawer): 增加 BodyContext 参数 * test: 增加单元测试 * chore: bump version 9.2.8-beta02 * test: 更新单元测试 Co-Authored-By: Argo <[email protected]>
1 parent 5960d5c commit 96c3db5

File tree

7 files changed

+50
-4
lines changed

7 files changed

+50
-4
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>9.2.8-beta01</Version>
4+
<Version>9.2.8-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Drawer/Drawer.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
}
1111
<div tabindex="-1" class="@DrawerClassString" style="@DrawerStyleString" @onclick:stopPropagation>
1212
<div class="drawer-content">
13-
<CascadingValue Value="Close" IsFixed=" true">
14-
@ChildContent
13+
<CascadingValue Name="BodyContext" Value="BodyContext" IsFixed="true">
14+
<CascadingValue Value="Close" IsFixed=" true">
15+
@ChildContent
16+
</CascadingValue>
1517
</CascadingValue>
1618
</div>
1719
@if (AllowResize)

src/BootstrapBlazor/Components/Drawer/Drawer.razor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public partial class Drawer
113113
[Parameter]
114114
public Func<Task>? OnCloseAsync { get; set; }
115115

116+
/// <summary>
117+
/// 获得/设置 抽屉内容相关数据 多用于传值
118+
/// </summary>
119+
[Parameter]
120+
public object? BodyContext { get; set; }
121+
116122
/// <summary>
117123
/// <inheritdoc/>
118124
/// </summary>

src/BootstrapBlazor/Components/Drawer/DrawerContainer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ private Dictionary<string, object> GetParameters(DrawerOption option)
8888
{
8989
parameters.Add(nameof(Drawer.OnClickBackdrop), option.OnClickBackdrop);
9090
}
91+
if (option.BodyContext != null)
92+
{
93+
parameters.Add(nameof(Drawer.BodyContext), option.BodyContext);
94+
}
9195
return parameters;
9296
}
9397

src/BootstrapBlazor/Components/Drawer/DrawerOption.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ public class DrawerOption
6464
/// 获得/设置 关闭当前 Drawer 回调委托 默认 null
6565
/// </summary>
6666
public Func<Task>? OnCloseAsync { get; set; }
67+
68+
/// <summary>
69+
/// 获得/设置 相关连数据,多用于传值使用
70+
/// </summary>
71+
public object? BodyContext { get; set; }
6772
}

test/UnitTest/Components/DrawerTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ public void ChildContent_Ok()
106106
Assert.NotNull(button);
107107
}
108108

109+
[Fact]
110+
public void BodyContext_Ok()
111+
{
112+
var cut = Context.RenderComponent<Drawer>(builder =>
113+
{
114+
builder.Add(a => a.BodyContext, "test-body-context");
115+
builder.Add(a => a.ChildContent, s =>
116+
{
117+
s.OpenComponent<MockContent>(0);
118+
s.CloseComponent();
119+
});
120+
});
121+
122+
var component = cut.FindComponent<MockContent>();
123+
Assert.NotNull(component);
124+
125+
Assert.Equal("test-body-context", component.Instance.GetBodyContext());
126+
}
127+
109128
[Fact]
110129
public void ShowBackdrop_Ok()
111130
{
@@ -141,4 +160,13 @@ public void Position_Ok()
141160
});
142161
cut.Contains("--bb-drawer-position: absolute;");
143162
}
163+
164+
class MockContent : ComponentBase
165+
{
166+
[CascadingParameter(Name = "BodyContext")]
167+
[NotNull]
168+
private object? BodyContext { get; set; }
169+
170+
public string? GetBodyContext() => BodyContext.ToString();
171+
}
144172
}

test/UnitTest/Services/DrawerServiceTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public async Task Show_Ok()
2222
OnClickBackdrop = () => Task.CompletedTask,
2323
OnCloseAsync = () => Task.CompletedTask,
2424
Placement = Placement.Bottom,
25-
ShowBackdrop = true
25+
ShowBackdrop = true,
26+
BodyContext = "test-body-context"
2627
};
2728
var service = Context.Services.GetRequiredService<DrawerService>();
2829
var cut = Context.RenderComponent<BootstrapBlazorRoot>();

0 commit comments

Comments
 (0)