!2394 refactor(#I4SODP): improve MultiSelect component

* refactor: 兼容 String 类型
* refactor: 精简逻辑
* refactor: MultiSelect 组件重构移除对 IDispose 接口继承
This commit is contained in:
Argo 2022-02-07 05:22:35 +00:00
parent 1c21fd9d1a
commit 86f1a7c691

View File

@ -13,7 +13,7 @@ namespace BootstrapBlazor.Components;
/// <summary>
/// MultiSelect 组件
/// </summary>
public partial class MultiSelect<TValue> : IDisposable
public partial class MultiSelect<TValue>
{
private ElementReference SelectElement { get; set; }
@ -198,19 +198,7 @@ public partial class MultiSelect<TValue> : IDisposable
var list = CurrentValueAsString.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var item in Items)
{
item.Active = false;
var v = item.Value;
if (!string.IsNullOrEmpty(v))
{
foreach (var l in list)
{
if (v == l.ToString())
{
item.Active = true;
break;
}
}
}
item.Active = list.Any(i => i.Equals(item.Value, StringComparison.OrdinalIgnoreCase));
}
}
@ -403,12 +391,18 @@ public partial class MultiSelect<TValue> : IDisposable
if (Items == null)
{
// 判断 IEnumerable<T> 泛型 T 是否为 Enum
Type? innerType = null;
if (typeof(IEnumerable).IsAssignableFrom(typeof(TValue)))
// 特别注意 string 是 IEnumerable 的实例
var type = typeof(TValue);
Type? innerType;
if (type.IsGenericType && type.IsAssignableTo(typeof(IEnumerable)))
{
innerType = typeof(TValue).GetGenericArguments()[0];
innerType = type.GetGenericArguments()[0];
}
if (innerType != null && innerType.IsEnum)
else
{
innerType = NullableUnderlyingType ?? type;
}
if (innerType.IsEnum)
{
Items = innerType.ToSelectList();
}
@ -423,21 +417,17 @@ public partial class MultiSelect<TValue> : IDisposable
/// Dispose 方法
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
protected override async ValueTask DisposeAsyncCore(bool disposing)
{
if (disposing && Interop != null)
await base.DisposeAsyncCore(disposing);
if (disposing)
{
Interop.Dispose();
Interop = null;
if (Interop != null)
{
Interop.Dispose();
Interop = null;
}
}
}
/// <summary>
/// Dispose 方法
/// </summary>
public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}