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 elsa 3 #5343

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Elsa.Workflows.Attributes.Conditional;
[AttributeUsage(AttributeTargets.Property)]
public class Input : InputAttribute
{
public Input(List<string>? descriptions = null)
{
if (descriptions is not null)
{
UIDescription = descriptions;
}
}

protected void Serialize(object? extendWithValues = null)
{
// Important:
// The description field is used to send the payload to the server.
Description = JsonConvert.SerializeObject(new
{
InputType = InputType,
Description = UIDescription
});

if (extendWithValues is not null)
{
var json = (JsonConvert.DeserializeAnonymousType(Description, extendWithValues) as JObject)!;
Description = json.ToString();
}
}

public string InputType { get; set; } = "Generic";
public List<string> UIDescription { get; set; } = ["Generic Input"];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Elsa.Workflows.Attributes.Conditional;
using Newtonsoft.Json;


namespace Elsa.Workflows.Attributes.Conditional;

[AttributeUsage(AttributeTargets.Property)]
public class ConditionalInput : Input
{
public ConditionalInput(string[] showForStates, string description = "") : base([description])
{
InputType = "ConditionalInput";
ShowForStates = showForStates;
Description = JsonConvert.SerializeObject(
new
{
InputType = InputType,
Description = UIDescription,
ShowForStates
}
);
}
public string[] ShowForStates { get; set; } = ["Default"];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Elsa.Workflows.Attributes.Conditional;
public static class ConditionalInputOptions
{
//
// Summary:
// State name (in this case the id is the state name)
public const string WithoutDescription = "s";
//
// Summary:
// State name, description (in this case the id is the state name)
public const string WithDescription = "sd";
//
// Summary:
// State name, id, description
public const string WithIdAndDescription = "sid";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using Elsa.Workflows.Attributes.Conditional;
using Elsa.Workflows.UIHints;
using Newtonsoft.Json;

namespace Elsa.Workflows.Attributes.Conditional;

[AttributeUsage(AttributeTargets.Property)]
public class StateDropdownInput : Input
{
public List<string> Ids { get; set; } = new();
public StateDropdownInput(string[] states) : base()
{
Init();
ParseDynamicStates(states);
Description = JsonConvert.SerializeObject(new
{
Mode = "Dynamic",
InputType = InputType,
Description = UIDescription,
Ids,
Options
});
}

private void Init()
{
UIHint = InputUIHints.DropDown;
InputType = "StateDropdown";
}

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;
UIDescription = descriptions;
Ids = ids;
}
}
Loading