Skip to content

Commit d76464a

Browse files
fix(Tab): not update url when close the latest tab item (#4477)
* fix(tab): 导航Tab 当关闭最后一个标签时,跳转默认url * 添加demo * doc: 增加组件注释 * chore: 更新格式 * refactor: 重构代码 * refactor: 更新逻辑 * refactor: 重构代码 * refactor: 删除标签页时清空菜单配置项参数 * test: 更新单元测试 * test: 更新单元测试 * chore: 移除测试工程 * chore: bump version 8.10.4-beta01 --------- Co-authored-by: Diego <[email protected]>
1 parent be300a8 commit d76464a

File tree

6 files changed

+37
-33
lines changed

6 files changed

+37
-33
lines changed

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

77
<ItemGroup>

src/BootstrapBlazor/Components/Layout/Layout.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ public partial class Layout : IHandlerException
104104
public bool IsFullSide { get; set; }
105105

106106
/// <summary>
107-
/// 获得/设置 是否为正页面布局 默认为 false
107+
/// 获得/设置 是否为整页面布局 默认为 false
108108
/// </summary>
109+
/// <remarks>为真时增加 is-page 样式</remarks>
109110
[Parameter]
110111
public bool IsPage { get; set; }
111112

src/BootstrapBlazor/Components/Tab/Tab.razor

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ else
2020
<RenderTemplate>
2121
@if (!Items.Any() && !string.IsNullOrEmpty(DefaultUrl))
2222
{
23-
AddTabItem(DefaultUrl);
23+
if (ClickTabToNavigation)
24+
{
25+
Navigator.NavigateTo(DefaultUrl);
26+
}
27+
else
28+
{
29+
AddTabItem(DefaultUrl);
30+
}
2431
}
2532
@if (FirstRender)
2633
{

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
1+
// Copyright (c) Argo Zhang (argo@live.ca). All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

@@ -600,15 +600,10 @@ private void AddTabItem(string url)
600600
SetTabItemParameters(Options.Text, Options.Icon, Options.Closable, Options.IsActive);
601601
Options.Reset();
602602
}
603-
else if (Layout != null)
604-
{
605-
// CascadeParameter Menus
606-
var menu = GetMenuItem(url);
607-
SetTabItemParameters(menu?.Text, menu?.Icon, true, true);
608-
}
609603
else
610604
{
611-
parameters.Add(nameof(TabItem.Text), url.Split("/").FirstOrDefault());
605+
var menu = GetMenuItem(url) ?? new MenuItem() { Text = url.Split("/").FirstOrDefault() };
606+
SetTabItemParameters(menu.Text, menu.Icon, true, true);
612607
}
613608
parameters.Add(nameof(TabItem.Url), url);
614609

@@ -679,6 +674,8 @@ private void AddTabItem(Dictionary<string, object?> parameters, int? index = nul
679674
/// <param name="item"></param>
680675
public async Task RemoveTab(TabItem item)
681676
{
677+
Options.Reset();
678+
682679
if (OnCloseTabItemAsync != null && !await OnCloseTabItemAsync(item))
683680
{
684681
return;

test/UnitTest/Components/TabTest.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ await cut.InvokeAsync(() => tab.AddTab(new Dictionary<string, object?>
261261
}
262262

263263
[Fact]
264-
public void AddTabByUrl_Ok()
264+
public async Task AddTabByUrl_Ok()
265265
{
266266
var navMan = Context.Services.GetRequiredService<FakeNavigationManager>();
267267
navMan.NavigateTo("/");
@@ -290,7 +290,7 @@ public void AddTabByUrl_Ok()
290290
});
291291

292292
navMan.NavigateTo("/");
293-
cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
293+
await cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
294294
{
295295
["Text"] = "Cat",
296296
["Url"] = "Cat"
@@ -299,10 +299,10 @@ public void AddTabByUrl_Ok()
299299
{
300300
pb.Add(a => a.ExcludeUrls, ["/Test"]);
301301
});
302-
cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());
302+
await cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());
303303

304304
// AddTab
305-
cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
305+
await cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
306306
{
307307
["Text"] = "Cat",
308308
["Url"] = null,
@@ -314,14 +314,16 @@ public void AddTabByUrl_Ok()
314314
Assert.NotNull(item);
315315
Assert.Equal("", item.Url);
316316

317-
cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
317+
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
318318
item = cut.Instance.GetActiveTab();
319319
Assert.NotNull(item);
320320
Assert.Equal("Cat", item.Url);
321321

322-
cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
322+
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
323323
item = cut.Instance.GetActiveTab();
324324
Assert.Null(item);
325+
326+
await cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());
325327
}
326328

327329
[Fact]
@@ -534,26 +536,24 @@ public void AlwaysLoad_Ok()
534536
}
535537

536538
[Fact]
537-
public void ActiveTab_Ok()
539+
public async Task ActiveTab_Ok()
538540
{
539541
var cut = Context.RenderComponent<Tab>(pb =>
540542
{
541543
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
542544
pb.Add(a => a.DefaultUrl, "/");
543545
});
544-
cut.InvokeAsync(() => cut.Instance.ActiveTab(0));
546+
await cut.InvokeAsync(() => cut.Instance.ActiveTab(0));
547+
545548
var item = cut.Instance.GetActiveTab();
546549
Assert.NotNull(item);
547-
cut.InvokeAsync(() =>
548-
{
549-
if (item != null)
550-
{
551-
cut.Instance.ActiveTab(item);
552-
}
553-
});
554-
cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
550+
551+
await cut.InvokeAsync(() => cut.Instance.ActiveTab(item));
552+
553+
// 移除标签导航到默认标签
554+
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
555555
item = cut.Instance.GetActiveTab();
556-
Assert.Null(item);
556+
Assert.NotNull(item);
557557
}
558558

559559
[Fact]

test/UnitTestDocs/MenuTest.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33
// Website: https://www.blazor.zone or https://argozhang.github.io/
44

5-
using BootstrapBlazor.Localization.Json;
65
using Microsoft.Extensions.Configuration;
76
using Microsoft.Extensions.DependencyInjection;
87
using Microsoft.Extensions.Options;
@@ -13,9 +12,9 @@ namespace UnitTestDocs;
1312

1413
public partial class MenuTest
1514
{
16-
private ITestOutputHelper _logger;
17-
private IServiceProvider _serviceProvider;
18-
private IEnumerable<Type> _routerTable;
15+
private readonly ITestOutputHelper _logger;
16+
private readonly IServiceProvider _serviceProvider;
17+
private readonly IEnumerable<Type> _routerTable;
1918

2019
public MenuTest(ITestOutputHelper logger)
2120
{
@@ -149,7 +148,7 @@ static string ReplacePayload(string payload, LocalizedString l) => payload
149148

150149
static string RemoveBlockStatement(string payload, string removeString)
151150
{
152-
var index = payload.IndexOf(removeString);
151+
var index = payload.IndexOf(removeString, StringComparison.Ordinal);
153152
if (index > -1)
154153
{
155154
var end = payload.IndexOf("\n", index, StringComparison.OrdinalIgnoreCase);

0 commit comments

Comments
 (0)