Skip to content

Commit a802e1a

Browse files
authored
feat(Table): add DisableDelete/EditButtonCallback Parameter (#4603)
* feat(Table): add DisableDelete/EditButtonCallback Parameter * test: 更新单元测试 * chore: bump version 9.0.0-rc.2.11.3.0 * chore: 更新 docker image
1 parent e832302 commit a802e1a

File tree

4 files changed

+95
-6
lines changed

4 files changed

+95
-6
lines changed

src/BootstrapBlazor.Server/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
22
#For more information, please see https://aka.ms/containercompat
33

4-
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
4+
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
55
WORKDIR /app
66
EXPOSE 80
77

8-
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
8+
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
99
WORKDIR /
1010
COPY . .
1111

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

77
<ItemGroup>

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,18 @@ public async Task ExpandDetailRow(TItem item)
724724
[NotNull]
725725
public string? AlignRightTooltipText { get; set; }
726726

727+
/// <summary>
728+
/// 获得/设置 删除按钮是否禁用回调方法
729+
/// </summary>
730+
[Parameter]
731+
public Func<List<TItem>, bool>? DisableDeleteButtonCallback { get; set; }
732+
733+
/// <summary>
734+
/// 获得/设置 编辑按钮是否禁用回调方法
735+
/// </summary>
736+
[Parameter]
737+
public Func<List<TItem>, bool>? DisableEditButtonCallback { get; set; }
738+
727739
[CascadingParameter]
728740
private ContextMenuZone? ContextMenuZone { get; set; }
729741

@@ -1461,13 +1473,13 @@ public async Task ResetSortAsync()
14611473
/// 返回 true 时按钮禁用
14621474
/// </summary>
14631475
/// <returns></returns>
1464-
private bool GetEditButtonStatus() => ShowAddForm || AddInCell || SelectedRows.Count != 1;
1476+
private bool GetEditButtonStatus() => ShowAddForm || AddInCell || (DisableEditButtonCallback?.Invoke(SelectedRows) ?? SelectedRows.Count != 1);
14651477

14661478
/// <summary>
14671479
/// 返回 true 时按钮禁用
14681480
/// </summary>
14691481
/// <returns></returns>
1470-
private bool GetDeleteButtonStatus() => ShowAddForm || AddInCell || SelectedRows.Count == 0;
1482+
private bool GetDeleteButtonStatus() => ShowAddForm || AddInCell || (DisableDeleteButtonCallback?.Invoke(SelectedRows) ?? SelectedRows.Count == 0);
14711483

14721484
private async Task InvokeItemsChanged()
14731485
{

test/UnitTest/Components/TableTest.cs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6302,7 +6302,46 @@ public void ShowEditButton_Ok()
63026302
{
63036303
pb.Add(a => a.ShowEditButton, false);
63046304
});
6305-
cut.WaitForAssertion(() => table.DoesNotContain("fa-regular fa-pen-to-square"));
6305+
table.DoesNotContain("fa-regular fa-pen-to-square");
6306+
}
6307+
6308+
[Fact]
6309+
public void DisableEditButtonCallback_Ok()
6310+
{
6311+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
6312+
var items = Foo.GenerateFoo(localizer, 2);
6313+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
6314+
{
6315+
pb.AddChildContent<Table<Foo>>(pb =>
6316+
{
6317+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
6318+
pb.Add(a => a.Items, items);
6319+
pb.Add(a => a.ShowToolbar, true);
6320+
pb.Add(a => a.TableColumns, foo => builder =>
6321+
{
6322+
builder.OpenComponent<TableColumn<Foo, string>>(0);
6323+
builder.AddAttribute(1, "Field", "Name");
6324+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
6325+
builder.CloseComponent();
6326+
});
6327+
pb.Add(a => a.SelectedRows, [items[0]]);
6328+
});
6329+
});
6330+
6331+
var buttons = cut.FindComponents<Button>();
6332+
var editButton = buttons.First(i => i.Instance.Text == "编辑");
6333+
Assert.False(editButton.Instance.IsDisabled);
6334+
6335+
// 即使选中行,编辑按钮仍然被禁用
6336+
var table = cut.FindComponent<Table<Foo>>();
6337+
table.SetParametersAndRender(pb =>
6338+
{
6339+
pb.Add(a => a.DisableEditButtonCallback, items =>
6340+
{
6341+
return true;
6342+
});
6343+
});
6344+
Assert.True(editButton.Instance.IsDisabled);
63066345
}
63076346

63086347
[Fact]
@@ -6337,6 +6376,44 @@ public void ShowDeleteButton_Ok()
63376376
cut.WaitForAssertion(() => cut.DoesNotContain("fa-solid fa-xmark"));
63386377
}
63396378

6379+
[Fact]
6380+
public void DisableDeleteButtonCallback_Ok()
6381+
{
6382+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
6383+
var items = Foo.GenerateFoo(localizer, 2);
6384+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
6385+
{
6386+
pb.AddChildContent<Table<Foo>>(pb =>
6387+
{
6388+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
6389+
pb.Add(a => a.Items, items);
6390+
pb.Add(a => a.ShowToolbar, true);
6391+
pb.Add(a => a.TableColumns, foo => builder =>
6392+
{
6393+
builder.OpenComponent<TableColumn<Foo, string>>(0);
6394+
builder.AddAttribute(1, "Field", "Name");
6395+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
6396+
builder.CloseComponent();
6397+
});
6398+
pb.Add(a => a.SelectedRows, [items[0]]);
6399+
});
6400+
});
6401+
6402+
var deleteButton = cut.FindComponent<TableToolbarPopConfirmButton<Foo>>();
6403+
Assert.False(deleteButton.Instance.IsDisabled);
6404+
6405+
// 即使选中行,编辑按钮仍然被禁用
6406+
var table = cut.FindComponent<Table<Foo>>();
6407+
table.SetParametersAndRender(pb =>
6408+
{
6409+
pb.Add(a => a.DisableDeleteButtonCallback, items =>
6410+
{
6411+
return true;
6412+
});
6413+
});
6414+
Assert.True(deleteButton.Instance.IsDisabled);
6415+
}
6416+
63406417
[Fact]
63416418
public void ShowEditButtonCallback_Ok()
63426419
{

0 commit comments

Comments
 (0)