Simplify CheckListOptions logic

Refactor `StaticCheckListOptionsProvider` to inherit from the new `CheckListOptionsProviderBase` and streamline checklist item generation logic.
This commit is contained in:
Sipke Schoorstra 2025-05-25 20:38:02 +02:00
parent d44b081848
commit f8968b5cfc
No known key found for this signature in database
GPG key ID: 5C10502B28A4268F
3 changed files with 25 additions and 16 deletions

View file

@ -0,0 +1,19 @@
using Elsa.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;
using Elsa.Workflows.UIHints.CheckList;
namespace Elsa.Server.Web.Activities;
public class CheckedActivity : CodeActivity<string[]>
{
[Input(UIHint = InputUIHints.CheckList, UIHandler = typeof(StaticCheckListOptionsProvider), Options = new[]{"A", "B", "C"})]
public Input<string[]> CheckedItems { get; set; }
protected override void Execute(ActivityExecutionContext context)
{
context.SetResult(CheckedItems.GetOrDefault(context));
}
}

View file

@ -6,32 +6,22 @@ namespace Elsa.Workflows.UIHints.CheckList;
/// <summary>
/// Provides static drop-down options for a given property.
/// </summary>
public class StaticCheckListOptionsProvider : IPropertyUIHandler
public class StaticCheckListOptionsProvider : CheckListOptionsProviderBase
{
/// <inheritdoc />
public ValueTask<IDictionary<string, object>> GetUIPropertiesAsync(PropertyInfo propertyInfo, object? context, CancellationToken cancellationToken = default)
protected override ValueTask<ICollection<CheckListItem>> GetItemsAsync(PropertyInfo propertyInfo, object? context, CancellationToken cancellationToken)
{
var inputAttribute = propertyInfo.GetCustomAttribute<InputAttribute>();
var inputOptions = inputAttribute?.Options;
var dictionary = new Dictionary<string, object>();
if (inputOptions == null)
return new(dictionary);
return new([]);
var selectListItems = (inputOptions as ICollection<string>)?.Select(x => new CheckListItem(x, x)).ToList();
if (selectListItems == null)
return new(dictionary);
var props = new CheckListProps
{
CheckList = new()
{
Items = selectListItems.ToList()
}
};
dictionary[InputUIHints.CheckList] = props;
return new(dictionary);
return new([]);
return new(selectListItems);
}
}