!2263 refactor(#I4OJRD): redesign Tab component

* doc: 格式化文档
* refactor: 更新 TabLink 内部逻辑
This commit is contained in:
Argo 2021-12-29 04:44:06 +00:00
parent 4278593c31
commit d9c5f0cfbe
3 changed files with 43 additions and 25 deletions

View File

@ -1,7 +1,4 @@
@namespace BootstrapBlazor.Components @namespace BootstrapBlazor.Components
@inherits BootstrapComponentBase @inherits BootstrapComponentBase
@if (ChildContent != null) <a @attributes="@AdditionalAttributes" href="@Url" @onclick="@OnClickLink">@RenderChildContent()</a>
{
<a @attributes="@AdditionalAttributes" href="@Url" @onclick="@OnClickLink">@ChildContent</a>
}

View File

@ -65,5 +65,27 @@ namespace BootstrapBlazor.Components
TabItemOptions.Text = Text; TabItemOptions.Text = Text;
TabItemOptions.Closable = Closable; TabItemOptions.Closable = Closable;
} }
private RenderFragment RenderChildContent() => builder =>
{
var index = 0;
if (ChildContent == null)
{
if (!string.IsNullOrEmpty(Icon))
{
builder.OpenElement(index++, "i");
builder.AddAttribute(index++, "class", Icon);
builder.CloseElement();
}
if (!string.IsNullOrEmpty(Text))
{
builder.AddContent(index++, Text);
}
}
else
{
builder.AddContent(index++, ChildContent);
}
};
} }
} }

View File

@ -6,30 +6,29 @@ using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
namespace BootstrapBlazor.Components namespace BootstrapBlazor.Components;
/// <summary>
/// NavigationManager 扩展方法
/// </summary>
public static class NavigationManagerExtensions
{ {
/// <summary> /// <summary>
/// /// 导航并添加 TabItem 方法
/// </summary> /// </summary>
public static class NavigationManagerExtensions /// <param name="navigation"></param>
/// <param name="provider"></param>
/// <param name="url"></param>
/// <param name="text"></param>
/// <param name="icon"></param>
/// <param name="closable"></param>
public static void NavigateTo(this NavigationManager navigation, IServiceProvider provider, string url, string text, string? icon = null, bool? closable = null)
{ {
/// <summary> var option = provider.GetRequiredService<TabItemTextOptions>();
/// 导航并添加 TabItem 方法 option.Text = text;
/// </summary> option.Icon = icon;
/// <param name="navigation"></param> option.IsActive = true;
/// <param name="provider"></param> option.Closable = closable ?? true;
/// <param name="url"></param> navigation.NavigateTo(url);
/// <param name="text"></param>
/// <param name="icon"></param>
/// <param name="closable"></param>
public static void NavigateTo(this NavigationManager navigation, IServiceProvider provider, string url, string text, string? icon = null, bool? closable = null)
{
var option = provider.GetRequiredService<TabItemTextOptions>();
option.Text = text;
option.Icon = icon;
option.IsActive = true;
option.Closable = closable ?? true;
navigation.NavigateTo(url);
}
} }
} }