Skip to content

Commit

Permalink
feat(Table): add CreateSearchModelCallback parameter (#4384)
Browse files Browse the repository at this point in the history
* perf: 优化性能

* refactor: 精简代码

* refactor: 增加 CreateSearchModelCallback 回调方法

* test: 更新单元测试

* chore: bump version 8.10.2-beta02

* test: 增加单元测试

* test: 更新单元测试

* test: 更新单元测试

* refactor: 删除注释掉的代码
  • Loading branch information
ArgoZhang authored Sep 30, 2024
1 parent f0dfab0 commit d85da3f
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 25 deletions.
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>8.10.2-beta01</Version>
<Version>8.10.2-beta02</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ private TItem CreateTItem()
return item;
}

/// <summary>
/// 获得/设置 新建搜索模型回调方法 默认 null 未设置时先 尝试使用 <see cref="CreateItemCallback"/> 回调,再使用默认无参构造函数创建
/// </summary>
[Parameter]
public Func<TItem>? CreateSearchModelCallback { get; set; }

private TItem CreateSearchModel() => CreateSearchModelCallback?.Invoke() ?? CreateTItem();

/// <summary>
/// 单选模式下选择行时调用此方法
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Components/Table/Table.razor.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected async Task ResetSearchClick()
}
else if (SearchTemplate == null)
{
Utility.Reset(SearchModel, CreateTItem());
Utility.Reset(SearchModel, CreateSearchModel());
}

PageIndex = 1;
Expand Down Expand Up @@ -246,7 +246,7 @@ protected IEnumerable<IFilterAction> GetCustomerSearches()
protected List<IFilterAction> GetAdvanceSearches()
{
var searches = new List<IFilterAction>();
if (ShowAdvancedSearch && CustomerSearchModel == null && SearchModel != null)
if (ShowAdvancedSearch && CustomerSearchModel == null)
{
var callback = GetAdvancedSearchFilterCallback ?? new Func<PropertyInfo, TItem, List<SearchFilterAction>?>((p, model) =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ private void OnInitParameters()
TreeNodeLoadingIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableTreeNodeLoadingIcon);
AdvancedSortButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.TableAdvancedSortButtonIcon);

SearchModel ??= CreateTItem();
SearchModel ??= CreateSearchModel();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,6 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value
OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value);
}

//private readonly List<string> _invalidComponents = [];

//internal void AddValidationComponent(string id)
//{
// _invalidComponents.Add(id);
//}

/// <summary>
/// 获取 当前表单值改变的属性集合
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace BootstrapBlazor.Components;

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

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

public static async Task<bool> IsAuthorizedAsync(this Type type, Task<AuthenticationState>? authenticateState, IAuthorizationPolicyProvider? authorizePolicy, IAuthorizationService? authorizeService, object? resource = null)
{
Expand Down
32 changes: 32 additions & 0 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,36 @@ public static object GetFormatterInvoker(Type type, Func<object?, Task<string?>>
private static Func<TType, Task<string?>> InvokeFormatterAsync<TType>(Func<object?, Task<string?>> formatter) => new(v => formatter(v));

#endregion

#region TypeExtensions
/// <summary>
/// 通过指定类型获得所有属性信息
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static List<PropertyInfo> GetRuntimeProperties(Type type)
{
var cacheKey = $"{nameof(GetRuntimeProperties)}-{type.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry =>
{
entry.SetDynamicAssemblyPolicy(type);
return type.GetRuntimeProperties().ToList();
});
}

/// <summary>
/// 通过指定类型获得所有字段信息
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static List<FieldInfo> GetRuntimeFields(Type type)
{
var cacheKey = $"{nameof(GetRuntimeFields)}-{type.GetUniqueTypeName()}";
return Instance.GetOrCreate(cacheKey, entry =>
{
entry.SetDynamicAssemblyPolicy(type);
return type.GetRuntimeFields().ToList();
})!;
}
#endregion
}
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Utils/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ public static class Utility
/// <typeparam name="TModel"></typeparam>
public static void Reset<TModel>(TModel source, TModel model) where TModel : class
{
var v = model;
var modelType = model.GetType();
foreach (var pi in source.GetType().GetRuntimeProperties().Where(p => p.IsCanWrite()))
{
var pInfo = v.GetType().GetPropertyByName(pi.Name);
var pInfo = modelType.GetPropertyByName(pi.Name);
if (pInfo != null)
{
pi.SetValue(source, pInfo.GetValue(v));
pi.SetValue(source, pInfo.GetValue(model));
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3838,6 +3838,14 @@ public async Task CustomerSearchTemplate_Ok()
var resetButton = cut.Find(".fa-trash-can");
await cut.InvokeAsync(() => resetButton.Click());
Assert.Null(searchModel.Name);

var table = cut.FindComponent<Table<Foo>>();
table.SetParametersAndRender(pb =>
{
pb.Add(a => a.ShowAdvancedSearch, false);
});
await cut.InvokeAsync(() => resetButton.Click());
Assert.Null(searchModel.Name);
}

[Fact]
Expand All @@ -3849,7 +3857,6 @@ public void SearchTemplate_Ok()
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.ShowSearch, true);
pb.Add(a => a.SearchModel, new Foo());
pb.Add(a => a.SearchMode, SearchMode.Top);
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.SearchTemplate, foo => builder => builder.AddContent(0, "test_SearchTemplate"));
Expand All @@ -3863,8 +3870,17 @@ public void SearchTemplate_Ok()
});
});
});

cut.Contains("test_SearchTemplate");

var table = cut.FindComponent<Table<Foo>>();
Assert.NotNull(table.Instance.SearchModel);

table.SetParametersAndRender(pb =>
{
pb.Add(a => a.SearchModel, null);
pb.Add(a => a.CreateSearchModelCallback, () => new Foo());
});
Assert.NotNull(table.Instance.SearchModel);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions test/UnitTest/Extensions/IQueryableExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace UnitTest.Extensions;

public class IQueryableExtensionsTest
public class IQueryableExtensionsTest : BootstrapBlazorTestBase
{
[Fact]
public void Where_Ok()
Expand All @@ -28,8 +28,8 @@ public void Sort_Ok()
new() { Name = "Test2" }
}.AsQueryable();

Assert.Equal("Test2", foos.Sort<Foo>("Name", SortOrder.Desc, true).First().Name);
Assert.Equal("Test1", foos.Sort<Foo>("Name", SortOrder.Desc, false).First().Name);
Assert.Equal("Test2", foos.Sort("Name", SortOrder.Desc, true).First().Name);
Assert.Equal("Test1", foos.Sort("Name", SortOrder.Desc, false).First().Name);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Extensions/LambadaExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace UnitTest.Extensions;

public class LambadaExtensionsTest
public class LambadaExtensionsTest : BootstrapBlazorTestBase
{
[Fact]
public void GetFilterFunc_Null()
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Extensions/ObjectExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace UnitTest.Extensions;

public class ObjectExtensionsTest
public class ObjectExtensionsTest : BootstrapBlazorTestBase
{
[Theory]
[InlineData(null, "")]
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Extensions/QueryPageOptionsExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace UnitTest.Extensions;

public class QueryPageOptionsExtensionsTest
public class QueryPageOptionsExtensionsTest : BootstrapBlazorTestBase
{
private readonly Foo[] _foos;

Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Performance/ReflectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace UnitTest.Performance;

public class ReflectionTest(ITestOutputHelper logger)
public class ReflectionTest(ITestOutputHelper logger) : BootstrapBlazorTestBase
{
private ITestOutputHelper Logger { get; } = logger;

Expand Down

0 comments on commit d85da3f

Please sign in to comment.