-
-
Notifications
You must be signed in to change notification settings - Fork 364
feat(Display): support LookupService method GetItemsByKeyAsync #4924
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ | |
| // Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone | ||
|
|
||
| using System.Collections; | ||
| using System.Linq.Expressions; | ||
| using System.Reflection; | ||
|
|
||
| namespace BootstrapBlazor.Components; | ||
|
|
@@ -18,10 +17,7 @@ public partial class Display<TValue> | |
| .AddClassFromAttributes(AdditionalAttributes) | ||
| .Build(); | ||
|
|
||
| /// <summary> | ||
| /// 获得 显示文本 | ||
| /// </summary> | ||
| protected string? CurrentTextAsString { get; set; } | ||
| private string? _displayText; | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 异步格式化字符串 | ||
|
|
@@ -56,15 +52,20 @@ public partial class Display<TValue> | |
| [Parameter] | ||
| public object? LookupServiceData { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/> | ||
| /// </summary> | ||
| [Parameter] | ||
| public ILookupService? LookupService { get; set; } | ||
|
|
||
| [Inject] | ||
| [NotNull] | ||
| private ILookupService? LookupService { get; set; } | ||
| private ILookupService? InjectLookupService { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 获得/设置 类型解析回调方法 组件泛型为 Array 时内部调用 | ||
| /// </summary> | ||
| [Parameter] | ||
|
|
||
| public Func<Assembly?, string, bool, Type?>? TypeResolver { get; set; } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -73,24 +74,6 @@ public partial class Display<TValue> | |
| [Parameter] | ||
| public bool ShowTooltip { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/>> | ||
| /// </summary> | ||
| /// <param name="parameters"></param> | ||
| /// <returns></returns> | ||
| public override Task SetParametersAsync(ParameterView parameters) | ||
| { | ||
| parameters.SetParameterProperties(this); | ||
|
|
||
| if (!string.IsNullOrEmpty(LookupServiceKey)) | ||
| { | ||
| Lookup ??= LookupService.GetItemsByKey(LookupServiceKey, LookupServiceData); | ||
| } | ||
|
|
||
| // For derived components, retain the usual lifecycle with OnInit/OnParametersSet/etc. | ||
| return base.SetParametersAsync(ParameterView.Empty); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <inheritdoc/>> | ||
| /// </summary> | ||
|
|
@@ -99,23 +82,23 @@ protected override async Task OnParametersSetAsync() | |
| { | ||
| await base.OnParametersSetAsync(); | ||
|
|
||
| CurrentTextAsString = await FormatTextAsString(Value); | ||
| _displayText = await FormatDisplayText(Value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 数值格式化委托方法 | ||
| /// </summary> | ||
| /// <param name="value"></param> | ||
| /// <returns></returns> | ||
| private async Task<string?> FormatTextAsString(TValue value) => FormatterAsync != null | ||
| private async Task<string?> FormatDisplayText(TValue value) => FormatterAsync != null | ||
| ? await FormatterAsync(value) | ||
| : (!string.IsNullOrEmpty(FormatString) && value != null | ||
| ? Utility.Format(value, FormatString) | ||
| : value == null | ||
| ? FormatValueString() | ||
| : FormatText(value)); | ||
| : await FormatText(value)); | ||
|
|
||
| private string FormatText([DisallowNull] TValue value) | ||
| private async Task<string> FormatText([DisallowNull] TValue value) | ||
| { | ||
| string ret; | ||
| var type = typeof(TValue); | ||
|
|
@@ -125,12 +108,12 @@ private string FormatText([DisallowNull] TValue value) | |
| } | ||
| else if (type.IsArray) | ||
| { | ||
| ret = ConvertArrayToString(value); | ||
| ret = ArrayConvertToString(value); | ||
| } | ||
| else if (type.IsGenericType && type.IsAssignableTo(typeof(IEnumerable))) | ||
| { | ||
| // 泛型集合 IEnumerable<TValue> | ||
| ret = ConvertEnumerableToString(value); | ||
| ret = await ConvertEnumerableToString(value); | ||
| } | ||
| else | ||
| { | ||
|
|
@@ -152,91 +135,51 @@ private string FormatValueString() | |
| return ret ?? valueString ?? string.Empty; | ||
| } | ||
|
|
||
| private Func<TValue, string>? _converterArray; | ||
| /// <summary> | ||
| /// 获取属性方法 Lambda 表达式 | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| private string ConvertArrayToString(TValue value) | ||
| private Func<TValue, string>? _arrayConvertoString; | ||
| private string ArrayConvertToString(TValue value) | ||
| { | ||
| return (_converterArray ??= ConvertArrayToStringLambda())(value); | ||
|
|
||
| Func<TValue, string> ConvertArrayToStringLambda() | ||
| { | ||
| Func<TValue, string> ret = _ => ""; | ||
| var param = Expression.Parameter(typeof(Array)); | ||
| var targetType = typeof(TValue).UnderlyingSystemType; | ||
| var methodType = ResolveArrayType(); | ||
| if (methodType != null) | ||
| { | ||
| // 调用 string.Join<T>(",", IEnumerable<T>) 方法 | ||
| var method = typeof(string).GetMethods().First(m => m is { Name: "Join", IsGenericMethod: true } && m.GetParameters()[0].ParameterType == typeof(string)).MakeGenericMethod(methodType); | ||
| var body = Expression.Call(method, Expression.Constant(","), Expression.Convert(param, targetType)); | ||
| ret = Expression.Lambda<Func<TValue, string>>(body, param).Compile(); | ||
| } | ||
| return ret; | ||
|
|
||
| Type? ResolveArrayType() | ||
| { | ||
| Type? t = null; | ||
| var typeName = targetType.FullName; | ||
| if (!string.IsNullOrEmpty(typeName)) | ||
| { | ||
| typeName = typeName.Replace("[]", ""); | ||
| if (typeName.Contains('+')) | ||
| { | ||
| typeName = typeName.Split('+', StringSplitOptions.RemoveEmptyEntries).Last(); | ||
| } | ||
| t = Type.GetType(typeName, null, TypeResolver, false, true); | ||
| } | ||
| return t; | ||
| } | ||
| } | ||
| _arrayConvertoString ??= LambdaExtensions.ArrayConvertToStringLambda<TValue>(TypeResolver).Compile(); | ||
| return _arrayConvertoString(value); | ||
| } | ||
|
|
||
| private static Func<TValue, string>? _convertEnumerableToString; | ||
| private static Func<TValue, IEnumerable<string>>? _convertToEnumerableString; | ||
| /// <summary> | ||
| /// 获取属性方法 Lambda 表达式 | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| private string ConvertEnumerableToString(TValue value) | ||
| private static Func<TValue, string>? _enumerableConvertToString; | ||
| private async Task<string> ConvertEnumerableToString(TValue value) | ||
| { | ||
| return Lookup == null | ||
| ? (_convertEnumerableToString ??= ConvertEnumerableToStringLambda())(value) | ||
| : GetTextByValue((_convertToEnumerableString ??= ConvertToEnumerableStringLambda())(value)); | ||
|
|
||
| static Func<TValue, string> ConvertEnumerableToStringLambda() | ||
| { | ||
| var typeArguments = typeof(TValue).GenericTypeArguments; | ||
| var param = Expression.Parameter(typeof(IEnumerable<>).MakeGenericType(typeArguments)); | ||
| var method = typeof(string).GetMethods().First(m => m is { Name: "Join", IsGenericMethod: true } && m.GetParameters()[0].ParameterType == typeof(string)).MakeGenericMethod(typeArguments); | ||
| var body = Expression.Call(method, Expression.Constant(","), param); | ||
| return Expression.Lambda<Func<TValue, string>>(body, param).Compile(); | ||
| } | ||
| _enumerableConvertToString ??= LambdaExtensions.EnumerableConvertToStringLambda<TValue>().Compile(); | ||
| var lookup = await GetLookup(); | ||
| return lookup == null | ||
| ? _enumerableConvertToString(value) | ||
| : GetTextByValue(lookup, value); | ||
| } | ||
|
|
||
| static Func<TValue, IEnumerable<string>> ConvertToEnumerableStringLambda() | ||
| private static Func<TValue, IEnumerable<string>>? _convertToStringEnumerable; | ||
| private static string GetTextByValue(IEnumerable<SelectedItem> lookup, TValue value) | ||
| { | ||
| _convertToStringEnumerable ??= LambdaExtensions.ConvertToStringEnumerableLambda<TValue>().Compile(); | ||
| var source = _convertToStringEnumerable(value); | ||
| return string.Join(",", source.Aggregate(new List<string>(), (s, i) => | ||
| { | ||
| var typeArguments = typeof(TValue).GenericTypeArguments; | ||
| var param = Expression.Parameter(typeof(IEnumerable<>).MakeGenericType(typeArguments)); | ||
|
|
||
| var method = typeof(Display<>).MakeGenericType(typeof(TValue)) | ||
| .GetMethod(nameof(Cast), BindingFlags.NonPublic | BindingFlags.Static)! | ||
| .MakeGenericMethod(typeArguments); | ||
| var body = Expression.Call(method, param); | ||
| return Expression.Lambda<Func<TValue, IEnumerable<string>>>(body, param).Compile(); | ||
| } | ||
| var text = lookup.FirstOrDefault(d => d.Value.Equals(i, StringComparison.OrdinalIgnoreCase))?.Text; | ||
| if (text != null) | ||
| { | ||
| s.Add(text); | ||
| } | ||
| return s; | ||
| })); | ||
| } | ||
|
|
||
| private static IEnumerable<string> Cast<TType>(IEnumerable<TType> source) => source.Select(i => i?.ToString() ?? string.Empty); | ||
| private ILookupService GetLookupService() => LookupService ?? InjectLookupService; | ||
|
|
||
| private string GetTextByValue(IEnumerable<string> source) => string.Join(",", source.Aggregate(new List<string>(), (s, i) => | ||
| private IEnumerable<SelectedItem>? _lookupData; | ||
| private async Task<IEnumerable<SelectedItem>?> GetLookup() | ||
| { | ||
| var text = Lookup!.FirstOrDefault(d => d.Value.Equals(i, StringComparison.OrdinalIgnoreCase))?.Text; | ||
| if (text != null) | ||
| if (Lookup != null) | ||
| { | ||
| s.Add(text); | ||
| return Lookup; | ||
| } | ||
| return s; | ||
| })); | ||
|
|
||
| var lookupService = GetLookupService(); | ||
| _lookupData ??= await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData); | ||
| return _lookupData; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.