Skip to content

Commit 5982ba8

Browse files
committed
refactor: 更新 TabItem Body 渲染逻辑
1 parent aa70582 commit 5982ba8

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/BootstrapBlazor/Components/Tab/Tab.razor

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ else
125125
{
126126
foreach (var item in Items)
127127
{
128-
<div @key="@item" class="@GetContentClassString(item)" id="@GetIdByTabItem(item)">
129-
@RenderTabItem(item)
130-
</div>
128+
@RenderTabItem(item)
131129
}
132130
}
133131
</CascadingValue>

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ public partial class Tab : IHandlerException
1616
{
1717
private bool FirstRender { get; set; } = true;
1818

19-
private static string? GetContentClassString(TabItem item) => CssBuilder.Default("tabs-body-content")
20-
.AddClass("d-none", !item.IsActive)
21-
.Build();
22-
2319
private string? WrapClassString => CssBuilder.Default("tabs-nav-wrap")
2420
.AddClass("extend", ShouldShowExtendButtons())
2521
.Build();

src/BootstrapBlazor/Components/Tab/TabItemContent.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,15 @@ internal class TabItemContent : IComponent
1212
/// <summary>
1313
/// Gets or sets the component content. Default is null
1414
/// </summary>
15-
[Parameter]
16-
public RenderFragment? ChildContent { get; set; }
15+
[Parameter, NotNull]
16+
public TabItem? Item { get; set; }
17+
18+
/// <summary>
19+
/// Gets <see cref="IComponentIdGenerator"/> instrance
20+
/// </summary>
21+
[Inject]
22+
[NotNull]
23+
private IComponentIdGenerator? ComponentIdGenerator { get; set; }
1724

1825
private RenderHandle _renderHandle;
1926

@@ -35,26 +42,28 @@ private void RenderContent()
3542
_renderHandle.Render(BuildRenderTree);
3643
}
3744

38-
private object key = new();
45+
private object _key = new();
3946

40-
/// <summary>
41-
/// <inheritdoc/>
42-
/// </summary>
43-
/// <param name="builder"></param>
4447
private void BuildRenderTree(RenderTreeBuilder builder)
4548
{
4649
builder.OpenElement(0, "div");
47-
builder.SetKey(key);
48-
builder.AddContent(10, ChildContent);
50+
builder.SetKey(_key);
51+
builder.AddAttribute(5, "class", ClassString);
52+
builder.AddAttribute(6, "id", ComponentIdGenerator.Generate(Item));
53+
builder.AddContent(10, Item.ChildContent);
4954
builder.CloseElement();
5055
}
5156

57+
private string? ClassString => CssBuilder.Default("tabs-body-content")
58+
.AddClass("d-none", !Item.IsActive)
59+
.Build();
60+
5261
/// <summary>
5362
/// Render method
5463
/// </summary>
5564
public void Render()
5665
{
57-
key = new object();
66+
_key = new object();
5867
RenderContent();
5968
}
6069
}

src/BootstrapBlazor/Extensions/TabItemExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static class TabItemExtensions
1515
public static RenderFragment RenderContent(this TabItem item, ConcurrentDictionary<TabItem, TabItemContent> cache) => builder =>
1616
{
1717
builder.OpenComponent<TabItemContent>(0);
18-
builder.AddAttribute(10, nameof(TabItemContent.ChildContent), item.ChildContent);
18+
builder.AddAttribute(10, nameof(TabItemContent.Item), item);
1919
builder.AddComponentReferenceCapture(20, content =>
2020
{
2121
var tabItemContent = (TabItemContent)content;

0 commit comments

Comments
 (0)