Skip to content

Commit

Permalink
fixes #508 [HxCheckboxList] Default chip generation is unreasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
hakenr committed Mar 28, 2023
1 parent 0f1a965 commit 036042d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
9 changes: 9 additions & 0 deletions BlazorAppTest/Pages/InputsTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@
</TextTemplate>
</HxCheckbox>

<HxCheckboxList TItem="CultureInfo"
TValue="string"
Label="HxCheckboxList"
ItemTextSelector="@(item => item.EnglishName)"
ItemValueSelector="@(item => item.EnglishName)"
Data="@cultureInfos"
@bind-Value="@model.CultureInfoMultiSelectNames" />


<HxSwitch Text="Switch" @bind-Value="@context.BoolSwitch" ValidationMessageMode="@validationMessageMode" />

<HxSubmit Icon="@BootstrapIcon.Check" Color="ThemeColor.Primary">Submit</HxSubmit>
Expand Down

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

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

27 changes: 27 additions & 0 deletions Havit.Blazor.Components.Web.Bootstrap/Forms/HxCheckboxList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ protected override void BuildRenderInput(RenderTreeBuilder builder)

builder.AddAttribute(51, nameof(HxCheckbox.ValidationMessageMode), Havit.Blazor.Components.Web.Bootstrap.ValidationMessageMode.None);
builder.AddAttribute(52, nameof(HxCheckbox.Inline), this.Inline);
builder.AddAttribute(53, nameof(HxCheckbox.GenerateChip), false);

builder.AddMultipleAttributes(100, this.AdditionalAttributes);

Expand Down Expand Up @@ -179,4 +180,30 @@ public override ValueTask FocusAsync()
throw new NotSupportedException($"{nameof(FocusAsync)} is not supported on {nameof(HxCheckboxList<TValue, TItem>)}.");
}

/// <inheritdoc />
protected override string FormatValueAsString(List<TValue> value)
{
// Used for CurrentValueAsString (which is used for the chip generator).
if ((!value?.Any() ?? true) || (Data == null))
{
// don't care about chip generator, it does not call this method for null/empty value
return String.Empty;
}

// Take itemsToRender because they are sorted.
List<TItem> selectedItems = itemsToRender.Where(item => value.Contains(SelectorHelpers.GetValue<TItem, TValue>(ItemValueSelector, item))).ToList();
return String.Join(", ", selectedItems.Select(ItemTextSelector));
}

/// <inheritdoc />
protected override bool ShouldRenderChipGenerator()
{
return CurrentValue?.Any() ?? false;
}

/// <inheritdoc />
protected override List<TValue> GetChipRemoveValue()
{
return new List<TValue>();
}
}

0 comments on commit 036042d

Please sign in to comment.