|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using Microsoft.AspNetCore.Components.Rendering; |
| 7 | +using System.ComponentModel; |
| 8 | + |
| 9 | +namespace BootstrapBlazor.Components; |
| 10 | + |
| 11 | +internal class LookupContent : ComponentBase |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// 获得/设置 <see cref="ILookupService"/> 服务实例 |
| 15 | + /// </summary> |
| 16 | + [Parameter] |
| 17 | + public ILookupService? LookupService { get; set; } |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值 常用于外键自动转换为名称操作,可以通过 <see cref="LookupServiceData"/> 传递自定义数据 |
| 21 | + /// </summary> |
| 22 | + [Parameter] |
| 23 | + [EditorRequired] |
| 24 | + public string? LookupServiceKey { get; set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// 获得/设置 <see cref="ILookupService"/> 服务获取 Lookup 数据集合键值自定义数据,通过 <see cref="LookupServiceKey"/> 指定键值 |
| 28 | + /// </summary> |
| 29 | + [Parameter] |
| 30 | + public object? LookupServiceData { get; set; } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// 获得/设置 字典数据源字符串比较规则 默认 <see cref="StringComparison.OrdinalIgnoreCase" /> 大小写不敏感 |
| 34 | + /// </summary> |
| 35 | + [Parameter] |
| 36 | + public StringComparison LookupStringComparison { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// 获得/设置 显示值 |
| 40 | + /// </summary> |
| 41 | + [Parameter] |
| 42 | + public string? Value { get; set; } |
| 43 | + |
| 44 | + [Inject] |
| 45 | + [NotNull] |
| 46 | + private ILookupService? InjectLookupService { get; set; } |
| 47 | + |
| 48 | + private string? _content; |
| 49 | + |
| 50 | + private List<SelectedItem>? _items; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// <inheritdoc/> |
| 54 | + /// </summary> |
| 55 | + /// <returns></returns> |
| 56 | + protected override async Task OnParametersSetAsync() |
| 57 | + { |
| 58 | + await base.OnParametersSetAsync(); |
| 59 | + |
| 60 | + _items ??= await GetLookupItemsAsync(); |
| 61 | + var item = _items.Find(i => i.Value.Equals(Value, LookupStringComparison)); |
| 62 | + _content = item?.Text ?? Value; |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// <inheritdoc/> |
| 67 | + /// </summary> |
| 68 | + /// <param name="builder"></param> |
| 69 | + protected override void BuildRenderTree(RenderTreeBuilder builder) |
| 70 | + { |
| 71 | + if (!string.IsNullOrEmpty(_content)) |
| 72 | + { |
| 73 | + builder.AddContent(0, _content); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private async Task<List<SelectedItem>> GetLookupItemsAsync() |
| 78 | + { |
| 79 | + var lookupService = LookupService ?? InjectLookupService; |
| 80 | + var items = await lookupService.GetItemsAsync(LookupServiceKey, LookupServiceData); |
| 81 | + return items?.ToList() ?? []; |
| 82 | + } |
| 83 | +} |
0 commit comments