Skip to content

Commit ad53d08

Browse files
committed
feat: 实现刷新逻辑
1 parent 42a7d27 commit ad53d08

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ public partial class Tab : IHandlerException
354354

355355
private string? DraggableString => AllowDrag ? "true" : null;
356356

357+
private readonly ConcurrentDictionary<TabItem, TabItemContent> _cache = [];
358+
357359
/// <summary>
358360
/// <inheritdoc/>
359361
/// </summary>
@@ -844,15 +846,15 @@ private RenderFragment RenderTabItemContent(TabItem item) => builder =>
844846

845847
if (item.IsActive)
846848
{
847-
builder.AddContent(0, item.ChildContent);
849+
builder.AddContent(0, item.RenderContent(_cache));
848850
if (IsLazyLoadTabItem)
849851
{
850852
LazyTabCache.AddOrUpdate(item, _ => true, (_, _) => true);
851853
}
852854
}
853855
else if (!IsLazyLoadTabItem || item.AlwaysLoad || LazyTabCache.TryGetValue(item, out var init) && init)
854856
{
855-
builder.AddContent(0, item.ChildContent);
857+
builder.AddContent(0, item.RenderContent(_cache));
856858
}
857859
};
858860

@@ -905,6 +907,9 @@ public async Task DragItemCallback(int originIndex, int currentIndex)
905907

906908
private Task OnRefreshAsync()
907909
{
910+
// refresh the active tab item
911+
var item = TabItems.FirstOrDefault(i => i.IsActive);
912+
item.Refresh(_cache);
908913
return Task.CompletedTask;
909914
}
910915

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using Microsoft.AspNetCore.Components.Rendering;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
internal class TabItemContent : IComponent
11+
{
12+
/// <summary>
13+
/// Gets or sets the component content. Default is null
14+
/// </summary>
15+
[Parameter]
16+
public RenderFragment? ChildContent { get; set; }
17+
18+
private RenderHandle _renderHandle;
19+
20+
void IComponent.Attach(RenderHandle renderHandle)
21+
{
22+
_renderHandle = renderHandle;
23+
}
24+
25+
Task IComponent.SetParametersAsync(ParameterView parameters)
26+
{
27+
parameters.SetParameterProperties(this);
28+
29+
RenderContent();
30+
return Task.CompletedTask;
31+
}
32+
33+
private void RenderContent()
34+
{
35+
_renderHandle.Render(BuildRenderTree);
36+
}
37+
38+
private object key = new();
39+
40+
/// <summary>
41+
/// <inheritdoc/>
42+
/// </summary>
43+
/// <param name="builder"></param>
44+
private void BuildRenderTree(RenderTreeBuilder builder)
45+
{
46+
builder.OpenElement(0, "div");
47+
builder.SetKey(key);
48+
builder.AddContent(10, ChildContent);
49+
builder.CloseElement();
50+
}
51+
52+
/// <summary>
53+
/// Render method
54+
/// </summary>
55+
public void Render()
56+
{
57+
key = new object();
58+
RenderContent();
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Collections.Concurrent;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// TabItem Extension
12+
/// </summary>
13+
internal static class TabItemExtensions
14+
{
15+
public static RenderFragment RenderContent(this TabItem item, ConcurrentDictionary<TabItem, TabItemContent> cache) => builder =>
16+
{
17+
builder.OpenComponent<TabItemContent>(0);
18+
builder.AddAttribute(10, nameof(TabItemContent.ChildContent), item.ChildContent);
19+
builder.AddComponentReferenceCapture(20, content =>
20+
{
21+
var tabItemContent = (TabItemContent)content;
22+
cache.AddOrUpdate(item, tabItemContent, (_, _) => tabItemContent);
23+
});
24+
builder.CloseComponent();
25+
};
26+
27+
public static void Refresh(this TabItem? item, ConcurrentDictionary<TabItem, TabItemContent> cache)
28+
{
29+
if (item is not null && cache.TryGetValue(item, out var content))
30+
{
31+
content.Render();
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)