Skip to content

Commit 76d63c8

Browse files
authored
feat(Tab): add Refresh method (#5699)
* style: 更新样式 * refactor: 增加 Refresh 方法 * doc: 增加右键菜单刷新示例 * refactor: 重构代码 * test: 更新单元测试 * chore: bump version 9.5.0-beta13
1 parent d95dca4 commit 76d63c8

File tree

9 files changed

+39
-11
lines changed

9 files changed

+39
-11
lines changed

src/BootstrapBlazor.Server/Components/Samples/Tabs.razor

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ private void Navigation()
501501
<Tab IsCard="true" ShowClose="true" TabStyle="TabStyle.Chrome" ShowToolbar="true" @ref="_tab">
502502
<TabItem Text="@Localizer["TabItem1Text"]" Icon="fa-solid fa-user">
503503
<div>@Localizer["TabItem1Content"]</div>
504+
<Counter></Counter>
504505
</TabItem>
505506
<TabItem Text="@Localizer["TabItem2Text"]" Icon="fa-solid fa-gauge-high">
506507
<div>@Localizer["TabItem2Content"]</div>
@@ -513,6 +514,8 @@ private void Navigation()
513514
</TabItem>
514515
</Tab>
515516
<ContextMenu>
517+
<ContextMenuItem Icon="fa-solid fa-rotate-right" Text="@Localizer["ContextRefresh"]" OnClick="OnRefrsh"></ContextMenuItem>
518+
<ContextMenuDivider></ContextMenuDivider>
516519
<ContextMenuItem Icon="fa-solid fa-xmark" Text="@Localizer["ContextClose"]" OnClick="OnClose"></ContextMenuItem>
517520
<ContextMenuItem Icon="fa-solid fa-left-right" Text="@Localizer["ContextCloseOther"]" OnClick="OnCloseOther"></ContextMenuItem>
518521
<ContextMenuItem Icon="fa-solid fa-arrows-left-right-to-line" Text="@Localizer["ContextCloseAll"]" OnClick="OnCloseAll"></ContextMenuItem>

src/BootstrapBlazor.Server/Components/Samples/Tabs.razor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ private Task OnSetTitle(string text)
174174
[NotNull]
175175
private Tab? _tab = null;
176176

177+
private Task OnRefrsh(ContextMenuItem item, object? context)
178+
{
179+
if (context is TabItem tabItem)
180+
{
181+
_tab.Refresh(tabItem);
182+
}
183+
return Task.CompletedTask;
184+
}
185+
177186
private async Task OnClose(ContextMenuItem item, object? context)
178187
{
179188
if (context is TabItem tabItem)

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@
21332133
"AttributeFullscreenToolbarButtonIcon": "Toolbar full screen button icon",
21342134
"TabsToolbarDesc": "After clicking the button, the counter value increases, and clicking the <b>Refresh</b> button on the toolbar will reset the counter.",
21352135
"AttributeOnToolbarRefreshCallback": "Click the toolbar refresh button callback method",
2136+
"ContextRefresh": "Refresh",
21362137
"ContextClose": "Close",
21372138
"ContextCloseOther": "Close Other Tabs",
21382139
"ContextCloseAll": "Close All Tabs",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@
21332133
"AttributeFullscreenToolbarButtonIcon": "工具栏全屏按钮图标",
21342134
"TabsToolbarDesc": "点击按钮计数器数值增加后,点击工具栏的 <b>刷新</b> 按钮计数器清零",
21352135
"AttributeOnToolbarRefreshCallback": "点击工具栏刷新按钮回调方法",
2136+
"ContextRefresh": "刷新",
21362137
"ContextClose": "关闭",
21372138
"ContextCloseOther": "关闭其他",
21382139
"ContextCloseAll": "关闭全部",

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.5.0-beta12</Version>
4+
<Version>9.5.0-beta13</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88
min-height: var(--bb-cm-icon-min-height);
99
display: inline-block;
1010
}
11+
12+
.divider {
13+
margin: 0.5rem 0;
14+
}
1115
}

src/BootstrapBlazor/Components/Tab/Tab.razor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,19 @@ private async Task OnRefreshAsync()
950950
{
951951
// refresh the active tab item
952952
var item = TabItems.FirstOrDefault(i => i.IsActive);
953+
954+
if (item is not null)
955+
{
956+
await Refresh(item);
957+
}
958+
}
959+
960+
/// <summary>
961+
/// Refresh the tab item method
962+
/// </summary>
963+
/// <param name="item"></param>
964+
public async Task Refresh(TabItem item)
965+
{
953966
item.Refresh(_cache);
954967

955968
if (OnToolbarRefreshCallback != null)

src/BootstrapBlazor/Extensions/TabItemExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public static RenderFragment RenderContent(this TabItem item, ConcurrentDictiona
2424
builder.CloseComponent();
2525
};
2626

27-
public static void Refresh(this TabItem? item, ConcurrentDictionary<TabItem, TabItemContent> cache)
27+
public static void Refresh(this TabItem item, ConcurrentDictionary<TabItem, TabItemContent> cache)
2828
{
29-
if (item is not null && cache.TryGetValue(item, out var content))
29+
if (cache.TryGetValue(item, out var content))
3030
{
3131
content.Render();
3232
}

test/UnitTest/Components/TabTest.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Bunit.TestDoubles;
88
using Microsoft.AspNetCore.Components.Rendering;
99
using System.Reflection;
10-
using System.Threading.Tasks;
1110
using UnitTest.Misc;
1211

1312
namespace UnitTest.Components;
@@ -1100,6 +1099,11 @@ public async Task ShowToolbar_Ok()
11001099
await cut.InvokeAsync(() => button.Click());
11011100
Assert.True(clicked);
11021101

1102+
clicked = false;
1103+
var item = cut.FindComponent<TabItem>();
1104+
await cut.InvokeAsync(() => tab.Instance.Refresh(item.Instance));
1105+
Assert.True(clicked);
1106+
11031107
tab.SetParametersAndRender(pb =>
11041108
{
11051109
pb.Add(a => a.ShowRefreshToolbarButton, false);
@@ -1111,13 +1115,6 @@ public async Task ShowToolbar_Ok()
11111115
pb.Add(a => a.ShowFullscreenToolbarButton, false);
11121116
});
11131117
cut.DoesNotContain("tabs-nav-toolbar-fs");
1114-
1115-
// 利用反射提高代码覆盖率
1116-
var type = Type.GetType("BootstrapBlazor.Components.TabItemExtensions, BootstrapBlazor");
1117-
Assert.NotNull(type);
1118-
var mi = type.GetMethod("Refresh", BindingFlags.Static | BindingFlags.Public);
1119-
Assert.NotNull(mi);
1120-
mi.Invoke(null, [null, null]);
11211118
}
11221119

11231120
class DisableTabItemButton : ComponentBase

0 commit comments

Comments
 (0)