Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Tab): not update url when close the latest tab item #4477

Merged
merged 13 commits into from
Oct 17, 2024
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.10.3</Version>
<Version>8.10.4-beta01</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/BootstrapBlazor/Components/Layout/Layout.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ public partial class Layout : IHandlerException
public bool IsFullSide { get; set; }

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

Expand Down
9 changes: 8 additions & 1 deletion src/BootstrapBlazor/Components/Tab/Tab.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ else
<RenderTemplate>
@if (!Items.Any() && !string.IsNullOrEmpty(DefaultUrl))
{
AddTabItem(DefaultUrl);
if (ClickTabToNavigation)
{
Navigator.NavigateTo(DefaultUrl);
}
else
{
AddTabItem(DefaultUrl);
}
}
@if (FirstRender)
{
Expand Down
13 changes: 5 additions & 8 deletions src/BootstrapBlazor/Components/Tab/Tab.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Copyright (c) Argo Zhang (argo@live.ca). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

Expand Down Expand Up @@ -600,15 +600,10 @@ private void AddTabItem(string url)
SetTabItemParameters(Options.Text, Options.Icon, Options.Closable, Options.IsActive);
Options.Reset();
}
else if (Layout != null)
{
// CascadeParameter Menus
var menu = GetMenuItem(url);
SetTabItemParameters(menu?.Text, menu?.Icon, true, true);
}
else
{
parameters.Add(nameof(TabItem.Text), url.Split("/").FirstOrDefault());
var menu = GetMenuItem(url) ?? new MenuItem() { Text = url.Split("/").FirstOrDefault() };
SetTabItemParameters(menu.Text, menu.Icon, true, true);
}
parameters.Add(nameof(TabItem.Url), url);

Expand Down Expand Up @@ -679,6 +674,8 @@ private void AddTabItem(Dictionary<string, object?> parameters, int? index = nul
/// <param name="item"></param>
public async Task RemoveTab(TabItem item)
{
Options.Reset();

if (OnCloseTabItemAsync != null && !await OnCloseTabItemAsync(item))
{
return;
Expand Down
34 changes: 17 additions & 17 deletions test/UnitTest/Components/TabTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ await cut.InvokeAsync(() => tab.AddTab(new Dictionary<string, object?>
}

[Fact]
public void AddTabByUrl_Ok()
public async Task AddTabByUrl_Ok()
{
var navMan = Context.Services.GetRequiredService<FakeNavigationManager>();
navMan.NavigateTo("/");
Expand Down Expand Up @@ -290,7 +290,7 @@ public void AddTabByUrl_Ok()
});

navMan.NavigateTo("/");
cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
await cut.InvokeAsync(() => cut.Instance.AddTab(new Dictionary<string, object?>
{
["Text"] = "Cat",
["Url"] = "Cat"
Expand All @@ -299,10 +299,10 @@ public void AddTabByUrl_Ok()
{
pb.Add(a => a.ExcludeUrls, ["/Test"]);
});
cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());
await cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());

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

cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
item = cut.Instance.GetActiveTab();
Assert.NotNull(item);
Assert.Equal("Cat", item.Url);

cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
item = cut.Instance.GetActiveTab();
Assert.Null(item);

await cut.InvokeAsync(() => cut.Instance.CloseCurrentTab());
}

[Fact]
Expand Down Expand Up @@ -534,26 +536,24 @@ public void AlwaysLoad_Ok()
}

[Fact]
public void ActiveTab_Ok()
public async Task ActiveTab_Ok()
{
var cut = Context.RenderComponent<Tab>(pb =>
{
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
pb.Add(a => a.DefaultUrl, "/");
});
cut.InvokeAsync(() => cut.Instance.ActiveTab(0));
await cut.InvokeAsync(() => cut.Instance.ActiveTab(0));

var item = cut.Instance.GetActiveTab();
Assert.NotNull(item);
cut.InvokeAsync(() =>
{
if (item != null)
{
cut.Instance.ActiveTab(item);
}
});
cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));

await cut.InvokeAsync(() => cut.Instance.ActiveTab(item));

// 移除标签导航到默认标签
await cut.InvokeAsync(() => cut.Instance.RemoveTab(item!));
item = cut.Instance.GetActiveTab();
Assert.Null(item);
Assert.NotNull(item);
}

[Fact]
Expand Down
9 changes: 4 additions & 5 deletions test/UnitTestDocs/MenuTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

using BootstrapBlazor.Localization.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand All @@ -13,9 +12,9 @@ namespace UnitTestDocs;

public partial class MenuTest
{
private ITestOutputHelper _logger;
private IServiceProvider _serviceProvider;
private IEnumerable<Type> _routerTable;
private readonly ITestOutputHelper _logger;
private readonly IServiceProvider _serviceProvider;
private readonly IEnumerable<Type> _routerTable;

public MenuTest(ITestOutputHelper logger)
{
Expand Down Expand Up @@ -149,7 +148,7 @@ static string ReplacePayload(string payload, LocalizedString l) => payload

static string RemoveBlockStatement(string payload, string removeString)
{
var index = payload.IndexOf(removeString);
var index = payload.IndexOf(removeString, StringComparison.Ordinal);
if (index > -1)
{
var end = payload.IndexOf("\n", index, StringComparison.OrdinalIgnoreCase);
Expand Down