!2644 feat(#I51TFF): add SetText method on TabItem instance

* test: 补充单元测试
* feat: 增加 SetText 实例方法
This commit is contained in:
Argo 2022-04-10 18:56:14 +00:00
parent 9af8fea3eb
commit 2602b93c1b
4 changed files with 70 additions and 19 deletions

View File

@ -24,9 +24,12 @@ else
{
<div class="tabs-active-bar"></div>
}
<CascadingValue Value="this" IsFixed="true">
@ChildContent
</CascadingValue>
@if (FirstRender)
{
<CascadingValue Value="this" IsFixed="true">
@ChildContent
</CascadingValue>
}
<RenderTemplate>
@if (!Items.Any() && !string.IsNullOrEmpty(DefaultUrl))
{
@ -58,7 +61,7 @@ else
</RenderTemplate>
</div>
</div>
@if(ButtonTemplate != null)
@if (ButtonTemplate != null)
{
@ButtonTemplate
}
@ -83,25 +86,29 @@ else
<div class="tabs-body">
<CascadingValue Value="this" IsFixed="true">
@if (IsOnlyRenderActiveTab)
{
var item = Items.FirstOrDefault(i => i.IsActive);
if (item != null)
{
var item = Items.FirstOrDefault(i => i.IsActive);
if (item != null)
{
<div class="@GetContentClassString(item)">
<div class="@GetContentClassString(item)">
<CascadingValue Value="item" IsFixed="true">
@RenderTabItemContent(item.ChildContent)
</div>
}
</CascadingValue>
</div>
}
else
}
else
{
foreach (var item in Items)
{
foreach (var item in Items)
{
<div @key="@item" class="@GetContentClassString(item)">
<div @key="@item" class="@GetContentClassString(item)">
<CascadingValue Value="item" IsFixed="true">
@RenderTabItemContent(item.ChildContent)
</div>
}
</CascadingValue>
</div>
}
</CascadingValue>
</div>
}
</CascadingValue>
</div>
</div>
}

View File

@ -528,6 +528,7 @@ public partial class Tab
private void AddTabItem(Dictionary<string, object?> parameters)
{
var item = TabItem.Create(parameters);
item.TabSet = this;
if (item.IsActive)
{
_items.ForEach(i => i.SetActive(false));

View File

@ -64,7 +64,7 @@ public class TabItem : ComponentBase
/// 获得/设置 所属 Tab 实例
/// </summary>
[CascadingParameter]
protected Tab? TabSet { get; set; }
protected internal Tab? TabSet { get; set; }
/// <summary>
/// OnInitialized 方法
@ -83,6 +83,30 @@ public class TabItem : ComponentBase
/// <param name="active"></param>
public virtual void SetActive(bool active) => IsActive = active;
/// <summary>
/// 重新设置标签文字等参数
/// </summary>
/// <param name="text"></param>
/// <param name="icon"></param>
/// <param name="closable"></param>
public void SetText(string text, string? icon = null, bool? closable = null)
{
if (TabSet != null)
{
Text = text;
if (!string.IsNullOrEmpty(icon))
{
Icon = icon;
}
if (closable.HasValue)
{
Closable = closable.Value;
}
TabSet.ActiveTab(this);
}
}
/// <summary>
/// 通过指定参数集合获取 TabItem 实例
/// </summary>

View File

@ -346,4 +346,23 @@ public class TabTest : BootstrapBlazorTestBase
});
cut.Contains("<div>test-button</div>");
}
[Fact]
public async Task SetText_Ok()
{
var cut = Context.RenderComponent<Tab>(pb =>
{
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
pb.Add(a => a.ClickTabToNavigation, true);
pb.AddChildContent<TabItem>(pb =>
{
pb.Add(a => a.Text, "Tab1");
pb.Add(a => a.Url, "/Cat");
});
});
cut.Contains("<span class=\"tabs-item-text\">Tab1</span>");
var item = cut.FindComponent<TabItem>();
await cut.InvokeAsync(() => item.Instance.SetText("Text", "fa fa-fa", true));
cut.Contains("<span class=\"tabs-item-text\">Text</span>");
}
}