Skip to content

feat(RadioList): support generic SelectedItem #4773

@BrandonStudio

Description

@BrandonStudio

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In following demo, the value of RadioList is set to ChartType.Line.

.razor:

@{
  var types = new List<SelectedItem<ChartType>>
  {
    new() { Value = ChartType.Bar, Text = "柱状图" },
    new() { Value = ChartType.Pie, Text = "饼图" },
  };
}
<RadioList TValue="ChartType" Items="types" IsButton="true" @bind-Value="@ChartType"></RadioList>

.razor.cs:

private ChartType ChartType { get; set; } = ChartType.Bar;

Cause

When checking current and default value, it is checking whether SelectedItem.Value which is of type string rather than generic type TValue, resulting in inequality (L73) and null return value of FirstOrDefault() (L75).
image
ref:

if (AutoSelectFirstWhenValueIsNull && !Items.Any(i => i.Value == CurrentValueAsString))
{
CurrentValueAsString = Items.FirstOrDefault()?.Value ?? "";
}

Expected Behavior

if expression in L73 is false.
Even when it is true (current value has not been set), L75 should return ChartType.Bar, which is the first value of the given list.

Interactive render mode

Interactive Server (Interactive server-side rendering (interactive SSR) using Blazor Server)

Steps To Reproduce

Use List<SelectedItem<ChartType>> as source of RadioList.

.razor:

@{
  var types = new List<SelectedItem<ChartType>>
  {
    new() { Value = ChartType.Bar, Text = "柱状图" },
    new() { Value = ChartType.Pie, Text = "饼图" },
  };
}
<RadioList TValue="ChartType" Items="types" IsButton="true" @bind-Value="@ChartType"></RadioList>

.razor.cs:

private ChartType ChartType { get; set; } = ChartType.Bar;

Exceptions (if any)

No response

.NET Version

8

Anything else?

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions