Skip to content

Commit 3f5750d

Browse files
committed
Merge branch 'main' into feat-textarea
2 parents 4088fe1 + 7b78b81 commit 3f5750d

File tree

5 files changed

+97
-13
lines changed

5 files changed

+97
-13
lines changed

src/BootstrapBlazor.Server/Components/Samples/TreeViews.razor

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
<section ignore>
3838
<div>@((MarkupString)Localizer["TreeViewCheckboxTips1"].Value)</div>
3939
<div>@((MarkupString)Localizer["TreeViewCheckboxTips2"].Value)</div>
40-
</section>
41-
<section ignore class="row form-inline">
42-
<div class="col-12 col-lg-auto">
43-
<Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText1"]" ShowAfterLabel="true" @bind-Value="@AutoCheckChildren"></Checkbox>
44-
<Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText2"]" ShowAfterLabel="true" @bind-Value="@AutoCheckParent" class="ms-3"></Checkbox>
45-
</div>
46-
<div class="col-12 col-lg-auto">
47-
<Button Text="@Localizer["TreeViewCheckboxButtonText"]" OnClick="@OnRefresh"></Button>
48-
<Button Text="@Localizer["TreeViewCheckboxAddButtonText"]" OnClick="OnClickAddNode" class="ms-3"></Button>
40+
<div class="row form-inline">
41+
<div class="col-12 col-lg-auto">
42+
<Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText1"]" ShowAfterLabel="true" @bind-Value="@AutoCheckChildren"></Checkbox>
43+
<Checkbox DisplayText="@Localizer["TreeViewCheckboxCheckBoxDisplayText2"]" ShowAfterLabel="true" @bind-Value="@AutoCheckParent" class="ms-3"></Checkbox>
44+
</div>
45+
<div class="col-12 col-lg-auto">
46+
<Button Text="@Localizer["TreeViewCheckboxButtonText"]" OnClick="@OnRefresh"></Button>
47+
<Button Text="@Localizer["TreeViewCheckboxAddButtonText"]" OnClick="OnClickAddNode" class="ms-3"></Button>
48+
</div>
4949
</div>
5050
</section>
5151
<TreeView Items="@CheckedItems" ShowCheckbox="true" OnTreeItemChecked="@OnTreeItemChecked" AutoCheckChildren="@AutoCheckChildren" AutoCheckParent="@AutoCheckParent"></TreeView>

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.3.1-beta11</Version>
4+
<Version>9.3.1-beta16</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
316316

317317
private string? EnableKeyboardString => EnableKeyboard ? "true" : null;
318318

319+
private bool _shouldRender = true;
320+
319321
private static string? GetItemTextClassString(TreeViewItem<TItem> item) => CssBuilder.Default("tree-node-text")
320322
.AddClass(item.CssClass)
321323
.Build();
@@ -355,13 +357,15 @@ protected override void OnParametersSet()
355357
protected override async Task OnParametersSetAsync()
356358
{
357359
_rows = null;
358-
TreeNodeStateCache.Reset();
359-
360360
if (Items != null)
361361
{
362362
if (Items.Count > 0)
363363
{
364+
_shouldRender = false;
364365
await CheckExpand(Items);
366+
_shouldRender = true;
367+
368+
_rows = null;
365369
}
366370

367371
if (ShowCheckbox && (AutoCheckParent || AutoCheckChildren))
@@ -402,6 +406,12 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
402406
}
403407
}
404408

409+
/// <summary>
410+
/// <inheritdoc/>
411+
/// </summary>
412+
/// <returns></returns>
413+
protected override bool ShouldRender() => _shouldRender;
414+
405415
/// <summary>
406416
/// <inheritdoc/>
407417
/// </summary>

src/BootstrapBlazor/Misc/ExpandableNodeCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public async Task CheckExpandAsync(TNode node, Func<TNode, Task<IEnumerable<IExp
9999
if (ExpandedNodeCache.Contains(node.Value))
100100
{
101101
// 原来是展开状态,
102-
if (node.HasChildren)
102+
if (node.HasChildren || node.Items.Any())
103103
{
104104
// 当前节点有子节点
105105
node.IsExpand = true;

test/UnitTest/Components/TreeViewTest.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,80 @@ public async Task OnExpandRowAsync_Ok()
363363
Assert.True(expanded);
364364
}
365365

366+
[Fact]
367+
public async Task KeepExpandState_Ok()
368+
{
369+
// UI 重新刷新后保持状态节点状态
370+
var items = TreeFoo.GetTreeItems();
371+
items.RemoveAt(1);
372+
items.RemoveAt(1);
373+
items[0].HasChildren = true;
374+
375+
var cut = Context.RenderComponent<TreeView<TreeFoo>>(pb =>
376+
{
377+
pb.Add(a => a.Items, items);
378+
pb.Add(a => a.OnExpandNodeAsync, item =>
379+
{
380+
return OnExpandNodeAsync(item.Value);
381+
});
382+
});
383+
var nodes = cut.FindAll(".tree-node");
384+
Assert.Single(nodes);
385+
386+
// 展开节点
387+
var bar = cut.Find(".fa-caret-right.visible");
388+
await cut.InvokeAsync(() => bar.Click());
389+
390+
cut.WaitForAssertion(() =>
391+
{
392+
nodes = cut.FindAll(".tree-node");
393+
Assert.Equal(3, nodes.Count);
394+
});
395+
396+
// 重新渲染
397+
items = TreeFoo.GetTreeItems();
398+
items.RemoveAt(1);
399+
items.RemoveAt(1);
400+
items[0].HasChildren = true;
401+
cut.SetParametersAndRender(pb =>
402+
{
403+
pb.Add(a => a.Items, items);
404+
});
405+
406+
cut.WaitForAssertion(() =>
407+
{
408+
nodes = cut.FindAll(".tree-node");
409+
Assert.Equal(3, nodes.Count);
410+
});
411+
412+
// 重新渲染
413+
items = TreeFoo.GetTreeItems();
414+
items.RemoveAt(1);
415+
items.RemoveAt(1);
416+
items[0].HasChildren = false;
417+
items[0].Items =
418+
[
419+
new(new TreeFoo() { Id = "101", ParentId = "1010" })
420+
{
421+
Text = "懒加载子节点11",
422+
HasChildren = true
423+
},
424+
new(new TreeFoo(){ Id = "102", ParentId = "1010" })
425+
{
426+
Text = "懒加载子节点22"
427+
}
428+
];
429+
cut.SetParametersAndRender(pb =>
430+
{
431+
pb.Add(a => a.Items, items);
432+
});
433+
cut.WaitForAssertion(() =>
434+
{
435+
nodes = cut.FindAll(".tree-node");
436+
Assert.Equal(3, nodes.Count);
437+
});
438+
}
439+
366440
[Fact]
367441
public async Task OnExpandRowAsync_CheckCascadeState_Ok()
368442
{

0 commit comments

Comments
 (0)