Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Include dropdown when using a nullable enum in an activity input #5313

Closed
nickrubenstein opened this issue May 1, 2024 · 2 comments
Closed
Labels
elsa 3 This issue is specific to Elsa 3 enhancement New feature or request
Milestone

Comments

@nickrubenstein
Copy link
Contributor

Enhancement Request

Enhancement Overview

Using the InputUIHints.DropDown on an Activity Input with an enum displays the dropdown with all the options in the studio activity editor as expected.

Changing the Input enum to be nullable makes the dropdown no longer show any options and does not work since it is a dropdown without any options to select.

Proposed Enhancement

Adding a few lines in StaticDropDownOptionsProvider class to check if wrappedPropertyType is nullable and has an underlying type that could be an enum. Then, add a blank item option in the enumValues list. I have taken a crack at it myself since it was so easy to run and debug elsa-core! It is such a small change and I do not know what this change could impact, but I can go through the PR process if requested.

Use Cases

I have an activity that queries data. There is a status property that is an enum that I would like to be able to filter by. In the meantime, I made a generic NullableEnumUIHandler to get around this and have a few type castings. It would definitely be cleaner code to simply add the InputUIHints.DropDown with the code below. Any pointers on a better way of having an enum input that can be changed back to being null after selecting an option would be great.

Additional Context

This generates an input with a dropdown of the enum values:

[Input(
    UIHint = InputUIHints.DropDown
)]
public Input<TestEnum> TestInput { get; set; } = new(default(TestEnum));

This ends up having a blank dropdown where nothing is selectable:

[Input(
    UIHint = InputUIHints.DropDown
)]
public Input<TestEnum?> TestNullableInput { get; set; } = new(default(TestEnum?));
@nickrubenstein nickrubenstein added the enhancement New feature or request label May 1, 2024
Copy link

glime-ai bot commented May 1, 2024

To address the issue of nullable enum types not displaying options in the dropdown, you can modify the StaticDropDownOptionsProvider class to handle nullable enums by checking if the property type is nullable and extracting the underlying enum type if it is. Then, you can add a blank item to the dropdown list to represent the null option.

Here's a code snippet that demonstrates how you might implement this:

public class StaticDropDownOptionsProvider : IActivityPropertyOptionsProvider
{
    public ValueTask<IEnumerable<SelectListItem>> GetOptionsAsync(ActivityPropertyOptionsContext context)
    {
        var propertyType = context.Property.PropertyType;
        var underlyingType = Nullable.GetUnderlyingType(propertyType);
        var enumType = underlyingType ?? propertyType;
        
        if (!enumType.IsEnum)
            throw new ArgumentException("The specified property type is not an enum type.");

        var enumValues = Enum.GetValues(enumType).Cast<object>()
            .Select(x => new SelectListItem(x.ToString(), x.ToString()))
            .ToList();

        // If the enum is nullable, add a blank option to represent null.
        if (underlyingType != null)
        {
            enumValues.Insert(0, new SelectListItem(string.Empty, null));
        }

        return new ValueTask<IEnumerable<SelectListItem>>(enumValues);
    }
}

In this code, underlyingType is used to determine if the property type is a nullable enum. If it is, a blank SelectListItem is inserted at the beginning of the list to represent the null option.

Regarding the enhancement request, it seems like you've already identified a solution and are considering submitting a pull request. If you believe your solution is robust and doesn't negatively impact other parts of the system, I would encourage you to contribute to the project by submitting a pull request. Make sure to include unit tests that cover the new functionality and document the changes in the PR description.

Before submitting the PR, you may also want to discuss your proposed changes with the Elsa community or maintainers to get feedback and ensure that it aligns with the project's goals and coding standards.

If you need further assistance or have additional questions, please let me know.

This comment was generated by Glime.

@sfmskywalker
Copy link
Member

Makes perfect sense. I noticed you went ahead and submitted a PR, thanks! I'll check it out shortly.

sfmskywalker added a commit that referenced this issue May 8, 2024
#5320)

Co-authored-by: Sipke Schoorstra <sipkeschoorstra@outlook.com>
@sfmskywalker sfmskywalker added the elsa 3 This issue is specific to Elsa 3 label May 8, 2024
@sfmskywalker sfmskywalker added this to the Elsa 3.2 milestone May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elsa 3 This issue is specific to Elsa 3 enhancement New feature or request
Projects
Status: Done
Development

No branches or pull requests

2 participants