diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 796e6d7247d..9c22f9c46f1 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 9.3.1-beta18 + 9.3.1-beta19 diff --git a/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor b/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor index d140b3ead61..624cff6ace2 100644 --- a/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor +++ b/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor @@ -7,14 +7,22 @@ @ChildContent - @foreach (var item in _contextMenuItems) + @foreach (var context in _contextMenuItems) { - var disabled = GetItemTriggerClick(item); - - - @item.Text - + if (context is ContextMenuDivider) + { + + continue; + } + else if (context is ContextMenuItem item) + { + var disabled = GetItemTriggerClick(item); + + + @item.Text + + } } diff --git a/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.cs b/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.cs index 9ccf441ae64..7092087e27a 100644 --- a/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.cs +++ b/src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.cs @@ -41,7 +41,7 @@ public partial class ContextMenu private string ZoneId => ContextMenuZone.Id; - private List _contextMenuItems = []; + private readonly List _contextMenuItems = []; private static string? GetItemClassString(bool disabled) => CssBuilder.Default("dropdown-item") .AddClass("disabled", disabled) @@ -112,11 +112,11 @@ private async Task OnClickItem(ContextMenuItem item) /// 增加 ContextMenuItem 方法 /// /// - internal void AddItem(ContextMenuItem item) => _contextMenuItems.Add(item); + internal void AddItem(IContextMenuItem item) => _contextMenuItems.Add(item); /// /// 移除 ContextMenuItem 方法 /// /// - internal void RemoveItem(ContextMenuItem item) => _contextMenuItems.Remove(item); + internal void RemoveItem(IContextMenuItem item) => _contextMenuItems.Remove(item); } diff --git a/src/BootstrapBlazor/Components/ContextMenu/ContextMenuDivider.cs b/src/BootstrapBlazor/Components/ContextMenu/ContextMenuDivider.cs new file mode 100644 index 00000000000..821116c6d15 --- /dev/null +++ b/src/BootstrapBlazor/Components/ContextMenu/ContextMenuDivider.cs @@ -0,0 +1,61 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +using Microsoft.AspNetCore.Components.Rendering; + +namespace BootstrapBlazor.Components; + +/// +/// ContextMenuDivider 组件 +/// +public class ContextMenuDivider : Divider, IContextMenuItem, IDisposable +{ + [CascadingParameter] + [NotNull] + private ContextMenu? ContextMenu { get; set; } + + /// + /// + /// + protected override void OnInitialized() + { + base.OnInitialized(); + + ContextMenu.AddItem(this); + } + + /// + /// + /// + /// + protected override void BuildRenderTree(RenderTreeBuilder builder) { } + + private bool disposedValue; + + /// + /// 释放资源方法 + /// + /// + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + ContextMenu.RemoveItem(this); + } + disposedValue = true; + } + } + + /// + /// + /// + public void Dispose() + { + Dispose(disposing: true); + GC.SuppressFinalize(this); + } +} diff --git a/src/BootstrapBlazor/Components/ContextMenu/ContextMenuItem.cs b/src/BootstrapBlazor/Components/ContextMenu/ContextMenuItem.cs index 4c836febbc7..f811f06fe3e 100644 --- a/src/BootstrapBlazor/Components/ContextMenu/ContextMenuItem.cs +++ b/src/BootstrapBlazor/Components/ContextMenu/ContextMenuItem.cs @@ -8,7 +8,7 @@ namespace BootstrapBlazor.Components; /// /// ContextMenuItem 类 /// -public class ContextMenuItem : ComponentBase, IDisposable +public class ContextMenuItem : ComponentBase, IContextMenuItem, IDisposable { /// /// 获得/设置 显示文本 diff --git a/src/BootstrapBlazor/Components/ContextMenu/IContextMenuItem.cs b/src/BootstrapBlazor/Components/ContextMenu/IContextMenuItem.cs new file mode 100644 index 00000000000..3031c4429ed --- /dev/null +++ b/src/BootstrapBlazor/Components/ContextMenu/IContextMenuItem.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the Apache 2.0 License +// See the LICENSE file in the project root for more information. +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone + +namespace BootstrapBlazor.Components; + +/// +/// IContextMenuItem 接口 +/// +public interface IContextMenuItem +{ + +} diff --git a/test/UnitTest/Components/ContextMenuTest.cs b/test/UnitTest/Components/ContextMenuTest.cs index 5f23e2f5995..14dd450deac 100644 --- a/test/UnitTest/Components/ContextMenuTest.cs +++ b/test/UnitTest/Components/ContextMenuTest.cs @@ -247,4 +247,31 @@ private static void TriggerTouchStart(IElement row) ] }); } + + [Fact] + public void ContextMenuDivider_Ok() + { + var cut = Context.RenderComponent(pb => + { + pb.AddChildContent(pb => + { + pb.AddChildContent(builder => + { + builder.Add(a => a.Text, "Item1"); + }); + pb.AddChildContent(); + pb.AddChildContent(builder => + { + builder.Add(a => a.Text, "Item2"); + }); + }); + }); + + var menu = cut.Find(".dropdown-menu"); + var children = menu.Children; + Assert.Equal(3, children.Length); + Assert.Equal("dropdown-item", children[0].ClassName); + Assert.Equal("divider", children[1].ClassName); + Assert.Equal("dropdown-item", children[2].ClassName); + } }