Skip to content

Commit 736a23f

Browse files
committed
refactor: 利用基类方法精简代码
1 parent 4282649 commit 736a23f

File tree

6 files changed

+26
-88
lines changed

6 files changed

+26
-88
lines changed

src/BootstrapBlazor/Components/Checkbox/CheckboxList.razor.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ public partial class CheckboxList<TValue> : ValidateBase<TValue>
9898
[Parameter]
9999
public Func<Task>? OnMaxSelectedCountExceed { get; set; }
100100

101-
[Inject]
102-
[NotNull]
103-
private IStringLocalizerFactory? LocalizerFactory { get; set; }
104-
105101
/// <summary>
106102
/// 获得 当前选项是否被禁用
107103
/// </summary>
@@ -121,23 +117,7 @@ protected override void OnInitialized()
121117
EnsureParameterValid();
122118

123119
// 处理 Required 标签
124-
if (EditContext != null && FieldIdentifier != null)
125-
{
126-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
127-
if (pi != null)
128-
{
129-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
130-
if (required != null)
131-
{
132-
Rules.Add(new RequiredValidator()
133-
{
134-
LocalizerFactory = LocalizerFactory,
135-
ErrorMessage = required.ErrorMessage,
136-
AllowEmptyString = required.AllowEmptyStrings
137-
});
138-
}
139-
}
140-
}
120+
AddRequiredValidator();
141121
}
142122

143123
/// <summary>

src/BootstrapBlazor/Components/Checkbox/CheckboxListGeneric.razor.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.Extensions.Localization;
7-
using System.Reflection;
87

98
namespace BootstrapBlazor.Components;
109

@@ -111,10 +110,6 @@ public partial class CheckboxListGeneric<TValue> : IModelEqualityComparer<TValue
111110
[Parameter]
112111
public Func<Task>? OnMaxSelectedCountExceed { get; set; }
113112

114-
[Inject]
115-
[NotNull]
116-
private IStringLocalizerFactory? LocalizerFactory { get; set; }
117-
118113
/// <summary>
119114
/// 获得 当前选项是否被禁用
120115
/// </summary>
@@ -132,23 +127,7 @@ protected override void OnInitialized()
132127
base.OnInitialized();
133128

134129
// 处理 Required 标签
135-
if (EditContext != null && FieldIdentifier != null)
136-
{
137-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
138-
if (pi != null)
139-
{
140-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
141-
if (required != null)
142-
{
143-
Rules.Add(new RequiredValidator()
144-
{
145-
LocalizerFactory = LocalizerFactory,
146-
ErrorMessage = required.ErrorMessage,
147-
AllowEmptyString = required.AllowEmptyStrings
148-
});
149-
}
150-
}
151-
}
130+
AddRequiredValidator();
152131
}
153132

154133
/// <summary>

src/BootstrapBlazor/Components/Select/SelectObject.razor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,6 @@ public partial class SelectObject<TItem>
111111
[NotNull]
112112
private IStringLocalizer<Select<TItem>>? Localizer { get; set; }
113113

114-
/// <summary>
115-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
116-
/// </summary>
117-
[Inject]
118-
[NotNull]
119-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
120-
121114
/// <summary>
122115
/// 获得 input 组件 Id 方法
123116
/// </summary>
@@ -140,10 +133,7 @@ protected override void OnInitialized()
140133
{
141134
base.OnInitialized();
142135

143-
if (ValidateForm != null)
144-
{
145-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = "{0} is required." });
146-
}
136+
AddRequiredValidator();
147137
_context = new InternalSelectObjectContext<TItem>() { Component = this };
148138
}
149139

src/BootstrapBlazor/Components/Select/SelectTable.razor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,6 @@ namespace BootstrapBlazor.Components;
178178
[NotNull]
179179
private IStringLocalizer<Select<TItem>>? Localizer { get; set; }
180180

181-
/// <summary>
182-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
183-
/// </summary>
184-
[Inject]
185-
[NotNull]
186-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
187-
188181
/// <summary>
189182
/// 获得 input 组件 Id 方法
190183
/// </summary>
@@ -205,10 +198,7 @@ protected override void OnInitialized()
205198
{
206199
base.OnInitialized();
207200

208-
if (ValidateForm != null)
209-
{
210-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = "{0} is required." });
211-
}
201+
AddRequiredValidator();
212202
}
213203

214204
/// <summary>

src/BootstrapBlazor/Components/Select/SelectTree.razor.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,17 @@ public partial class SelectTree<TValue> : IModelEqualityComparer<TValue>
155155
[NotNull]
156156
private IIconTheme? IconTheme { get; set; }
157157

158+
/// <summary>
159+
/// <inheritdoc/>
160+
/// </summary>
161+
protected override void OnInitialized()
162+
{
163+
base.OnInitialized();
164+
165+
// 处理 Required 标签
166+
AddRequiredValidator();
167+
}
168+
158169
/// <summary>
159170
/// <inheritdoc/>
160171
/// </summary>
@@ -171,12 +182,20 @@ protected override async Task OnInitializedAsync()
171182
/// <summary>
172183
/// <inheritdoc/>
173184
/// </summary>
174-
protected override async Task OnParametersSetAsync()
185+
protected override void OnParametersSet()
175186
{
176-
await base.OnParametersSetAsync();
187+
base.OnParametersSet();
177188

178189
DropdownIcon ??= IconTheme.GetIconByKey(ComponentIcons.SelectTreeDropdownIcon);
179190
PlaceHolder ??= Localizer[nameof(PlaceHolder)];
191+
}
192+
193+
/// <summary>
194+
/// <inheritdoc/>
195+
/// </summary>
196+
protected override async Task OnParametersSetAsync()
197+
{
198+
await base.OnParametersSetAsync();
180199

181200
Items ??= [];
182201

src/BootstrapBlazor/Components/Transfer/Transfer.razor.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,6 @@ public partial class Transfer<TValue>
188188
[Parameter]
189189
public RenderFragment<SelectedItem>? RightItemTemplate { get; set; }
190190

191-
/// <summary>
192-
/// 获得/设置 IStringLocalizerFactory 注入服务实例 默认为 null
193-
/// </summary>
194-
[Inject]
195-
[NotNull]
196-
public IStringLocalizerFactory? LocalizerFactory { get; set; }
197-
198191
[Inject]
199192
[NotNull]
200193
private IIconTheme? IconTheme { get; set; }
@@ -206,21 +199,8 @@ protected override void OnInitialized()
206199
{
207200
base.OnInitialized();
208201

209-
OnSetItemClass ??= _ => null;
210-
211202
// 处理 Required 标签
212-
if (FieldIdentifier != null)
213-
{
214-
var pi = FieldIdentifier.Value.Model.GetType().GetPropertyByName(FieldIdentifier.Value.FieldName);
215-
if (pi != null)
216-
{
217-
var required = pi.GetCustomAttribute<RequiredAttribute>(true);
218-
if (required != null)
219-
{
220-
Rules.Add(new RequiredValidator() { LocalizerFactory = LocalizerFactory, ErrorMessage = required.ErrorMessage, AllowEmptyString = required.AllowEmptyStrings });
221-
}
222-
}
223-
}
203+
AddRequiredValidator();
224204
}
225205

226206
/// <summary>

0 commit comments

Comments
 (0)