Skip to content

Commit

Permalink
feat(Table): add OnToggleDetailRowCallback parameter (#3218)
Browse files Browse the repository at this point in the history
* feat: add OnToggleDetailRowCallback parameter

* doc: update document

* test: 更新单元测试

* test: 更新单元测试
  • Loading branch information
ArgoZhang committed Apr 5, 2024
1 parent 1d0398a commit 35627de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ private List<DetailRow> GetDetailDataSource(Foo foo)
Func<Foo, Task>? ret = null;
if (IsDetails)
{
ret = foo =>
ret = async foo =>
{
Table.ExpandDetailRow(foo);
return Task.CompletedTask;
await Table.ExpandDetailRow(foo);
};
}
return ret;
Expand Down
13 changes: 12 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,12 @@ private string GetSortTooltip(ITableColumn col) => SortName != col.GetFieldName(
[Parameter]
public Func<string, TItem, object?, Task>? OnDoubleClickCellCallback { get; set; }

/// <summary>
/// 获得/设置 展开收起明细行回调方法 第二个参数 true 时表示展开 false 时表示收起
/// </summary>
[Parameter]
public Func<TItem, bool, Task>? OnToggleDetailRowCallback { get; set; }

/// <summary>
/// 获得/设置 工具栏下拉框按钮是否 IsPopover 默认 false
/// </summary>
Expand Down Expand Up @@ -351,8 +357,13 @@ private async Task OnBreakPointChanged(BreakPoint size)
/// 明细行功能中切换行状态时调用此方法
/// </summary>
/// <param name="item"></param>
public void ExpandDetailRow(TItem item)
public async Task ExpandDetailRow(TItem item)
{
// 展开明细行回调方法
if (OnToggleDetailRowCallback != null)
{
await OnToggleDetailRowCallback(item, !ExpandRows.Contains(item));
}
if (!DetailRows.Contains(item))
{
DetailRows.Add(item);
Expand Down
22 changes: 17 additions & 5 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,8 @@ public async Task IsAccordion_OK()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 4);
Foo? currentDetailRow = null;
var toggleDetailRow = false;
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
Expand All @@ -2743,6 +2745,12 @@ public async Task IsAccordion_OK()
{
builder.AddContent(1, new MarkupString($"<div class=\"detail-row-test\">{foo.Address}</div>"));
});
pb.Add(a => a.OnToggleDetailRowCallback, (foo, toggle) =>
{
currentDetailRow = foo;
toggleDetailRow = toggle;
return Task.CompletedTask;
});
});
});

Expand All @@ -2767,19 +2775,23 @@ public async Task IsAccordion_OK()
{
pb.Add(a => a.IsAccordion, true);
});
await cut.InvokeAsync(() =>
await cut.InvokeAsync(async () =>
{
table.Instance.ExpandDetailRow(items[0]);
table.Instance.ExpandDetailRow(items[1]);
table.Instance.ExpandDetailRow(items[2]);
table.Instance.ExpandDetailRow(items[3]);
await table.Instance.ExpandDetailRow(items[0]);
await table.Instance.ExpandDetailRow(items[1]);
await table.Instance.ExpandDetailRow(items[2]);
await table.Instance.ExpandDetailRow(items[3]);
});
table.SetParametersAndRender();
rows = cut.FindAll(".detail-row-test");
Assert.Equal(4, rows.Count);

rows = cut.FindAll(".is-detail.show");
Assert.Single(rows);

Assert.NotNull(currentDetailRow);
Assert.Equal(4, currentDetailRow.Id);
Assert.True(toggleDetailRow);
}

[Theory]
Expand Down

0 comments on commit 35627de

Please sign in to comment.