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

Conditional inputs #6410

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify ConditionalDescriptor
Remove unused properties
Add docstrings
  • Loading branch information
Lukas Benner committed Feb 14, 2025
commit 62ea66a529dc976eda6e84dbaffd1a8527379b29
33 changes: 21 additions & 12 deletions src/apps/Elsa.Server.Web/ConditionalActivity.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
using Elsa.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Attributes.Conditional;
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;

namespace Elsa.Server.Web;

public class CondActivity : Activity
public class CondActivity : CodeActivity
{
[StateDropdownInput([
ConditionalInputOptions.WithDescription, "getTicket", "Get ticket",
ConditionalInputOptions.WithDescription, "addArticle", "Add artice"])]
public Input<string> ApiName{ get; set; } = default;

[StateDropdownInput(
["double", "string"],
DefaultValue = "double")]
public Input<string> ApiName{ get; set; } = default;

[ConditionalInput(["double"], Description = "Double")]
public Input<double> DoubleInput {get; set;} = default;

[ConditionalInput(["addArticle"], Description = "Article double", DefaultValue = 0.0, UIHint = InputUIHints.SingleLine)]
public Input<double> ActicleText {get; set;} = default;
[ConditionalInput(["string"], Description = "String")]
public Input<string> StringInput {get; set;} = default;

[ConditionalInput(["getTicket"], Description = "Article int", DefaultValue = 0, UIHint = InputUIHints.SingleLine)]
public Input<int> ActicleInt {get; set;} = default;
}
protected override void Execute(ActivityExecutionContext context)
{
object? value = null;
if (ApiName.Get(context) == "double")
value = DoubleInput.Get(context);
else if (ApiName.Get(context) == "string")
value = StringInput.Get(context);
Console.WriteLine(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ public record ConditionalDescriptor
{
public string[] ShowForStates { get; set; } = [];
public InputType InputType{ get; set; }
public DropDownStates? DropDownStates { get; set; }
public List<string>? DropDownStates { get; set; }

}

public record DropDownStates {
public string Mode {get; set;} = "Dynamic";
public required List<string> Descriptions {get; set;}
public required List<string> Ids { get; set; }
public required List<string> Options { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
using Elsa.Workflows.Models;
using Elsa.Workflows.UIHints;
using Elsa.Workflows.UIHints;

namespace Elsa.Workflows.Attributes.Conditional;

[AttributeUsage(AttributeTargets.Property)]
public class StateDropdownInput : InputAttribute
{
public StateDropdownInput(string[] states) : base()
public StateDropdownInput(string[] stateOptions) : base()
{
UIHint = InputUIHints.DropDown;
InputType = InputType.StateDropdown;
ParseDynamicStates(states);
}

public void ParseDynamicStates(string[] states)
{
int count = states.Length;
List<string> options = new();
List<string> descriptions = new();
List<string> ids = new();

for (int i = 0; i < count; ++i)
{
string current = states[i];
if (current == ConditionalInputOptions.WithoutDescription)
{
++i;
options.Add(states[i]);
descriptions.Add("");
ids.Add(states[i]);
}
else if (current == ConditionalInputOptions.WithDescription)
{
++i;
options.Add(states[i]);
ids.Add(states[i]);
++i;
descriptions.Add(states[i]);
}
else if (current == ConditionalInputOptions.WithIdAndDescription)
{
++i;
options.Add(states[i]);
++i;
ids.Add(states[i]);
++i;
descriptions.Add(states[i]);
}
}

Options = options;
DropDownStates = new DropDownStates{
Descriptions = descriptions,
Ids = ids,
Options = options
};
Options = stateOptions.ToList();
}
}
13 changes: 8 additions & 5 deletions src/modules/Elsa.Workflows.Core/Attributes/InputAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Elsa.Workflows.Models;

namespace Elsa.Workflows.Attributes;

/// <summary>
Expand Down Expand Up @@ -101,9 +99,14 @@ public class InputAttribute : Attribute
/// </summary>
public Type[]? UIHandlers { get; set; }

/// <summary>
/// A set of states for which this input should be shown
/// </summary>
public string[]? ShowForStates { get; set; }

public InputType InputType { get; set; } = InputType.Generic;

public DropDownStates? DropDownStates { get; set; }
/// <summary>
/// The type of the input.
/// To differentiate between state dropdown, conditional input and normal input
/// </summary>
public InputType InputType { get; set; } = InputType.Default;
}
7 changes: 5 additions & 2 deletions src/modules/Elsa.Workflows.Core/Attributes/InputType.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace Elsa.Workflows.Attributes;

/// <summary>
/// Type of an input.
/// To differentiate between state dropdown, conditional input and normal input.
/// </summary>
public enum InputType
{
Generic,
Default,
ConditionalInput,
StateDropdown

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ public record ConditionalDescriptor
{
public string[] ShowForStates { get; set; } = [];
public InputType InputType{ get; set; }
public DropDownStates? DropDownStates { get; set; }
public List<string>? DropDownStates { get; set; }

}

public record DropDownStates {
public string Mode {get; set;} = "Dynamic";
public required List<string> Descriptions {get; set;}
public required List<string> Ids { get; set; }
public required List<string> Options { get; set; }
}
7 changes: 3 additions & 4 deletions src/modules/Elsa.Workflows.Core/Services/ActivityDescriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,15 @@ public async Task<InputDescriptor> DescribeInputPropertyAsync(PropertyInfo prope
if (inputAttribute is null)
return inputDescriptor;

if (inputAttribute.InputType == InputType.Generic)
if (inputAttribute.InputType == InputType.Default)
return inputDescriptor;

var conditionalDescriptor = new ConditionalDescriptor{
ShowForStates = inputAttribute!.ShowForStates ?? [],
ShowForStates = inputAttribute.ShowForStates ?? [],
InputType = inputAttribute.InputType,
DropDownStates = inputAttribute.DropDownStates,
DropDownStates = inputAttribute.Options as List<string>
};
inputDescriptor.ConditionalDescriptor = conditionalDescriptor;

return inputDescriptor;
}

Expand Down