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

Text Parameter should have the possibility to have ControlType.Select with options #39

Open
WimVdSElia opened this issue May 6, 2024 · 0 comments

Comments

@WimVdSElia
Copy link

WimVdSElia commented May 6, 2024

Hi @jsakamoto,

For the moment the text field only have color and default but I would like to have the possibility to choose from a list to facilite the rendering.

I was thinking to do like this in the TextParameterController.razor

@inherits ParameterControllerBase

@if (Parameter?.Control is ControlType.Color)
{
    var colorValue = (Value ?? Parameter.DefaultValue) as string;
    <ColorInput Value="@colorValue" PlaceHolder="Choose color..." OnInput="@((args) => OnInputAsync(args.Value))" />
}
else if (Parameter?.Control is ControlType.Select)
{
    var selectValue = (Value ?? Parameter.DefaultValue) as string;
    <Select Value="@selectValue" Items="Parameter.Options" OnChange="@((args) => OnInputAsync(args.Value))" />
}
else
{
    <TextArea PlaceHolder="Edit string..." Value="@(Value as string)" OnInput="@((args) => OnInputAsync(args.Value))" />
}

I was thinking to do like this in the ArgType.razor

@using System.Linq.Expressions;
@typeparam TComponent where TComponent : notnull
@typeparam TParameter

@code {
    [CascadingParameter]
    internal IEnumerable<ComponentParameter>? ComponentParameters { get; set; }

    [Parameter]
    public Expression<Func<TComponent, TParameter>>? For { get; set; }

    [Parameter]
    public ControlType Control { get; set; } = ControlType.Default;

    [Parameter]
    public object? DefaultValue { get; set; }

    [Parameter]
    public string[] Options { get; set; } = Array.Empty<string>();

    protected override void OnInitialized()
    {
        if (this.ComponentParameters == null) throw new InvalidOperationException($"The ComponentParameters cascading parameter is required.");
        var parameterName = ParameterExtractor.GetParameterName(this.For);
        if (this.ComponentParameters.TryGetByName(parameterName, out var parameter))
        {
            parameter.Control = this.Control;
            parameter.DefaultValue = this.DefaultValue;
            parameter.Options = this.Options;
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant