Skip to content

Commit d85da3f

Browse files
authored
feat(Table): add CreateSearchModelCallback parameter (#4384)
* perf: 优化性能 * refactor: 精简代码 * refactor: 增加 CreateSearchModelCallback 回调方法 * test: 更新单元测试 * chore: bump version 8.10.2-beta02 * test: 增加单元测试 * test: 更新单元测试 * test: 更新单元测试 * refactor: 删除注释掉的代码
1 parent f0dfab0 commit d85da3f

File tree

14 files changed

+74
-25
lines changed

14 files changed

+74
-25
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>8.10.2-beta01</Version>
4+
<Version>8.10.2-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,14 @@ private TItem CreateTItem()
304304
return item;
305305
}
306306

307+
/// <summary>
308+
/// 获得/设置 新建搜索模型回调方法 默认 null 未设置时先 尝试使用 <see cref="CreateItemCallback"/> 回调,再使用默认无参构造函数创建
309+
/// </summary>
310+
[Parameter]
311+
public Func<TItem>? CreateSearchModelCallback { get; set; }
312+
313+
private TItem CreateSearchModel() => CreateSearchModelCallback?.Invoke() ?? CreateTItem();
314+
307315
/// <summary>
308316
/// 单选模式下选择行时调用此方法
309317
/// </summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected async Task ResetSearchClick()
139139
}
140140
else if (SearchTemplate == null)
141141
{
142-
Utility.Reset(SearchModel, CreateTItem());
142+
Utility.Reset(SearchModel, CreateSearchModel());
143143
}
144144

145145
PageIndex = 1;
@@ -246,7 +246,7 @@ protected IEnumerable<IFilterAction> GetCustomerSearches()
246246
protected List<IFilterAction> GetAdvanceSearches()
247247
{
248248
var searches = new List<IFilterAction>();
249-
if (ShowAdvancedSearch && CustomerSearchModel == null && SearchModel != null)
249+
if (ShowAdvancedSearch && CustomerSearchModel == null)
250250
{
251251
var callback = GetAdvancedSearchFilterCallback ?? new Func<PropertyInfo, TItem, List<SearchFilterAction>?>((p, model) =>
252252
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ private void OnInitParameters()
859859
TreeNodeLoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableTreeNodeLoadingIcon);
860860
AdvancedSortButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableAdvancedSortButtonIcon);
861861

862-
SearchModel ??= CreateTItem();
862+
SearchModel ??= CreateSearchModel();
863863
}
864864

865865
/// <summary>

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -632,13 +632,6 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value
632632
OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value);
633633
}
634634

635-
//private readonly List<string> _invalidComponents = [];
636-
637-
//internal void AddValidationComponent(string id)
638-
//{
639-
// _invalidComponents.Add(id);
640-
//}
641-
642635
/// <summary>
643636
/// 获取 当前表单值改变的属性集合
644637
/// </summary>

src/BootstrapBlazor/Extensions/TypeExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ namespace BootstrapBlazor.Components;
1010

1111
internal static class TypeExtensions
1212
{
13-
public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => type.GetRuntimeProperties().FirstOrDefault(p => p.Name == propertyName);
13+
public static PropertyInfo? GetPropertyByName(this Type type, string propertyName) => CacheManager.GetRuntimeProperties(type).Find(p => p.Name == propertyName);
1414

15-
public static FieldInfo? GetFieldByName(this Type type, string fieldName) => type.GetRuntimeFields().FirstOrDefault(p => p.Name == fieldName);
15+
public static FieldInfo? GetFieldByName(this Type type, string fieldName) => CacheManager.GetRuntimeFields(type).Find(p => p.Name == fieldName);
1616

1717
public static async Task<bool> IsAuthorizedAsync(this Type type, Task<AuthenticationState>? authenticateState, IAuthorizationPolicyProvider? authorizePolicy, IAuthorizationService? authorizeService, object? resource = null)
1818
{

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,36 @@ public static object GetFormatterInvoker(Type type, Func<object?, Task<string?>>
720720
private static Func<TType, Task<string?>> InvokeFormatterAsync<TType>(Func<object?, Task<string?>> formatter) => new(v => formatter(v));
721721

722722
#endregion
723+
724+
#region TypeExtensions
725+
/// <summary>
726+
/// 通过指定类型获得所有属性信息
727+
/// </summary>
728+
/// <param name="type"></param>
729+
/// <returns></returns>
730+
public static List<PropertyInfo> GetRuntimeProperties(Type type)
731+
{
732+
var cacheKey = $"{nameof(GetRuntimeProperties)}-{type.GetUniqueTypeName()}";
733+
return Instance.GetOrCreate(cacheKey, entry =>
734+
{
735+
entry.SetDynamicAssemblyPolicy(type);
736+
return type.GetRuntimeProperties().ToList();
737+
});
738+
}
739+
740+
/// <summary>
741+
/// 通过指定类型获得所有字段信息
742+
/// </summary>
743+
/// <param name="type"></param>
744+
/// <returns></returns>
745+
public static List<FieldInfo> GetRuntimeFields(Type type)
746+
{
747+
var cacheKey = $"{nameof(GetRuntimeFields)}-{type.GetUniqueTypeName()}";
748+
return Instance.GetOrCreate(cacheKey, entry =>
749+
{
750+
entry.SetDynamicAssemblyPolicy(type);
751+
return type.GetRuntimeFields().ToList();
752+
})!;
753+
}
754+
#endregion
723755
}

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ public static class Utility
213213
/// <typeparam name="TModel"></typeparam>
214214
public static void Reset<TModel>(TModel source, TModel model) where TModel : class
215215
{
216-
var v = model;
216+
var modelType = model.GetType();
217217
foreach (var pi in source.GetType().GetRuntimeProperties().Where(p => p.IsCanWrite()))
218218
{
219-
var pInfo = v.GetType().GetPropertyByName(pi.Name);
219+
var pInfo = modelType.GetPropertyByName(pi.Name);
220220
if (pInfo != null)
221221
{
222-
pi.SetValue(source, pInfo.GetValue(v));
222+
pi.SetValue(source, pInfo.GetValue(model));
223223
}
224224
}
225225
}

test/UnitTest/Components/TableTest.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3838,6 +3838,14 @@ public async Task CustomerSearchTemplate_Ok()
38383838
var resetButton = cut.Find(".fa-trash-can");
38393839
await cut.InvokeAsync(() => resetButton.Click());
38403840
Assert.Null(searchModel.Name);
3841+
3842+
var table = cut.FindComponent<Table<Foo>>();
3843+
table.SetParametersAndRender(pb =>
3844+
{
3845+
pb.Add(a => a.ShowAdvancedSearch, false);
3846+
});
3847+
await cut.InvokeAsync(() => resetButton.Click());
3848+
Assert.Null(searchModel.Name);
38413849
}
38423850

38433851
[Fact]
@@ -3849,7 +3857,6 @@ public void SearchTemplate_Ok()
38493857
pb.AddChildContent<Table<Foo>>(pb =>
38503858
{
38513859
pb.Add(a => a.ShowSearch, true);
3852-
pb.Add(a => a.SearchModel, new Foo());
38533860
pb.Add(a => a.SearchMode, SearchMode.Top);
38543861
pb.Add(a => a.RenderMode, TableRenderMode.Table);
38553862
pb.Add(a => a.SearchTemplate, foo => builder => builder.AddContent(0, "test_SearchTemplate"));
@@ -3863,8 +3870,17 @@ public void SearchTemplate_Ok()
38633870
});
38643871
});
38653872
});
3866-
38673873
cut.Contains("test_SearchTemplate");
3874+
3875+
var table = cut.FindComponent<Table<Foo>>();
3876+
Assert.NotNull(table.Instance.SearchModel);
3877+
3878+
table.SetParametersAndRender(pb =>
3879+
{
3880+
pb.Add(a => a.SearchModel, null);
3881+
pb.Add(a => a.CreateSearchModelCallback, () => new Foo());
3882+
});
3883+
Assert.NotNull(table.Instance.SearchModel);
38683884
}
38693885

38703886
[Fact]

test/UnitTest/Extensions/IQueryableExtensionsTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace UnitTest.Extensions;
66

7-
public class IQueryableExtensionsTest
7+
public class IQueryableExtensionsTest : BootstrapBlazorTestBase
88
{
99
[Fact]
1010
public void Where_Ok()
@@ -28,8 +28,8 @@ public void Sort_Ok()
2828
new() { Name = "Test2" }
2929
}.AsQueryable();
3030

31-
Assert.Equal("Test2", foos.Sort<Foo>("Name", SortOrder.Desc, true).First().Name);
32-
Assert.Equal("Test1", foos.Sort<Foo>("Name", SortOrder.Desc, false).First().Name);
31+
Assert.Equal("Test2", foos.Sort("Name", SortOrder.Desc, true).First().Name);
32+
Assert.Equal("Test1", foos.Sort("Name", SortOrder.Desc, false).First().Name);
3333
}
3434

3535
[Fact]

0 commit comments

Comments
 (0)