diff --git a/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor b/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor index 6d8015f47d7..4e4720d6cfc 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor @@ -190,7 +190,7 @@ private void Navigation()

- +
@Localizer["TabItem1Content"]
@@ -211,7 +211,7 @@ private void Navigation()
- +
@Localizer["TabItem1Content"]
@@ -232,7 +232,7 @@ private void Navigation()
- +
@Localizer["TabItem1Content"]
@@ -339,7 +339,7 @@ private void Navigation() - +
@Localizer["TabItem1Content"]
@@ -497,30 +497,25 @@ private void Navigation() - - - -
@Localizer["TabItem1Content"]
- -
- -
@Localizer["TabItem2Content"]
-
- -
@Localizer["TabItem3Content"]
-
- -
@Localizer["TabItem4Content"]
-
-
- - - - - - - -
+
+

@((MarkupString)Localizer["TabsContextMenuDesc"].Value)

+
+ + +
@Localizer["TabItem1Content"]
+ +
+ +
@Localizer["TabItem2Content"]
+
+ +
@Localizer["TabItem3Content"]
+
+ +
@Localizer["TabItem4Content"]
+
+
diff --git a/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor.cs index 737571a6762..f84abb3028d 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/Tabs.razor.cs @@ -13,7 +13,7 @@ public sealed partial class Tabs [NotNull] private Tab? TabSet { get; set; } - private Placement BindPlacement = Placement.Top; + private Placement _bindPlacement = Placement.Top; private bool RemoveEnabled => (TabSet?.Items.Count() ?? 4) < 4; @@ -39,13 +39,13 @@ public sealed partial class Tabs private void SetPlacement(Placement placement) { - BindPlacement = placement; + _bindPlacement = placement; } - private Task AddTab(Tab tabset) + private Task AddTab(Tab tab) { - var text = $"Tab {tabset.Items.Count() + 1}"; - tabset.AddTab(new Dictionary + var text = $"Tab {tab.Items.Count() + 1}"; + tab.AddTab(new Dictionary { [nameof(TabItem.Text)] = text, [nameof(TabItem.IsActive)] = true, @@ -59,44 +59,39 @@ private Task AddTab(Tab tabset) return Task.CompletedTask; } - private static Task Active(Tab tabset) + private static Task Active(Tab tab) { - tabset.ActiveTab(0); + tab.ActiveTab(0); return Task.CompletedTask; } - private static async Task RemoveTab(Tab tabset) + private static async Task RemoveTab(Tab tab) { - if (tabset.Items.Count() > 4) + if (tab.Items.Count() > 4) { - var item = tabset.Items.Last(); - await tabset.RemoveTab(item); + var item = tab.Items.Last(); + await tab.RemoveTab(item); } } private void OnToggleDisable() { Disabled = !Disabled; - DisableText = Disabled ? "Enable" : "Disable"; } /// - /// OnAfterRenderAsync + /// /// /// - /// - protected override async Task OnAfterRenderAsync(bool firstRender) + protected override void OnAfterRender(bool firstRender) { if (firstRender) { - var menuItem = TabMenu?.Items.FirstOrDefault(); - if (menuItem != null) + var menuItem = TabMenu.Items.FirstOrDefault(); + if (menuItem != null && TabMenu.OnClick is not null) { - await InvokeAsync(() => - { - var _ = TabMenu?.OnClick?.Invoke(menuItem); - }); + TabMenu.OnClick(menuItem); } } } @@ -116,6 +111,12 @@ private Task OnClickMenuItem(MenuItem item) return Task.CompletedTask; } + private async Task OnBeforeShowContextMenu(TabItem item) + { + await Task.Yield(); + return item.IsDisabled == false; + } + private void AddTabItem(string text) => TabSetMenu.AddTab(new Dictionary { [nameof(TabItem.Text)] = text, @@ -123,11 +124,6 @@ private void AddTabItem(string text) => TabSetMenu.AddTab(new Dictionary().Render() : BootstrapDynamicComponent.CreateComponent().Render() }); - private void OnClick() - { - ShowButtons = !ShowButtons; - } - private async Task RemoveTab() { if (TabSetApp.Items.Count() > 4) @@ -171,42 +167,6 @@ private Task OnSetTitle(string text) return Task.CompletedTask; } - [NotNull] - private Tab? _tab = null; - - private Task OnRefrsh(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - _tab.Refresh(tabItem); - } - return Task.CompletedTask; - } - - private async Task OnClose(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - await _tab.RemoveTab(tabItem); - } - } - - private Task OnCloseOther(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - _tab.ActiveTab(tabItem); - } - _tab.CloseOtherTabs(); - return Task.CompletedTask; - } - - private Task OnCloseAll(ContextMenuItem item, object? context) - { - _tab.CloseAllTabs(); - return Task.CompletedTask; - } - /// /// 获得属性方法 /// diff --git a/src/BootstrapBlazor.Server/Locales/en-US.json b/src/BootstrapBlazor.Server/Locales/en-US.json index 83e167868de..38c3ac9e39a 100644 --- a/src/BootstrapBlazor.Server/Locales/en-US.json +++ b/src/BootstrapBlazor.Server/Locales/en-US.json @@ -2138,7 +2138,8 @@ "ContextCloseOther": "Close Other Tabs", "ContextCloseAll": "Close All Tabs", "TabsContextMenuTitle": "TabItem Context Menu", - "TabsContextMenuIntro": "Use the built-in ContextMenuZone component to pop up a custom context menu when you right-click a tab item" + "TabsContextMenuIntro": "Use the built-in ContextMenuZone component to pop up a custom context menu when you right-click a tab item", + "TabsContextMenuDesc": "The disabled tab can be set by setting the OnBeforeShowContextMenu callback method to control whether the right-click menu is displayed according to the return value. If it is not set, the right-click menu is also displayed for the disabled tab" }, "BootstrapBlazor.Server.Components.Components.DemoTabItem": { "Info": "Reset the title of this TabItem by click the button", diff --git a/src/BootstrapBlazor.Server/Locales/zh-CN.json b/src/BootstrapBlazor.Server/Locales/zh-CN.json index ee4b22c87e9..42580fe6dad 100644 --- a/src/BootstrapBlazor.Server/Locales/zh-CN.json +++ b/src/BootstrapBlazor.Server/Locales/zh-CN.json @@ -2138,7 +2138,8 @@ "ContextCloseOther": "关闭其他", "ContextCloseAll": "关闭全部", "TabsContextMenuTitle": "右键菜单", - "TabsContextMenuIntro": "通过内置 ContextMenuZone 组件,点击标签页右键时弹窗自定义右键菜单" + "TabsContextMenuIntro": "通过内置 ContextMenuZone 组件,点击标签页右键时弹窗自定义右键菜单", + "TabsContextMenuDesc": "被禁用的标签页可以通过设置 OnBeforeShowContextMenu 回调方法根据返回值控制右键菜单是否显示,未设置时禁用标签页也显示右键菜单" }, "BootstrapBlazor.Server.Components.Components.DemoTabItem": { "Info": "点击下方按钮,本 TabItem 标题更改为当前分钟与秒", diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor b/src/BootstrapBlazor/Components/Layout/Layout.razor index 4b5c4fe496a..75d823a6a9e 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor @@ -116,31 +116,7 @@ @
@if (UseTabSet) { - @if (ShowTabContextMenu) - { - - @RenderTab - - @if (BeforeTabContextMenuTemplate != null) - { - @BeforeTabContextMenuTemplate(_tab) - } - - - - - - @if (TabContextMenuTemplate != null) - { - @TabContextMenuTemplate(_tab) - } - - - } - else - { - @RenderTab - } + @RenderTab } else { @@ -153,6 +129,11 @@ ShowExtendButtons="ShowTabExtendButtons" ShowClose="ShowTabItemClose" AllowDrag="AllowDragTab" DefaultUrl="@TabDefaultUrl" ExcludeUrls="@ExcludeUrls" IsOnlyRenderActiveTab="IsOnlyRenderActiveTab" TabStyle="TabStyle" ShowToolbar="@ShowToolbar" ToolbarTemplate="@ToolbarTemplate" + ShowContextMenu="ShowTabContextMenu" + BeforeContextMenuTemplate="@BeforeTabContextMenuTemplate" ContextMenuTemplate="@TabContextMenuTemplate" + ContextMenuRefreshIcon="@TabContextMenuRefreshIcon" ContextMenuCloseIcon="@TabContextMenuCloseIcon" + ContextMenuCloseOtherIcon="@TabContextMenuCloseOtherIcon" ContextMenuCloseAllIcon="@TabContextMenuCloseAllIcon" + OnBeforeShowContextMenu="@OnBeforeShowContextMenu" ShowRefreshToolbarButton="ShowRefreshToolbarButton" ShowFullscreenToolbarButton="ShowFullscreenToolbarButton" RefreshToolbarButtonIcon="@RefreshToolbarButtonIcon" FullscreenToolbarButtonIcon="@FullscreenToolbarButtonIcon" RefreshToolbarTooltipText="@RefreshToolbarTooltipText" FullscreenToolbarTooltipText="@FullscreenToolbarTooltipText" diff --git a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs index 7937c288fc9..3e68e58ffad 100644 --- a/src/BootstrapBlazor/Components/Layout/Layout.razor.cs +++ b/src/BootstrapBlazor/Components/Layout/Layout.razor.cs @@ -339,6 +339,12 @@ public partial class Layout : IHandlerException [Parameter] public string? TabContextMenuCloseAllIcon { get; set; } + /// + /// Gets or sets before popup context menu callback. Default is null. + /// + [Parameter] + public Func>? OnBeforeShowContextMenu { get; set; } + [Inject] [NotNull] private NavigationManager? Navigation { get; set; } @@ -509,10 +515,6 @@ protected override void OnParametersSet() TooltipText ??= Localizer[nameof(TooltipText)]; MenuBarIcon ??= IconTheme.GetIconByKey(ComponentIcons.LayoutMenuBarIcon); - TabContextMenuRefreshIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuRefreshIcon); - TabContextMenuCloseIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseIcon); - TabContextMenuCloseOtherIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseOtherIcon); - TabContextMenuCloseAllIcon ??= IconTheme.GetIconByKey(ComponentIcons.TabContextMenuCloseAllIcon); } /// @@ -627,38 +629,6 @@ public virtual Task HandlerException(Exception ex, RenderFragment err private string? GetTargetString() => IsFixedTabHeader ? ".tabs-body" : null; - private async Task OnRefrsh(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - await _tab.Refresh(tabItem); - } - } - - private async Task OnClose(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - await _tab.RemoveTab(tabItem); - } - } - - private Task OnCloseOther(ContextMenuItem item, object? context) - { - if (context is TabItem tabItem) - { - _tab.ActiveTab(tabItem); - } - _tab.CloseOtherTabs(); - return Task.CompletedTask; - } - - private Task OnCloseAll(ContextMenuItem item, object? context) - { - _tab.CloseAllTabs(); - return Task.CompletedTask; - } - /// /// /// diff --git a/src/BootstrapBlazor/Components/Tab/Tab.razor b/src/BootstrapBlazor/Components/Tab/Tab.razor index e1fafcdbe15..a9de8d62912 100644 --- a/src/BootstrapBlazor/Components/Tab/Tab.razor +++ b/src/BootstrapBlazor/Components/Tab/Tab.razor @@ -6,9 +6,35 @@ { @Body } +else if (ShowContextMenu) +{ + + @RenderTab + + @if (BeforeContextMenuTemplate != null) + { + @BeforeContextMenuTemplate(this) + } + + + + + + @if (ContextMenuTemplate != null) + { + @ContextMenuTemplate(this) + } + + +} else { -
+ @RenderTab +} + +@code { + RenderFragment RenderTab => + @
@if (BeforeNavigatorTemplate != null) @@ -155,18 +181,17 @@ else }
-
-} - -@code { +
; + RenderFragment RenderTabItem => item => @ @RenderTabItemContent(item) ; - RenderFragment RenderDisabledHeaderItem(TabItem item) => + RenderFragment RenderDisabledHeaderItem(TabItem item) => @
-