Skip to content

Commit 5ca5ef4

Browse files
authored
feat(Table): add DisableAddButtonCallback parameter (#4607)
* feat(Table): add DisableAddButtonCallback parameter * test: 更新单元测试 * chore: bump version 9.0.0-rc.2.11.5.0
1 parent 04b8949 commit 5ca5ef4

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

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.0.0-rc.2.11.3.0</Version>
4+
<Version>9.0.0-rc.2.11.5.0</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
{
4949
@if (ShowAddButton)
5050
{
51-
<TableToolbarButton TItem="TItem" Color="Color.Success" OnClick="AddAsync" Icon="@AddButtonIcon" Text="@AddButtonText" />
51+
<TableToolbarButton TItem="TItem" Color="Color.Success" OnClick="AddAsync" Icon="@AddButtonIcon" Text="@AddButtonText" IsDisabled="GetAddButtonStatus()" />
5252
}
5353
@if (!IsExcel && ShowEditButton)
5454
{

src/BootstrapBlazor/Components/Table/Table.razor.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,19 @@ public async Task ExpandDetailRow(TItem item)
725725
public string? AlignRightTooltipText { get; set; }
726726

727727
/// <summary>
728-
/// 获得/设置 删除按钮是否禁用回调方法
728+
/// 获得/设置 新建按钮是否禁用回调方法 默认 null 未设置
729+
/// </summary>
730+
[Parameter]
731+
public Func<List<TItem>, bool>? DisableAddButtonCallback { get; set; }
732+
733+
/// <summary>
734+
/// 获得/设置 删除按钮是否禁用回调方法 默认 null 未设置
729735
/// </summary>
730736
[Parameter]
731737
public Func<List<TItem>, bool>? DisableDeleteButtonCallback { get; set; }
732738

733739
/// <summary>
734-
/// 获得/设置 编辑按钮是否禁用回调方法
740+
/// 获得/设置 编辑按钮是否禁用回调方法 默认 null 未设置
735741
/// </summary>
736742
[Parameter]
737743
public Func<List<TItem>, bool>? DisableEditButtonCallback { get; set; }
@@ -1469,6 +1475,8 @@ public async Task ResetSortAsync()
14691475
await QueryData();
14701476
}
14711477

1478+
private bool GetAddButtonStatus() => DisableAddButtonCallback?.Invoke(SelectedRows) ?? false;
1479+
14721480
/// <summary>
14731481
/// 返回 true 时按钮禁用
14741482
/// </summary>

test/UnitTest/Components/TableTest.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6344,6 +6344,45 @@ public void DisableEditButtonCallback_Ok()
63446344
Assert.True(editButton.Instance.IsDisabled);
63456345
}
63466346

6347+
[Fact]
6348+
public void DisableAddButtonCallback_Ok()
6349+
{
6350+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
6351+
var items = Foo.GenerateFoo(localizer, 2);
6352+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
6353+
{
6354+
pb.AddChildContent<Table<Foo>>(pb =>
6355+
{
6356+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
6357+
pb.Add(a => a.Items, items);
6358+
pb.Add(a => a.ShowToolbar, true);
6359+
pb.Add(a => a.TableColumns, foo => builder =>
6360+
{
6361+
builder.OpenComponent<TableColumn<Foo, string>>(0);
6362+
builder.AddAttribute(1, "Field", "Name");
6363+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
6364+
builder.CloseComponent();
6365+
});
6366+
pb.Add(a => a.SelectedRows, [items[0]]);
6367+
});
6368+
});
6369+
6370+
var buttons = cut.FindComponents<Button>();
6371+
var editButton = buttons.First(i => i.Instance.Text == "新建");
6372+
Assert.False(editButton.Instance.IsDisabled);
6373+
6374+
// 新建按钮被禁用
6375+
var table = cut.FindComponent<Table<Foo>>();
6376+
table.SetParametersAndRender(pb =>
6377+
{
6378+
pb.Add(a => a.DisableAddButtonCallback, items =>
6379+
{
6380+
return true;
6381+
});
6382+
});
6383+
Assert.True(editButton.Instance.IsDisabled);
6384+
}
6385+
63476386
[Fact]
63486387
public void ShowDeleteButton_Ok()
63496388
{

0 commit comments

Comments
 (0)