Skip to content

Commit

Permalink
feat(module: radio): support enum type for RadioGroup (#1840)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames authored Aug 14, 2021
1 parent bcd5e6b commit b2494a6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
16 changes: 11 additions & 5 deletions components/core/Helpers/EnumHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ public static class EnumHelper<T>
private static readonly Func<T, T, T> _aggregateFunction;

private static IEnumerable<T> _valueList;

private static IEnumerable<(T Value, string Label)> _valueLabelList;
private static Type _enumType;

static EnumHelper()
{
_enumType = THelper.GetUnderlyingType<T>();
_aggregateFunction = BuildAggregateFunction();
_valueList = Enum.GetValues(_enumType).Cast<T>();
_valueLabelList = _valueList.Select(value => (value, GetDisplayName(value)));
}

// There is no constraint or type check for type parameter T, be sure that T is an enumeration type
Expand All @@ -49,11 +50,16 @@ public static IEnumerable<T> GetValueList()
return _valueList;
}

public static string GetDisplayName(T t)
public static IEnumerable<(T Value, string Label)> GetValueLabelList()
{
return _valueLabelList;
}

public static string GetDisplayName(T enumValue)
{
var fieldInfo = _enumType.GetField(t.ToString());
return fieldInfo.GetCustomAttribute<DisplayAttribute>(true)?.Name ??
fieldInfo.Name;
var enumName = Enum.GetName(_enumType, enumValue);
var fieldInfo = _enumType.GetField(enumName);
return fieldInfo.GetCustomAttribute<DisplayAttribute>(true)?.Name ?? enumName;
}

private static Func<T, T, T> BuildAggregateFunction()
Expand Down
21 changes: 21 additions & 0 deletions components/radio/EnumRadioGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AntDesign
{
public class EnumRadioGroup<TEnum> : RadioGroup<TEnum>
{
public EnumRadioGroup()
{
Options = EnumHelper<TEnum>.GetValueLabelList()
.Select(x => new RadioOption<TEnum> { Value = x.Value, Label = x.Label })
.ToArray();
}
}
}
19 changes: 19 additions & 0 deletions site/AntDesign.Docs/Demos/Components/Radio/demo/EnumGroup.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@using System.ComponentModel.DataAnnotations

<EnumRadioGroup TEnum="Fruits" @bind-Value="_radioValue"></EnumRadioGroup>
<br />
Value: @_radioValue

@code {
Fruits _radioValue = Fruits.Apple;

enum Fruits
{
[Display(Name = "🍎 Apple")]
Apple,

Pear,

Orange
}
}
15 changes: 15 additions & 0 deletions site/AntDesign.Docs/Demos/Components/Radio/demo/enum-group.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
order: 8.5
title:
zh-CN: RadioGroup 组合 - 枚举方式
en-US: Radio group - enum type
---

## zh-CN

通过指定枚举类型,来渲染单选框。


## en-US

Render radios by specific enum type.

0 comments on commit b2494a6

Please sign in to comment.