Add support for nullable enum properties

This commit is contained in:
Sipke Schoorstra 2021-03-23 21:02:19 +01:00
parent 8ace98740a
commit 58f79cef11
3 changed files with 31 additions and 3 deletions

View file

@ -34,6 +34,13 @@ namespace Elsa.Activities.Telnyx.Activities
get => GetState<RingGroupStrategy>();
set => SetState(value);
}
[ActivityProperty]
public RingGroupStrategy? StrategyTest
{
get => GetState<RingGroupStrategy?>();
set => SetState(value);
}
[ActivityProperty(Hint =
"The 'from' number to be used as the caller id presented to the destination ('To' number). The number should be in +E164 format. This attribute will default to the 'From' number of the original call if omitted.")]

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AutoMapper.Internal;
using Elsa.Attributes;
using Elsa.Design;
using Humanizer;
@ -26,8 +28,8 @@ namespace Elsa.Metadata
if (activityPropertyAttribute.OptionsProvider == null)
{
if (activityPropertyInfo.PropertyType.IsEnum)
return activityPropertyInfo.PropertyType.GetEnumNames().Select(x => new SelectListItem(x.Humanize(LetterCasing.Title), x));
if (TryGetEnumOptions(activityPropertyInfo, out var items))
return items;
return activityPropertyAttribute.Options;
}
@ -38,5 +40,23 @@ namespace Elsa.Metadata
var provider = (IActivityPropertyOptionsProvider) ActivatorUtilities.GetServiceOrCreateInstance(scope.ServiceProvider, providerType);
return provider.GetOptions(activityPropertyInfo);
}
private bool TryGetEnumOptions(PropertyInfo activityPropertyInfo, out IList<SelectListItem>? items)
{
var isNullable = activityPropertyInfo.PropertyType.IsNullableType();
var propertyType = isNullable ? activityPropertyInfo.PropertyType.GetTypeOfNullable() : activityPropertyInfo.PropertyType;
items = null;
if (!propertyType.IsEnum)
return false;
items = propertyType.GetEnumNames().Select(x => new SelectListItem(x.Humanize(LetterCasing.Title), x)).ToList();
if (isNullable)
items.Insert(0, new SelectListItem("-", ""));
return true;
}
}
}

View file

@ -1,5 +1,6 @@
using System.Collections;
using System.Reflection;
using AutoMapper.Internal;
using Elsa.Attributes;
using Elsa.Design;
@ -25,7 +26,7 @@ namespace Elsa.Metadata
if (typeof(IEnumerable).IsAssignableFrom(type))
return ActivityPropertyUIHints.Dropdown;
if (type.IsEnum)
if (type.IsEnum || type.IsNullableType() && type.GetTypeOfNullable().IsEnum)
return ActivityPropertyUIHints.Dropdown;
return ActivityPropertyUIHints.SingleLine;