Skip to content

Commit f0dfab0

Browse files
authored
perf(ValidateForm): support validate multiple validate form (#4382)
* perf: 优化性能 * refactor: 精简代码 * refactor: 重构代码提高可读性 * revert: 移除后来增加的代码 * test: 更新单元测试
1 parent b18a550 commit f0dfab0

File tree

4 files changed

+14
-66
lines changed

4 files changed

+14
-66
lines changed

src/BootstrapBlazor/Components/Validate/ValidateBase.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,6 @@ public virtual void ToggleMessage(IEnumerable<ValidationResult> results)
474474
OnValidate(IsValid);
475475
}
476476

477-
if (!string.IsNullOrEmpty(ErrorMessage))
478-
{
479-
ValidateForm?.AddValidationComponent(Id);
480-
}
481-
482477
// 必须刷新一次 UI 保证状态正确
483478
StateHasChanged();
484479
}

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,6 @@ protected override void OnParametersSet()
137137
}
138138
}
139139

140-
/// <summary>
141-
/// <inheritdoc/>
142-
/// </summary>
143-
/// <param name="firstRender"></param>
144-
/// <returns></returns>
145-
protected override async Task OnAfterRenderAsync(bool firstRender)
146-
{
147-
await base.OnAfterRenderAsync(firstRender);
148-
149-
if (!ShowAllInvalidResult && _invalidComponents.Count > 0)
150-
{
151-
await InvokeVoidAsync("update", Id, _invalidComponents);
152-
_invalidComponents.Clear();
153-
}
154-
}
155-
156140
/// <summary>
157141
/// 添加数据验证组件到 EditForm 中
158142
/// </summary>
@@ -177,13 +161,14 @@ internal void AddValidator((string FieldName, Type ModelType) key, (FieldIdentif
177161
/// <param name="errorMessage">错误描述信息,可为空,为空时查找资源文件</param>
178162
public void SetError<TModel>(Expression<Func<TModel, object?>> expression, string errorMessage)
179163
{
180-
if (expression.Body is UnaryExpression unary && unary.Operand is MemberExpression mem)
181-
{
182-
InternalSetError(mem, errorMessage);
183-
}
184-
else if (expression.Body is MemberExpression exp)
164+
switch (expression.Body)
185165
{
186-
InternalSetError(exp, errorMessage);
166+
case UnaryExpression { Operand: MemberExpression mem }:
167+
InternalSetError(mem, errorMessage);
168+
break;
169+
case MemberExpression exp:
170+
InternalSetError(exp, errorMessage);
171+
break;
187172
}
188173
}
189174

@@ -633,9 +618,7 @@ private async Task OnInvalidSubmitForm(EditContext context)
633618
public bool Validate()
634619
{
635620
_invalid = true;
636-
var ret = Validator.Validate() && !_invalid;
637-
StateHasChanged();
638-
return ret;
621+
return Validator.Validate() && !_invalid;
639622
}
640623

641624
/// <summary>
@@ -649,12 +632,12 @@ public void NotifyFieldChanged(in FieldIdentifier fieldIdentifier, object? value
649632
OnFieldValueChanged?.Invoke(fieldIdentifier.FieldName, value);
650633
}
651634

652-
private readonly List<string> _invalidComponents = [];
635+
//private readonly List<string> _invalidComponents = [];
653636

654-
internal void AddValidationComponent(string id)
655-
{
656-
_invalidComponents.Add(id);
657-
}
637+
//internal void AddValidationComponent(string id)
638+
//{
639+
// _invalidComponents.Add(id);
640+
//}
658641

659642
/// <summary>
660643
/// 获取 当前表单值改变的属性集合

src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,6 @@ export function init(id) {
1414
})
1515
}
1616

17-
export function update(id, invalidIds) {
18-
const el = document.getElementById(id);
19-
const items = [...el.querySelectorAll(".is-invalid")];
20-
const invalidElements = invalidIds.map(cId => {
21-
const item = document.getElementById(cId);
22-
let order = items.indexOf(item);
23-
if (order === -1) {
24-
const invalidEl = item.querySelector(".is-invalid");
25-
if (invalidEl) {
26-
order = items.indexOf(invalidEl);
27-
}
28-
}
29-
return { item, order };
30-
});
31-
invalidElements.sort((a, b) => b.order - a.order);
32-
33-
const invalid = invalidElements.pop();
34-
if (invalid) {
35-
const handler = setInterval(() => {
36-
const tip = bootstrap.Tooltip.getInstance(invalid.item)
37-
if (tip) {
38-
clearInterval(handler);
39-
if (!tip._isShown()) {
40-
tip.show();
41-
}
42-
}
43-
}, 20);
44-
}
45-
}
46-
4717
export function dispose(id) {
4818
const el = document.getElementById(id)
4919
EventHandler.off(el, 'keydown')

test/UnitTest/Components/ValidateFormTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public async Task ValidateFromCode_Ok()
524524

525525
var form = cut.Instance;
526526
await cut.InvokeAsync(() => form.Validate());
527-
Assert.Contains("form-control invalid is-invalid", cut.Markup);
527+
Assert.Contains("form-control valid is-invalid", cut.Markup);
528528
}
529529

530530
[Fact]

0 commit comments

Comments
 (0)