Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
{
<BootstrapLabel required="@Required" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
}
@* @if (IsButton)
@if (IsButton)

Check failure on line 9 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'IsButton' does not exist in the current context

Check failure on line 9 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'IsButton' does not exist in the current context

Check failure on line 9 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'IsButton' does not exist in the current context

Check failure on line 9 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'IsButton' does not exist in the current context

Check failure on line 9 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / run test

The name 'IsButton' does not exist in the current context
{
<div @attributes="@AdditionalAttributes" class="radio-list-group">
<div class="@ButtonClassString" role="group">
@foreach (var item in Items)
{
<DynamicElement TagName="span" TriggerClick="!IsDisabled" OnClick="() => OnClick(item)" class="@GetButtonItemClassString(item)">

Check failure on line 15 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'BootstrapBlazor.Components.SelectedItem<TValue>' to 'BootstrapBlazor.Components.SelectedItem'

Check failure on line 15 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'BootstrapBlazor.Components.SelectedItem<TValue>' to 'BootstrapBlazor.Components.SelectedItem'

Check failure on line 15 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

Argument 1: cannot convert from 'BootstrapBlazor.Components.SelectedItem<TValue>' to 'BootstrapBlazor.Components.SelectedItem'

Check failure on line 15 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / run test

Argument 1: cannot convert from 'BootstrapBlazor.Components.SelectedItem<TValue>' to 'BootstrapBlazor.Components.SelectedItem'
@item.Text
</DynamicElement>
}
Expand All @@ -25,8 +25,7 @@
@foreach (var item in Items)
{
var content = GetChildContent(item);
<Radio Value="@item" Color="@Color" GroupName="@GroupName" IsDisabled="@GetDisabledState(item)" ShowAfterLabel="true" ShowLabel="false" DisplayText="@item.Text" State="@CheckState(item)" OnClick="OnClick" ChildContent="content!"></Radio>

Check failure on line 28 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'GetDisabledState' does not exist in the current context

Check failure on line 28 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'GetDisabledState' does not exist in the current context

Check failure on line 28 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / build

The name 'GetDisabledState' does not exist in the current context

Check failure on line 28 in src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor

View workflow job for this annotation

GitHub Actions / run test

The name 'GetDisabledState' does not exist in the current context
}
</div>
}
*@
157 changes: 103 additions & 54 deletions src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ namespace BootstrapBlazor.Components;
/// <summary>
/// 单选框组合组件
/// </summary>
[ExcludeFromCodeCoverage]
public partial class RadioListGeneric<TValue>
public partial class RadioListGeneric<TValue> : IModelEqualityComparer<TValue>
{
/// <summary>
/// 获得/设置 值为可为空枚举类型时是否自动添加空值 默认 false 自定义空值显示文本请参考 <see cref="NullItemText"/>
Expand All @@ -30,56 +29,106 @@ public partial class RadioListGeneric<TValue>
[Parameter]
public RenderFragment<SelectedItem<TValue>>? ItemTemplate { get; set; }

//private string? GroupName => Id;

//private string? ClassString => CssBuilder.Default("radio-list form-control")
// .AddClass("no-border", !ShowBorder && ValidCss != "is-invalid")
// .AddClass("is-vertical", IsVertical)
// .AddClass(CssClass).AddClass(ValidCss)
// .Build();

//private string? ButtonClassString => CssBuilder.Default("radio-list btn-group")
// .AddClass("disabled", IsDisabled)
// .AddClass("btn-group-vertical", IsVertical)
// .AddClassFromAttributes(AdditionalAttributes)
// .Build();

//private string? GetButtonItemClassString(SelectedItem item) => CssBuilder.Default("btn")
// .AddClass($"active bg-{Color.ToDescriptionString()}", CurrentValueAsString == item.Value)
// .Build();

///// <summary>
///// <inheritdoc/>
///// </summary>
//protected override void OnParametersSet()
//{
// var t = NullableUnderlyingType ?? typeof(TValue);
// if (t.IsEnum && Items == null)
// {
// Items = t.ToSelectList<TValue>((NullableUnderlyingType != null && IsAutoAddNullItem) ? new SelectedItem<TValue>(default, NullItemText) : null);
// }

// base.OnParametersSet();
//}

///// <summary>
///// 点击选择框方法
///// </summary>
//private async Task OnClick(SelectedItem<TValue> item)
//{
// if (!IsDisabled)
// {
// if (OnSelectedChanged != null)
// {
// await OnSelectedChanged(Items, Value);
// }
// StateHasChanged();
// }
//}

//private CheckboxState CheckState(SelectedItem<TValue> item) => this.Equals<TValue>(Value, item.Value) ? CheckboxState.Checked : CheckboxState.UnChecked;

//private RenderFragment? GetChildContent(SelectedItem<TValue> item) => ItemTemplate == null
// ? null
// : ItemTemplate(item);
/// <summary>
/// 获得/设置 是否显示边框 默认为 true
/// </summary>
[Parameter]
public bool ShowBorder { get; set; } = true;

/// <summary>
/// 获得/设置 是否为竖向排列 默认为 false
/// </summary>
[Parameter]
public bool IsVertical { get; set; }

/// <summary>
/// 获得/设置 按钮颜色 默认为 None 未设置
/// </summary>
[Parameter]
public Color Color { get; set; }

/// <summary>
/// 获得/设置 数据源
/// </summary>
[Parameter]
[NotNull]
public IEnumerable<SelectedItem<TValue>>? Items { get; set; }

/// <summary>
/// 获得/设置 SelectedItemChanged 方法
/// </summary>
[Parameter]
public Func<IEnumerable<SelectedItem<TValue>>, TValue, Task>? OnSelectedChanged { get; set; }

/// <summary>
/// 获得/设置 数据主键标识标签 默认为 <see cref="KeyAttribute"/><code><br /></code>用于判断数据主键标签,如果模型未设置主键时可使用 <see cref="ModelEqualityComparer"/> 参数自定义判断 <code><br /></code>数据模型支持联合主键
/// </summary>
[Parameter]
[NotNull]
public Type? CustomKeyAttribute { get; set; } = typeof(KeyAttribute);

/// <summary>
/// 获得/设置 比较数据是否相同回调方法 默认为 null
/// <para>提供此回调方法时忽略 <see cref="CustomKeyAttribute"/> 属性</para>
/// </summary>
[Parameter]
public Func<TValue, TValue, bool>? ModelEqualityComparer { get; set; }

private string? GroupName => Id;

private string? ClassString => CssBuilder.Default("radio-list form-control")
.AddClass("no-border", !ShowBorder && ValidCss != "is-invalid")
.AddClass("is-vertical", IsVertical)
.AddClass(CssClass).AddClass(ValidCss)
.Build();

private string? ButtonClassString => CssBuilder.Default("radio-list btn-group")
.AddClass("disabled", IsDisabled)
.AddClass("btn-group-vertical", IsVertical)
.AddClassFromAttributes(AdditionalAttributes)
.Build();

private string? GetButtonItemClassString(SelectedItem item) => CssBuilder.Default("btn")
.AddClass($"active bg-{Color.ToDescriptionString()}", CurrentValueAsString == item.Value)
.Build();

/// <summary>
/// <inheritdoc/>
/// </summary>
protected override void OnParametersSet()
{
var t = NullableUnderlyingType ?? typeof(TValue);
if (t.IsEnum && Items == null)
{
Items = t.ToSelectList<TValue>((NullableUnderlyingType != null && IsAutoAddNullItem) ? new SelectedItem<TValue>(default, NullItemText) : null);
}

base.OnParametersSet();
}

/// <summary>
/// 点击选择框方法
/// </summary>
private async Task OnClick(SelectedItem<TValue> item)
{
if (!IsDisabled)
{
if (OnSelectedChanged != null)
{
await OnSelectedChanged(Items, Value);
}
StateHasChanged();
}
}

private CheckboxState CheckState(SelectedItem<TValue> item) => this.Equals<TValue>(Value, item.Value) ? CheckboxState.Checked : CheckboxState.UnChecked;

private RenderFragment? GetChildContent(SelectedItem<TValue> item) => ItemTemplate == null
? null
: ItemTemplate(item);

/// <summary>
/// <inheritdoc/>
/// </summary>
public bool Equals(TValue? x, TValue? y) => this.Equals<TValue>(x, y);
}
Loading