Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/BootstrapBlazor/Components/TreeView/TreeView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,13 @@ public partial class TreeView<TItem> : IModelEqualityComparer<TItem>
[Parameter]
public bool AutoCheckParent { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 <see cref="GetCheckedItems()"/> 方法返回的集合是否包含半选节点,默认为 false</para>
/// <para lang="en">Gets or sets whether the collection returned by <see cref="GetCheckedItems()"/> includes indeterminate nodes. Default is false</para>
/// </summary>
[Parameter]
public bool IsIncludeIndeterminateWhenGetCheckedItems { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 是否允许拖放操作,默认为 false</para>
/// <para lang="en">Gets or sets a value indicating whether drag-and-drop operations are allowed. Default is false</para>
Expand Down Expand Up @@ -955,7 +962,10 @@ public IEnumerable<TreeViewItem<TItem>> GetCheckedItems() => Items.Aggregate(new
t.Add(item);
t.AddRange(item.GetAllSubItems().OfType<TreeViewItem<TItem>>());
return t;
}).Where(i => i.CheckedState == CheckboxState.Checked);
}).Where(GetCheckedItems);


private bool GetCheckedItems(TreeViewItem<TItem> item) => IsIncludeIndeterminateWhenGetCheckedItems ? item.CheckedState != CheckboxState.UnChecked : item.CheckedState == CheckboxState.Checked;

/// <summary>
/// <para lang="zh">检查数据是否相同</para>
Expand Down
29 changes: 29 additions & 0 deletions test/UnitTest/Components/TreeViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,35 @@ public async Task AutoCheckParent_TriState_Ok()
Assert.Equal(CheckboxState.UnChecked, checkboxes[0].Instance.Value.CheckedState);
}

[Fact]
public void GetCheckedItems_IncludeIndeterminate_Ok()
{
var items = new List<TreeFoo>
{
new() { Text = "Node1", Id = "1010" },
new() { Text = "Node11", Id = "1011", ParentId = "1010" },
new() { Text = "Node12", Id = "1012", ParentId = "1010" }
};

var nodes = TreeFoo.CascadingTree(items);
nodes[0].Items[0].CheckedState = CheckboxState.Checked;

var cut = Context.Render<TreeView<TreeFoo>>(pb =>
{
pb.Add(a => a.AutoCheckParent, true);
pb.Add(a => a.Items, nodes);
pb.Add(a => a.ShowCheckbox, true);
});

Assert.Equal(CheckboxState.Indeterminate, nodes[0].CheckedState);
Assert.False(cut.Instance.IsIncludeIndeterminateWhenGetCheckedItems);
Assert.Equal([nodes[0].Items[0]], cut.Instance.GetCheckedItems());

cut.Render(pb => pb.Add(a => a.IsIncludeIndeterminateWhenGetCheckedItems, true));

Assert.Equal([nodes[0], nodes[0].Items[0]], cut.Instance.GetCheckedItems());
}

[Fact]
public async Task AutoCheckParent_ToggleNode_Ok()
{
Expand Down
Loading