Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Table): add DisableDelete/EditButtonCallback Parameter #4603

Merged
merged 4 commits into from
Nov 3, 2024
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
4 changes: 2 additions & 2 deletions src/BootstrapBlazor.Server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,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.
#For more information, please see https://aka.ms/containercompat

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /
COPY . .

Expand Down
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.0.0-rc.2.11.2.1</Version>
<Version>9.0.0-rc.2.11.3.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 14 additions & 2 deletions src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,18 @@ public async Task ExpandDetailRow(TItem item)
[NotNull]
public string? AlignRightTooltipText { get; set; }

/// <summary>
/// 获得/设置 删除按钮是否禁用回调方法
/// </summary>
[Parameter]
public Func<List<TItem>, bool>? DisableDeleteButtonCallback { get; set; }

/// <summary>
/// 获得/设置 编辑按钮是否禁用回调方法
/// </summary>
[Parameter]
public Func<List<TItem>, bool>? DisableEditButtonCallback { get; set; }

[CascadingParameter]
private ContextMenuZone? ContextMenuZone { get; set; }

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

/// <summary>
/// 返回 true 时按钮禁用
/// </summary>
/// <returns></returns>
private bool GetDeleteButtonStatus() => ShowAddForm || AddInCell || SelectedRows.Count == 0;
private bool GetDeleteButtonStatus() => ShowAddForm || AddInCell || (DisableDeleteButtonCallback?.Invoke(SelectedRows) ?? SelectedRows.Count == 0);

private async Task InvokeItemsChanged()
{
Expand Down
79 changes: 78 additions & 1 deletion test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6302,7 +6302,46 @@ public void ShowEditButton_Ok()
{
pb.Add(a => a.ShowEditButton, false);
});
cut.WaitForAssertion(() => table.DoesNotContain("fa-regular fa-pen-to-square"));
table.DoesNotContain("fa-regular fa-pen-to-square");
}

[Fact]
public void DisableEditButtonCallback_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.Items, items);
pb.Add(a => a.ShowToolbar, true);
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Name");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();
});
pb.Add(a => a.SelectedRows, [items[0]]);
});
ArgoZhang marked this conversation as resolved.
Show resolved Hide resolved
});

var buttons = cut.FindComponents<Button>();
var editButton = buttons.First(i => i.Instance.Text == "编辑");
Assert.False(editButton.Instance.IsDisabled);

// 即使选中行,编辑按钮仍然被禁用
var table = cut.FindComponent<Table<Foo>>();
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.DisableEditButtonCallback, items =>
{
return true;
});
});
Assert.True(editButton.Instance.IsDisabled);
ArgoZhang marked this conversation as resolved.
Show resolved Hide resolved
}

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

[Fact]
public void DisableDeleteButtonCallback_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.Items, items);
ArgoZhang marked this conversation as resolved.
Show resolved Hide resolved
pb.Add(a => a.ShowToolbar, true);
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Name");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();
});
pb.Add(a => a.SelectedRows, [items[0]]);
});
});

var deleteButton = cut.FindComponent<TableToolbarPopConfirmButton<Foo>>();
Assert.False(deleteButton.Instance.IsDisabled);

// 即使选中行,编辑按钮仍然被禁用
var table = cut.FindComponent<Table<Foo>>();
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.DisableDeleteButtonCallback, items =>
{
return true;
});
});
Assert.True(deleteButton.Instance.IsDisabled);
}

[Fact]
public void ShowEditButtonCallback_Ok()
{
Expand Down