Skip to content

Commit 417d198

Browse files
AiYuZhenAiZhenArgoZhang
authored
feat(Table): enhances the validation logic for edit and delete operations (#5992)
* 增强编辑和删除操作的按钮状态检查 添加了对编辑按钮禁用状态的检查,确保在编辑时考虑行的可编辑性和按钮的可用性。同时,更新了删除操作的逻辑,增加了对删除按钮禁用状态的检查,以提高代码的健壮性。 * refactor: 重构代码 * test: 增加单元测试 * test: 更新单元测试 --------- Co-Authored-By: AiZhen <[email protected]> Co-Authored-By: AiZhen <[email protected]> Co-Authored-By: Argo Zhang <[email protected]>
1 parent a92587e commit 417d198

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ public async Task EditAsync()
544544
{
545545
if (SelectedRows.Count == 1)
546546
{
547-
// 检查是否选中了不可编辑行(行内无编辑按钮)
548-
if (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0]))
547+
// 检查是否选中了不可编辑行(行内无编辑按钮),同时检查按钮禁用状态(禁用时不可编辑)
548+
if (CanEdit())
549549
{
550550
// 提示不可编辑
551551
await ShowToastAsync(EditButtonToastTitle, EditButtonToastReadonlyContent);
@@ -992,7 +992,7 @@ protected async Task<bool> ConfirmDelete()
992992
{
993993
await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastContent);
994994
}
995-
else if (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i)))
995+
else if (CanDelete())
996996
{
997997
await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastCanNotDeleteContent);
998998
}
@@ -1003,6 +1003,16 @@ protected async Task<bool> ConfirmDelete()
10031003
return ret;
10041004
}
10051005

1006+
private bool CanEdit() => (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0]))
1007+
|| !ShowExtendEditButton
1008+
|| (DisableExtendEditButtonCallback != null && DisableExtendEditButtonCallback(SelectedRows[0]))
1009+
|| DisableExtendEditButton;
1010+
1011+
private bool CanDelete() => (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i)))
1012+
|| !ShowExtendDeleteButton
1013+
|| (DisableExtendDeleteButtonCallback != null && SelectedRows.Any(x => DisableExtendDeleteButtonCallback(x)))
1014+
|| DisableExtendDeleteButton;
1015+
10061016
/// <summary>
10071017
/// 删除数据方法
10081018
/// </summary>

test/UnitTest/Components/TableTest.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.ComponentModel.DataAnnotations;
1212
using System.Data;
1313
using System.Reflection;
14+
using System.Runtime.CompilerServices;
1415

1516
namespace UnitTest.Components;
1617

@@ -8635,6 +8636,84 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
86358636
});
86368637
}
86378638

8639+
[Fact]
8640+
public void Modify_Ok()
8641+
{
8642+
var cut = Context.RenderComponent<Table<Foo>>(pb =>
8643+
{
8644+
pb.Add(a => a.TableColumns, foo => builder =>
8645+
{
8646+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8647+
builder.AddAttribute(1, "Field", "Name");
8648+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
8649+
builder.CloseComponent();
8650+
});
8651+
pb.Add(a => a.ShowExtendEditButton, false);
8652+
pb.Add(a => a.ShowExtendDeleteButton, false);
8653+
});
8654+
Assert.True(CanEdit(cut.Instance));
8655+
Assert.True(CanDelete(cut.Instance));
8656+
8657+
cut.SetParametersAndRender(pb =>
8658+
{
8659+
pb.Add(a => a.ShowExtendEditButton, true);
8660+
pb.Add(a => a.ShowExtendDeleteButton, true);
8661+
pb.Add(a => a.DisableExtendEditButton, true);
8662+
pb.Add(a => a.DisableExtendDeleteButton, true);
8663+
});
8664+
Assert.True(CanEdit(cut.Instance));
8665+
Assert.True(CanDelete(cut.Instance));
8666+
8667+
cut.SetParametersAndRender(pb =>
8668+
{
8669+
pb.Add(a => a.ShowExtendEditButton, true);
8670+
pb.Add(a => a.ShowExtendDeleteButton, true);
8671+
pb.Add(a => a.DisableExtendEditButton, false);
8672+
pb.Add(a => a.DisableExtendDeleteButton, false);
8673+
pb.Add(a => a.SelectedRows, [new Foo()]);
8674+
pb.Add(a => a.DisableExtendEditButtonCallback, rows =>
8675+
{
8676+
return true;
8677+
});
8678+
pb.Add(a => a.DisableExtendDeleteButtonCallback, rows =>
8679+
{
8680+
return true;
8681+
});
8682+
});
8683+
Assert.True(CanEdit(cut.Instance));
8684+
Assert.True(CanDelete(cut.Instance));
8685+
}
8686+
8687+
static bool CanEdit(Table<Foo> @this)
8688+
{
8689+
var ret = false;
8690+
var methodInfo = @this.GetType().GetMethod("CanEdit", BindingFlags.Instance | BindingFlags.NonPublic);
8691+
if (methodInfo != null)
8692+
{
8693+
var result = methodInfo.Invoke(@this, null);
8694+
if (result is bool d)
8695+
{
8696+
ret = d;
8697+
}
8698+
}
8699+
return ret;
8700+
}
8701+
8702+
static bool CanDelete(Table<Foo> @this)
8703+
{
8704+
var ret = false;
8705+
var methodInfo = @this.GetType().GetMethod("CanDelete", BindingFlags.Instance | BindingFlags.NonPublic);
8706+
if (methodInfo != null)
8707+
{
8708+
var result = methodInfo.Invoke(@this, null);
8709+
if (result is bool d)
8710+
{
8711+
ret = d;
8712+
}
8713+
}
8714+
return ret;
8715+
}
8716+
86388717
class MockFoo(string name)
86398718
{
86408719
public string Name { get; set; } = name;

0 commit comments

Comments
 (0)