File tree Expand file tree Collapse file tree 7 files changed +122
-12
lines changed Expand file tree Collapse file tree 7 files changed +122
-12
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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 /// 获得/设置 显示文本
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments