Skip to content

Commit 9f3214b

Browse files
authored
feat(ContextMenuDivider): add ContextMenuDivider component (#5379)
* feat: 增加 IContextMenuItem 接口用于规范化右键菜单组件 * refactor: ContextMenuItem 集成 IContextMenuItem 接口 * feat: 增加 ContextMenuDivider 组件 * refactor: 增加只读关键字消除提示信息 * refactor: 更改参数类型为接口 * refactor: 根据接口更新逻辑 * feat: 增加其他组件兼容逻辑 * refactor: 移除其他组件支持逻辑 * test: 增加单元测试 * chore: bump version 9.3.1-beta19
1 parent 19f6dfe commit 9f3214b

File tree

7 files changed

+122
-12
lines changed

7 files changed

+122
-12
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.3.1-beta18</Version>
4+
<Version>9.3.1-beta19</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77
@ChildContent
88
</CascadingValue>
99
<RenderTemplate>
10-
@foreach (var item in _contextMenuItems)
10+
@foreach (var context in _contextMenuItems)
1111
{
12-
var disabled = GetItemTriggerClick(item);
13-
<DynamicElement @key="item" TagName="div" class="@GetItemClassString(disabled)"
14-
TriggerClick="!disabled" OnClick="() => OnClickItem(item)">
15-
<i class="@GetItemIconString(item)"></i>
16-
<span>@item.Text</span>
17-
</DynamicElement>
12+
if (context is ContextMenuDivider)
13+
{
14+
<Divider></Divider>
15+
continue;
16+
}
17+
else if (context is ContextMenuItem item)
18+
{
19+
var disabled = GetItemTriggerClick(item);
20+
<DynamicElement @key="item" TagName="div" class="@GetItemClassString(disabled)"
21+
TriggerClick="!disabled" OnClick="() => OnClickItem(item)">
22+
<i class="@GetItemIconString(item)"></i>
23+
<span>@item.Text</span>
24+
</DynamicElement>
25+
}
1826
}
1927
</RenderTemplate>
2028
</div>

src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public partial class ContextMenu
4141

4242
private string ZoneId => ContextMenuZone.Id;
4343

44-
private List<ContextMenuItem> _contextMenuItems = [];
44+
private readonly List<IContextMenuItem> _contextMenuItems = [];
4545

4646
private static string? GetItemClassString(bool disabled) => CssBuilder.Default("dropdown-item")
4747
.AddClass("disabled", disabled)
@@ -112,11 +112,11 @@ private async Task OnClickItem(ContextMenuItem item)
112112
/// 增加 ContextMenuItem 方法
113113
/// </summary>
114114
/// <param name="item"></param>
115-
internal void AddItem(ContextMenuItem item) => _contextMenuItems.Add(item);
115+
internal void AddItem(IContextMenuItem item) => _contextMenuItems.Add(item);
116116

117117
/// <summary>
118118
/// 移除 ContextMenuItem 方法
119119
/// </summary>
120120
/// <param name="item"></param>
121-
internal void RemoveItem(ContextMenuItem item) => _contextMenuItems.Remove(item);
121+
internal void RemoveItem(IContextMenuItem item) => _contextMenuItems.Remove(item);
122122
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Rendering;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// ContextMenuDivider 组件
12+
/// </summary>
13+
public class ContextMenuDivider : Divider, IContextMenuItem, IDisposable
14+
{
15+
[CascadingParameter]
16+
[NotNull]
17+
private ContextMenu? ContextMenu { get; set; }
18+
19+
/// <summary>
20+
/// <inheritdoc/>
21+
/// </summary>
22+
protected override void OnInitialized()
23+
{
24+
base.OnInitialized();
25+
26+
ContextMenu.AddItem(this);
27+
}
28+
29+
/// <summary>
30+
/// <inheritdoc/>
31+
/// </summary>
32+
/// <param name="builder"></param>
33+
protected override void BuildRenderTree(RenderTreeBuilder builder) { }
34+
35+
private bool disposedValue;
36+
37+
/// <summary>
38+
/// 释放资源方法
39+
/// </summary>
40+
/// <param name="disposing"></param>
41+
protected virtual void Dispose(bool disposing)
42+
{
43+
if (!disposedValue)
44+
{
45+
if (disposing)
46+
{
47+
ContextMenu.RemoveItem(this);
48+
}
49+
disposedValue = true;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// <inheritdoc/>
55+
/// </summary>
56+
public void Dispose()
57+
{
58+
Dispose(disposing: true);
59+
GC.SuppressFinalize(this);
60+
}
61+
}

src/BootstrapBlazor/Components/ContextMenu/ContextMenuItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BootstrapBlazor.Components;
88
/// <summary>
99
/// ContextMenuItem 类
1010
/// </summary>
11-
public class ContextMenuItem : ComponentBase, IDisposable
11+
public class ContextMenuItem : ComponentBase, IContextMenuItem, IDisposable
1212
{
1313
/// <summary>
1414
/// 获得/设置 显示文本
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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([email protected]) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// IContextMenuItem 接口
10+
/// </summary>
11+
public interface IContextMenuItem
12+
{
13+
14+
}

test/UnitTest/Components/ContextMenuTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,31 @@ private static void TriggerTouchStart(IElement row)
247247
]
248248
});
249249
}
250+
251+
[Fact]
252+
public void ContextMenuDivider_Ok()
253+
{
254+
var cut = Context.RenderComponent<ContextMenuZone>(pb =>
255+
{
256+
pb.AddChildContent<ContextMenu>(pb =>
257+
{
258+
pb.AddChildContent<ContextMenuItem>(builder =>
259+
{
260+
builder.Add(a => a.Text, "Item1");
261+
});
262+
pb.AddChildContent<ContextMenuDivider>();
263+
pb.AddChildContent<ContextMenuItem>(builder =>
264+
{
265+
builder.Add(a => a.Text, "Item2");
266+
});
267+
});
268+
});
269+
270+
var menu = cut.Find(".dropdown-menu");
271+
var children = menu.Children;
272+
Assert.Equal(3, children.Length);
273+
Assert.Equal("dropdown-item", children[0].ClassName);
274+
Assert.Equal("divider", children[1].ClassName);
275+
Assert.Equal("dropdown-item", children[2].ClassName);
276+
}
250277
}

0 commit comments

Comments
 (0)