Skip to content

Commit d1579e1

Browse files
authored
feat(Tab): add IsLoopSwitchTabItem parameter controll switch tab item behavior (#5844)
* feat: 增加 IsAutoLoopTabItem 参数 * doc: 更新文档 * chore: bump version 9.5.8-beta01 * test: 增加单元测试
1 parent 2cc46ca commit d1579e1

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ private AttributeItem[] GetAttributes() =>
202202
DefaultValue = "false"
203203
},
204204
new()
205+
{
206+
Name = nameof(Tab.IsLoopSwitchTabItem),
207+
Description = Localizer["TabAttIsLoopSwitchTabItem"].Value,
208+
Type = "boolean",
209+
ValueList = "true/false",
210+
DefaultValue = "true"
211+
},
212+
new()
205213
{
206214
Name = "ShowClose",
207215
Description = Localizer["TabAtt4ShowClose"].Value,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@
20632063
"Block9Div": "Header",
20642064
"TabsAppTitle": "Live Tab components",
20652065
"TabsAppIntro": "By setting the <code>ShowExtendButtons</code> property to <code>true</code>, turning on the left and right buttons of the component and closing the drop-down menu, it is very useful in practice",
2066-
"TabsAppDescription": "Dynamically adjust the number of <code>TabItem</code> by <b>adding</b>, <b>delete</b>buttons to view the left and right effects beyond the number of containers, <b>user management</b> is set to not close, and the feature button cannot close this tab",
2066+
"TabsAppDescription": "Dynamically adjust the number of <code>TabItem</code> by <b>adding</b>, <b>delete</b>buttons to view the left and right effects beyond the number of containers, <b>user management</b> is set to not close, and the feature button cannot close this tab. The component switches tabs in a loop by default. You can disable this feature by <code>IsLoopSwitchTabItem=\"false\"</code>",
20672067
"TabsIsOnlyRenderActiveTitle": "Only the current label is rendered",
20682068
"TabsIsOnlyRenderActiveIntro": "By setting the <code>isOnlyRenderActiveTab</code> parameter, the component renders only the current active label",
20692069
"BlockSetTextTitle": "TabItem Text",
@@ -2090,6 +2090,7 @@
20902090
"TabAttShowNavigatorButtons": "Whether to display the previous and next navigation buttons",
20912091
"TabAttShowActiveBar": "Whether to display active bar",
20922092
"TabAttIsLazyLoadTabItem": "Whether lazy load tab item",
2093+
"TabAttIsLoopSwitchTabItem": "Whether loop switch tab item",
20932094
"TabAtt6ClickTabToNavigation": "Whether to navigate when you click on the title",
20942095
"TabAtt7Placement": "Set the label position",
20952096
"TabAtt8Height": "Set the label height",

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,7 @@
20632063
"Block9Div": "网站 Header",
20642064
"TabsAppTitle": "实战 Tab 组件",
20652065
"TabsAppIntro": "通过设置 <code>ShowExtendButtons</code> 属性为 <code>true</code>,开启组件左右移动按钮与关闭下拉菜单,实战中非常实用",
2066-
"TabsAppDescription": "通过 <b>添加</b> <b>删除</b> 按钮动态调整 <code>TabItem</code> 数量,使其超出容器数量查看,左右移动效果;<b>用户管理</b> 设置为不可关闭;功能按钮无法关闭这个标签页",
2066+
"TabsAppDescription": "通过 <b>添加</b> <b>删除</b> 按钮动态调整 <code>TabItem</code> 数量,使其超出容器数量查看,左右移动效果;<b>用户管理</b> 设置为不可关闭;功能按钮无法关闭这个标签页。组件默认是循环切换标签页的,可以通过 <code>IsLoopSwitchTabItem=\"false\"</code> 关闭这个功能",
20672067
"TabsIsOnlyRenderActiveTitle": "仅渲染当前标签",
20682068
"TabsIsOnlyRenderActiveIntro": "通过设置 <code>IsOnlyRenderActiveTab</code> 参数使组件仅渲染当前活动标签",
20692069
"BlockSetTextTitle": "设置标签文本",
@@ -2090,6 +2090,7 @@
20902090
"TabAttShowNavigatorButtons": "是否显示前后导航按钮",
20912091
"TabAttShowActiveBar": "是否显示活动标签",
20922092
"TabAttIsLazyLoadTabItem": "是否延时加载标签内容",
2093+
"TabAttIsLoopSwitchTabItem": "是否循环切换标签",
20932094
"TabAtt6ClickTabToNavigation": "点击标题时是否导航",
20942095
"TabAtt7Placement": "设置标签位置",
20952096
"TabAtt8Height": "设置标签高度",

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.7</Version>
4+
<Version>9.5.8-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ public partial class Tab : IHandlerException
139139
[Parameter]
140140
public bool ShowNavigatorButtons { get; set; } = true;
141141

142+
/// <summary>
143+
/// Gets or sets whether auto reset tab item index. Default is true.
144+
/// </summary>
145+
[Parameter]
146+
public bool IsLoopSwitchTabItem { get; set; } = true;
147+
142148
/// <summary>
143149
/// 获得/设置 是否显示活动标签 默认为 true 显示
144150
/// </summary>
@@ -650,7 +656,14 @@ public void ClickPrevTab()
650656
index--;
651657
if (index < 0)
652658
{
653-
index = TabItems.Count - 1;
659+
if (IsLoopSwitchTabItem)
660+
{
661+
index = TabItems.Count - 1;
662+
}
663+
else
664+
{
665+
return;
666+
}
654667
}
655668

656669
if (!ClickTabToNavigation)
@@ -683,15 +696,22 @@ public void ClickNextTab()
683696
var index = TabItems.IndexOf(item);
684697
if (index < TabItems.Count)
685698
{
686-
if (!ClickTabToNavigation)
699+
index++;
700+
if (index + 1 > TabItems.Count)
687701
{
688-
item.SetActive(false);
702+
if (IsLoopSwitchTabItem)
703+
{
704+
index = 0;
705+
}
706+
else
707+
{
708+
return;
709+
}
689710
}
690711

691-
index++;
692-
if (index + 1 > TabItems.Count)
712+
if (!ClickTabToNavigation)
693713
{
694-
index = 0;
714+
item.SetActive(false);
695715
}
696716

697717
item = TabItems[index];

test/UnitTest/Components/TabTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,42 @@ public void IsBorderCard_Ok()
171171
Assert.Contains("tabs-border-card", cut.Markup);
172172
}
173173

174+
[Fact]
175+
public async Task IsLoopSwitchTabItem_Ok()
176+
{
177+
var cut = Context.RenderComponent<Tab>(pb =>
178+
{
179+
pb.Add(a => a.ShowExtendButtons, true);
180+
pb.Add(a => a.IsLoopSwitchTabItem, false);
181+
pb.AddChildContent<TabItem>(pb =>
182+
{
183+
pb.Add(a => a.Text, "Tab1");
184+
pb.Add(a => a.Url, "/Index");
185+
pb.Add(a => a.ChildContent, "Tab1-Content");
186+
});
187+
pb.AddChildContent<TabItem>(pb =>
188+
{
189+
pb.Add(a => a.Text, "Tab2");
190+
pb.Add(a => a.Url, "/");
191+
pb.Add(a => a.ChildContent, "Tab2-Content");
192+
});
193+
});
194+
Assert.Equal("Tab2-Content", cut.Find(".tabs-body .d-none").InnerHtml);
195+
196+
// Click Prev
197+
var button = cut.Find(".nav-link-bar.left .nav-link-bar-button");
198+
await cut.InvokeAsync(() => button.Click());
199+
Assert.Equal("Tab2-Content", cut.Find(".tabs-body .d-none").InnerHtml);
200+
201+
// Click Next
202+
button = cut.Find(".nav-link-bar.right .nav-link-bar-button");
203+
await cut.InvokeAsync(() => button.Click());
204+
Assert.Equal("Tab1-Content", cut.Find(".tabs-body .d-none").InnerHtml);
205+
206+
await cut.InvokeAsync(() => button.Click());
207+
Assert.Equal("Tab1-Content", cut.Find(".tabs-body .d-none").InnerHtml);
208+
}
209+
174210
[Fact]
175211
public void ClickTab_Ok()
176212
{

0 commit comments

Comments
 (0)