Skip to content

Commit 7629ab1

Browse files
committed
feat: 枚举类型支持 Flags 标签
1 parent 6ca9ede commit 7629ab1

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/BootstrapBlazor/Components/Select/MultiSelect.razor.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,15 @@ protected override void OnParametersSet()
236236
ResetRules();
237237

238238
_itemsCache = null;
239+
239240
// 通过 Value 对集合进行赋值
240-
if (PreviousValue != CurrentValueAsString)
241+
var _currentValue = CurrentValueAsString;
242+
if (PreviousValue != _currentValue)
241243
{
242-
PreviousValue = CurrentValueAsString;
243-
var list = CurrentValueAsString.Split(',', StringSplitOptions.RemoveEmptyEntries);
244+
PreviousValue = _currentValue;
245+
var list = _currentValue.Split(',', StringSplitOptions.RemoveEmptyEntries);
244246
SelectedItems.Clear();
245-
SelectedItems.AddRange(Rows.Where(item => list.Any(i => i == item.Value)));
247+
SelectedItems.AddRange(Rows.Where(item => list.Any(i => i.Trim() == item.Value)));
246248
}
247249
}
248250

@@ -418,18 +420,9 @@ private async Task SetValue()
418420
}
419421
CurrentValue = (TValue)(ValueType.IsGenericType ? instance : listType.GetMethod("ToArray")!.Invoke(instance, null)!);
420422
}
421-
else if (ValueType.IsEnum && ValueType.GetCustomAttribute<FlagsAttribute>() != null)
423+
else if (ValueType.IsFlagEnum())
422424
{
423-
var count = 0;
424-
foreach (var item in SelectedItems)
425-
{
426-
if (Enum.TryParse(ValueType, item.Value, true, out var val))
427-
{
428-
count += (int)val;
429-
}
430-
}
431-
432-
CurrentValue = (TValue)(object)count;
425+
CurrentValue = (TValue?)SelectedItems.ParseFlagEnum<TValue>(ValueType);
433426
}
434427

435428
if (ValidateForm == null && (Min > 0 || Max > 0))

src/BootstrapBlazor/Extensions/EnumExtensions.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,7 @@ public static List<SelectedItem> ToSelectList(this Type type, SelectedItem? addi
6363
if (type.IsEnum())
6464
{
6565
var t = Nullable.GetUnderlyingType(type) ?? type;
66-
foreach (var field in Enum.GetNames(t))
67-
{
68-
var desc = Utility.GetDisplayName(t, field);
69-
ret.Add(new SelectedItem(field, desc));
70-
}
66+
ret.AddRange(from field in Enum.GetNames(t) let desc = Utility.GetDisplayName(t, field) select new SelectedItem(field, desc));
7167
}
7268
return ret;
7369
}
@@ -114,4 +110,33 @@ public static bool IsEnum(this Type? type)
114110
}
115111
return ret;
116112
}
113+
114+
/// <summary>
115+
/// 判断类型是否为 Flag 枚举类型
116+
/// </summary>
117+
/// <param name="type"></param>
118+
/// <returns></returns>
119+
public static bool IsFlagEnum(this Type? type) => type != null && IsEnum(type) && type.GetCustomAttribute<FlagsAttribute>() != null;
120+
121+
/// <summary>
122+
/// 将 <see cref="IEnumerable{T}"/> 集合转换为 Flag 枚举值
123+
/// </summary>
124+
/// <param name="items"></param>
125+
/// <param name="type"></param>
126+
/// <returns></returns>
127+
internal static object? ParseFlagEnum<TValue>(this IEnumerable<SelectedItem> items, Type type)
128+
{
129+
TValue? v = default;
130+
if (type.IsFlagEnum())
131+
{
132+
foreach (var item in items)
133+
{
134+
if (Enum.TryParse(type, item.Value, true, out var val))
135+
{
136+
v = (TValue)Enum.ToObject(type, Convert.ToInt32(v) | Convert.ToInt32(val));
137+
}
138+
}
139+
}
140+
return v;
141+
}
117142
}

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,10 @@ public static string Format(object? source, IFormatProvider provider)
830830
}
831831
}
832832
}
833+
else if (typeValue.IsFlagEnum())
834+
{
835+
ret = value!.ToString();
836+
}
833837
return ret;
834838
}
835839

0 commit comments

Comments
 (0)