Skip to content

Commit

Permalink
feat: add AddTabItemBindOptions service (#647)
Browse files Browse the repository at this point in the history
* feat: 增加 AddTabItemMenuBindOption 服务

* doc: 增加 ConfigureTabItemBindOptions 配置示例

* feat: 增加 TabItemMenuBindOptions 逻辑

* doc: 移除 TabItemOptionAttribute 标签

* fix: 修复地址由于 / 导致比对结果不一致问题

* test: 增加 ConfigureTabItemBindOptions 单元测试

* refactor: 重命名配置服务

* chore: bump version 7.4.2-beta04

* refactor: rename TabItemMenuBindOptions
  • Loading branch information
ArgoZhang authored Mar 8, 2023
1 parent 2c83805 commit e991637
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 31 deletions.
7 changes: 7 additions & 0 deletions src/BootstrapBlazor.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@

builder.Services.Configure<HubOptions>(option => option.MaximumReceiveMessageSize = null);

builder.Services.ConfigureTabItemMenuBindOptions(options =>
{
options.Binders.Add("layout-demo", new() { Text = "Text 1" });
options.Binders.Add("layout-demo?text=Parameter", new() { Text = "Text 2" });
options.Binders.Add("layout-demo/text=Parameter", new() { Text = "Text 3" });
});

var app = builder.Build();

// 启用本地化
Expand Down
1 change: 0 additions & 1 deletion src/BootstrapBlazor.Shared/Samples/LayoutDemo.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
@page "/layout-demo"
@page "/layout-demo/{title?}"
@layout PageLayout
@attribute [TabItemOption(Text = "示例网页", Icon = "fa-fw fa-solid fa-laptop")]

<p>测试路径:</p>

Expand Down
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>7.4.2-beta03</Version>
<Version>7.4.2-beta04</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
48 changes: 21 additions & 27 deletions src/BootstrapBlazor/Components/Tab/Tab.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/

using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Reflection;

Expand Down Expand Up @@ -213,6 +214,10 @@ public partial class Tab : IHandlerException, IDisposable
[NotNull]
private TabItemTextOptions? Options { get; set; }

[Inject]
[NotNull]
private IOptionsMonitor<TabItemBindOptions>? TabItemMenuBinder { get; set; }

private ConcurrentDictionary<TabItem, bool> LazyTabCache { get; } = new();

/// <summary>
Expand Down Expand Up @@ -455,22 +460,26 @@ private void AddTabItem(string url)
if (context.Handler != null)
{
// 检查 Options 优先
var option = context.Handler.GetCustomAttribute<TabItemOptionAttribute>(false);
if (Options.Valid())
var option = context.Handler.GetCustomAttribute<TabItemOptionAttribute>(false)
?? TabItemMenuBinder.CurrentValue.Binders
.FirstOrDefault(i => i.Key.TrimStart('/').Equals(url.TrimStart('/'), StringComparison.OrdinalIgnoreCase))
.Value;
if (option != null)
{
AddParameters(option);
parameters.Add(nameof(TabItem.Icon), option.Icon);
parameters.Add(nameof(TabItem.Closable), option.Closable);
parameters.Add(nameof(TabItem.IsActive), true);
parameters.Add(nameof(TabItem.Text), option.Text);
}
else
else if (Options.Valid())
{
if (option != null)
{
parameters.Add(nameof(TabItem.Icon), option.Icon);
parameters.Add(nameof(TabItem.Closable), option.Closable);
parameters.Add(nameof(TabItem.IsActive), true);
}
parameters.Add(nameof(TabItem.Text), option?.Text ?? url.Split("/").FirstOrDefault());
parameters.Add(nameof(TabItem.Url), url);
parameters.Add(nameof(TabItem.Icon), Options.Icon);
parameters.Add(nameof(TabItem.Closable), Options.Closable);
parameters.Add(nameof(TabItem.IsActive), Options.IsActive);
parameters.Add(nameof(TabItem.Text), Options.Text);
Options.Reset();
}
parameters.Add(nameof(TabItem.Url), url);

parameters.Add(nameof(TabItem.ChildContent), new RenderFragment(builder =>
{
Expand All @@ -491,21 +500,6 @@ private void AddTabItem(string url)
}

AddTabItem(parameters);

void AddParameters(TabItemOptionAttribute? option)
{
var text = option?.Text ?? Options.Text;
var icon = option?.Icon ?? Options.Icon ?? string.Empty;
var active = Options.IsActive;
var closable = option?.Closable ?? Options.Closable;
Options.Reset();

parameters.Add(nameof(TabItem.Url), url);
parameters.Add(nameof(TabItem.Icon), icon);
parameters.Add(nameof(TabItem.Closable), closable);
parameters.Add(nameof(TabItem.IsActive), active);
parameters.Add(nameof(TabItem.Text), text);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static IServiceCollection AddBootstrapBlazor(this IServiceCollection serv

services.ConfigureBootstrapBlazorOption(configureOptions);
services.ConfigureIPLocatorOption();

services.AddTabItemBindOptions();
return services;
}

Expand Down Expand Up @@ -140,4 +142,27 @@ public static IServiceCollection AddOptionsMonitor<TOptions>(this IServiceCollec
services.TryAddSingleton<IConfigureOptions<TOptions>, ConfigureOptions<TOptions>>();
return services;
}

/// <summary>
/// 增加 菜单与标签捆绑类配置项服务
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
static IServiceCollection AddTabItemBindOptions(this IServiceCollection services)
{
services.AddOptionsMonitor<TabItemBindOptions>();
return services;
}

/// <summary>
/// 增加第三方菜单路由与 Tab 捆绑字典配置
/// </summary>
/// <param name="services"></param>
/// <param name="configureOptions"></param>
/// <returns></returns>
public static IServiceCollection ConfigureTabItemMenuBindOptions(this IServiceCollection services, Action<TabItemBindOptions> configureOptions)
{
services.Configure(configureOptions);
return services;
}
}
16 changes: 16 additions & 0 deletions src/BootstrapBlazor/Options/TabItemMenuBindOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Argo Zhang (argo@163.com). 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/

namespace BootstrapBlazor.Components;

/// <summary>
/// 标签与菜单捆绑配置项
/// </summary>
public class TabItemBindOptions
{
/// <summary>
/// 获得/设置 集合
/// </summary>
public Dictionary<string, TabItemOptionAttribute> Binders { get; set; } = new();
}
15 changes: 13 additions & 2 deletions test/UnitTest/Components/TabTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void TabItem_HeaderTemplate()
}

[Fact]
public async Task TabItemOption_Ok()
public void TabItemOptions_Ok()
{
var cut = Context.RenderComponent<Tab>(pb =>
{
Expand All @@ -535,8 +535,19 @@ public async Task TabItemOption_Ok()
pb.Add(a => a.ShowClose, true);
pb.Add(a => a.DefaultUrl, "/Dog");
});
await cut.InvokeAsync(() => cut.Instance.AddTab("/Dog", "Dog"));
cut.InvokeAsync(() => cut.Instance.AddTab("/Dog", "Dog"));
var tabItem = cut.FindComponents<DynamicElement>().First(i => i.Markup.Contains("Dog"));
Assert.DoesNotContain("tabs-item-close", tabItem.Markup);
}

[Fact]
public void TabItemMenuBinder_Ok()
{
var cut = Context.RenderComponent<Tab>(pb =>
{
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
pb.Add(a => a.DefaultUrl, "/Binder");
});
cut.Contains("Index_Binder_Test");
}
}
7 changes: 7 additions & 0 deletions test/UnitTest/Core/TabTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ protected virtual void ConfigureServices(IServiceCollection services)
{
services.AddBootstrapBlazor(op => op.ToastDelay = 2000);
services.ConfigureJsonLocalizationOptions(op => op.AdditionalJsonAssemblies = new[] { typeof(Alert).Assembly });
services.ConfigureTabItemMenuBindOptions(options =>
{
options.Binders = new()
{
{ "/Binder", new() { Text = "Index_Binder_Test" } }
};
});
}

protected virtual void ConfigureConfigration(IServiceCollection services)
Expand Down
16 changes: 16 additions & 0 deletions test/UnitTest/Pages/Binder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Argo Zhang (argo@163.com). 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/

using Microsoft.AspNetCore.Components.Rendering;

namespace UnitTest.Pages;

[Route("/Binder")]
public class Binder : ComponentBase
{
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
builder.AddContent(0, "Binder");
}
}

0 comments on commit e991637

Please sign in to comment.