@@ -8,8 +8,7 @@ namespace BootstrapBlazor.Components;
88/// <summary>
99/// 单选框组合组件
1010/// </summary>
11- [ ExcludeFromCodeCoverage ]
12- public partial class RadioListGeneric < TValue >
11+ public partial class RadioListGeneric < TValue > : IModelEqualityComparer < TValue >
1312{
1413 /// <summary>
1514 /// 获得/设置 值为可为空枚举类型时是否自动添加空值 默认 false 自定义空值显示文本请参考 <see cref="NullItemText"/>
@@ -30,56 +29,106 @@ public partial class RadioListGeneric<TValue>
3029 [ Parameter ]
3130 public RenderFragment < SelectedItem < TValue > > ? ItemTemplate { get ; set ; }
3231
33- //private string? GroupName => Id;
34-
35- //private string? ClassString => CssBuilder.Default("radio-list form-control")
36- // .AddClass("no-border", !ShowBorder && ValidCss != "is-invalid")
37- // .AddClass("is-vertical", IsVertical)
38- // .AddClass(CssClass).AddClass(ValidCss)
39- // .Build();
40-
41- //private string? ButtonClassString => CssBuilder.Default("radio-list btn-group")
42- // .AddClass("disabled", IsDisabled)
43- // .AddClass("btn-group-vertical", IsVertical)
44- // .AddClassFromAttributes(AdditionalAttributes)
45- // .Build();
46-
47- //private string? GetButtonItemClassString(SelectedItem item) => CssBuilder.Default("btn")
48- // .AddClass($"active bg-{Color.ToDescriptionString()}", CurrentValueAsString == item.Value)
49- // .Build();
50-
51- ///// <summary>
52- ///// <inheritdoc/>
53- ///// </summary>
54- //protected override void OnParametersSet()
55- //{
56- // var t = NullableUnderlyingType ?? typeof(TValue);
57- // if (t.IsEnum && Items == null)
58- // {
59- // Items = t.ToSelectList<TValue>((NullableUnderlyingType != null && IsAutoAddNullItem) ? new SelectedItem<TValue>(default, NullItemText) : null);
60- // }
61-
62- // base.OnParametersSet();
63- //}
64-
65- ///// <summary>
66- ///// 点击选择框方法
67- ///// </summary>
68- //private async Task OnClick(SelectedItem<TValue> item)
69- //{
70- // if (!IsDisabled)
71- // {
72- // if (OnSelectedChanged != null)
73- // {
74- // await OnSelectedChanged(Items, Value);
75- // }
76- // StateHasChanged();
77- // }
78- //}
79-
80- //private CheckboxState CheckState(SelectedItem<TValue> item) => this.Equals<TValue>(Value, item.Value) ? CheckboxState.Checked : CheckboxState.UnChecked;
81-
82- //private RenderFragment? GetChildContent(SelectedItem<TValue> item) => ItemTemplate == null
83- // ? null
84- // : ItemTemplate(item);
32+ /// <summary>
33+ /// 获得/设置 是否显示边框 默认为 true
34+ /// </summary>
35+ [ Parameter ]
36+ public bool ShowBorder { get ; set ; } = true ;
37+
38+ /// <summary>
39+ /// 获得/设置 是否为竖向排列 默认为 false
40+ /// </summary>
41+ [ Parameter ]
42+ public bool IsVertical { get ; set ; }
43+
44+ /// <summary>
45+ /// 获得/设置 按钮颜色 默认为 None 未设置
46+ /// </summary>
47+ [ Parameter ]
48+ public Color Color { get ; set ; }
49+
50+ /// <summary>
51+ /// 获得/设置 数据源
52+ /// </summary>
53+ [ Parameter ]
54+ [ NotNull ]
55+ public IEnumerable < SelectedItem < TValue > > ? Items { get ; set ; }
56+
57+ /// <summary>
58+ /// 获得/设置 SelectedItemChanged 方法
59+ /// </summary>
60+ [ Parameter ]
61+ public Func < IEnumerable < SelectedItem < TValue > > , TValue , Task > ? OnSelectedChanged { get ; set ; }
62+
63+ /// <summary>
64+ /// 获得/设置 数据主键标识标签 默认为 <see cref="KeyAttribute"/><code><br /></code>用于判断数据主键标签,如果模型未设置主键时可使用 <see cref="ModelEqualityComparer"/> 参数自定义判断 <code><br /></code>数据模型支持联合主键
65+ /// </summary>
66+ [ Parameter ]
67+ [ NotNull ]
68+ public Type ? CustomKeyAttribute { get ; set ; } = typeof ( KeyAttribute ) ;
69+
70+ /// <summary>
71+ /// 获得/设置 比较数据是否相同回调方法 默认为 null
72+ /// <para>提供此回调方法时忽略 <see cref="CustomKeyAttribute"/> 属性</para>
73+ /// </summary>
74+ [ Parameter ]
75+ public Func < TValue , TValue , bool > ? ModelEqualityComparer { get ; set ; }
76+
77+ private string ? GroupName => Id ;
78+
79+ private string ? ClassString => CssBuilder . Default ( "radio-list form-control" )
80+ . AddClass ( "no-border" , ! ShowBorder && ValidCss != "is-invalid" )
81+ . AddClass ( "is-vertical" , IsVertical )
82+ . AddClass ( CssClass ) . AddClass ( ValidCss )
83+ . Build ( ) ;
84+
85+ private string ? ButtonClassString => CssBuilder . Default ( "radio-list btn-group" )
86+ . AddClass ( "disabled" , IsDisabled )
87+ . AddClass ( "btn-group-vertical" , IsVertical )
88+ . AddClassFromAttributes ( AdditionalAttributes )
89+ . Build ( ) ;
90+
91+ private string ? GetButtonItemClassString ( SelectedItem item ) => CssBuilder . Default ( "btn" )
92+ . AddClass ( $ "active bg-{ Color . ToDescriptionString ( ) } ", CurrentValueAsString == item . Value )
93+ . Build ( ) ;
94+
95+ /// <summary>
96+ /// <inheritdoc/>
97+ /// </summary>
98+ protected override void OnParametersSet ( )
99+ {
100+ var t = NullableUnderlyingType ?? typeof ( TValue ) ;
101+ if ( t . IsEnum && Items == null )
102+ {
103+ Items = t . ToSelectList < TValue > ( ( NullableUnderlyingType != null && IsAutoAddNullItem ) ? new SelectedItem < TValue > ( default , NullItemText ) : null ) ;
104+ }
105+
106+ base . OnParametersSet ( ) ;
107+ }
108+
109+ /// <summary>
110+ /// 点击选择框方法
111+ /// </summary>
112+ private async Task OnClick ( SelectedItem < TValue > item )
113+ {
114+ if ( ! IsDisabled )
115+ {
116+ if ( OnSelectedChanged != null )
117+ {
118+ await OnSelectedChanged ( Items , Value ) ;
119+ }
120+ StateHasChanged ( ) ;
121+ }
122+ }
123+
124+ private CheckboxState CheckState ( SelectedItem < TValue > item ) => this . Equals < TValue > ( Value , item . Value ) ? CheckboxState . Checked : CheckboxState . UnChecked ;
125+
126+ private RenderFragment ? GetChildContent ( SelectedItem < TValue > item ) => ItemTemplate == null
127+ ? null
128+ : ItemTemplate ( item ) ;
129+
130+ /// <summary>
131+ /// <inheritdoc/>
132+ /// </summary>
133+ public bool Equals ( TValue ? x , TValue ? y ) => this . Equals < TValue > ( x , y ) ;
85134}
0 commit comments