Skip to content

Commit

Permalink
[HxMultiSelect] InputTextSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
Harvey1214 committed Feb 24, 2023
1 parent e682431 commit 8936baf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions Havit.Blazor.Components.Web.Bootstrap/Forms/HxMultiSelect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ public class HxMultiSelect<TValue, TItem> : HxInputBase<List<TValue>>, IInputWit
/// <summary>
/// Text to display in the input instead of a list of selected values.
/// </summary>
[Parameter] public string Text { get; set; }
[Parameter] public string InputText { get; set; }

/// <summary>
/// Selects the value to be displayed in the input instead of a list of selected values.
/// </summary>
[Parameter] public Func<IEnumerable<TItem>, string> InputTextSelector { get; set; }

/// <summary>
/// Input-group at the end of the input.
/// </summary>
[Parameter] public RenderFragment InputGroupEndTemplate { get; set; }


private List<TItem> itemsToRender;
private HxMultiSelectInternal<TValue, TItem> hxMultiSelectInternalComponent;

Expand Down Expand Up @@ -169,7 +175,7 @@ protected override void BuildRenderInput(RenderTreeBuilder builder)
builder.OpenComponent<HxMultiSelectInternal<TValue, TItem>>(100);
builder.AddAttribute(101, nameof(HxMultiSelectInternal<TValue, TItem>.InputId), InputId);
builder.AddAttribute(102, nameof(HxMultiSelectInternal<TValue, TItem>.InputCssClass), GetInputCssClassToRender());
builder.AddAttribute(103, nameof(HxMultiSelectInternal<TValue, TItem>.InputText), !string.IsNullOrEmpty(Text) ? Text : CurrentValueAsString);
builder.AddAttribute(103, nameof(HxMultiSelectInternal<TValue, TItem>.InputText), GetInputText());
builder.AddAttribute(104, nameof(HxMultiSelectInternal<TValue, TItem>.EnabledEffective), EnabledEffective);
builder.AddAttribute(105, nameof(HxMultiSelectInternal<TValue, TItem>.ItemsToRender), itemsToRender);
builder.AddAttribute(106, nameof(HxMultiSelectInternal<TValue, TItem>.TextSelector), TextSelector);
Expand All @@ -189,6 +195,21 @@ protected override void BuildRenderInput(RenderTreeBuilder builder)
builder.CloseComponent();
}

private string GetInputText()
{
if (!string.IsNullOrEmpty(InputText))
{
return InputText;
}
else if (InputTextSelector is null || Data is null || CurrentValue is null)
{
return CurrentValueAsString;
}

var currentItems = Data.Where(i => CurrentValue.Contains(SelectorHelpers.GetValue(ValueSelector, i)));
return SelectorHelpers.GetValue(InputTextSelector, currentItems);
}

/// <inheritdoc />
protected override string FormatValueAsString(List<TValue> value)
{
Expand Down

0 comments on commit 8936baf

Please sign in to comment.