Skip to content

Commit

Permalink
feat(CheckboxListGeneric): add ItemTemplate parameter (#5255)
Browse files Browse the repository at this point in the history
* chore: bump version 9.3.1-beta05

* feat: 增加 ItemTemplate 支持

* test: 增加单元测试
  • Loading branch information
ArgoZhang authored Jan 31, 2025
1 parent 3a219b6 commit 34a6a8a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.3.1-beta04</Version>
<Version>9.3.1-beta05</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ else
<Checkbox TValue="bool" IsDisabled="GetDisabledState(item)"
ShowAfterLabel="true" ShowLabel="false" ShowLabelTooltip="ShowLabelTooltip"
DisplayText="@item.Text" OnBeforeStateChanged="_onBeforeStateChangedCallback!"
Value="@item.Active" OnStateChanged="@((_, v) => OnStateChanged(item, v))"></Checkbox>
Value="@item.Active" OnStateChanged="@((_, v) => OnStateChanged(item, v))"
ChildContent="GetChildContent(item)"></Checkbox>
</div>
}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public partial class CheckboxListGeneric<TValue> : IModelEqualityComparer<TValue
[Parameter]
public Func<Task>? OnMaxSelectedCountExceed { get; set; }

/// <summary>
/// 获得/设置 项模板
/// </summary>
[Parameter]
public RenderFragment<SelectedItem<TValue>>? ItemTemplate { get; set; }

/// <summary>
/// 获得 当前选项是否被禁用
/// </summary>
Expand Down Expand Up @@ -216,6 +222,10 @@ private async Task OnStateChanged(SelectedItem<TValue> item, bool v)
/// </summary>
private Task OnClick(SelectedItem<TValue> item) => OnStateChanged(item, !item.Active);

private RenderFragment? GetChildContent(SelectedItem<TValue> item) => ItemTemplate == null
? null
: ItemTemplate(item);

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand Down
21 changes: 21 additions & 0 deletions test/UnitTest/Components/CheckboxListGenericTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@ await cut.InvokeAsync(async () =>
Assert.False(max);
}

[Fact]
public void ItemTemplate_Ok()
{
var items = new List<SelectedItem<Foo>>()
{
new(new Foo() { Id = 1, Name = "Test1" }, "Test 1"),
new(new Foo() { Id = 2, Name = "Test2" }, "Test 2"),
new(new Foo() { Id = 3, Name = "Test3" }, "Test 3")
};
var cut = Context.RenderComponent<CheckboxListGeneric<Foo>>(pb =>
{
pb.Add(a => a.Items, items);
pb.Add(a => a.ItemTemplate, foo => builder =>
{
builder.AddContent(0, foo.Text);
});
});
var labels = cut.FindAll(".checkbox-item .form-check-label");
Assert.Equal(3, labels.Count);
}

private class Dummy
{
[Required]
Expand Down

0 comments on commit 34a6a8a

Please sign in to comment.