Skip to content

Commit d4ad723

Browse files
authored
feat(ValidateForm): reduce render improve performance (#7439)
* refactor: 重构代码 * refactor: 增加文化参数 * test: 代码格式化 * refactor: 提高性能 * test: 更新单元测试 * chore: bump version 10.2.0 * refactor: 精简逻辑 * Revert "refactor: 精简逻辑" This reverts commit 24130a4. * refactor: 重构代码 * refactor: 精简代码 * refactor: 增加 Render 逻辑 * refactor: 重构代码
1 parent 1f60cea commit d4ad723

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.1.5-beta05</Version>
4+
<Version>10.2.0</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,9 +1086,9 @@ async Task<bool> DeleteItemsAsync()
10861086
// 由于数据删除导致页码会改变,尤其是最后一页
10871087
// 重新计算页码
10881088
// https://gitee.com/LongbowEnterprise/BootstrapBlazor/issues/I1UJSL
1089-
PageIndex = Math.Max(1, Math.Min(PageIndex, int.Parse(Math.Ceiling((TotalCount - SelectedRows.Count) * 1d / _pageItems).ToString())));
1090-
var items = PageItemsSource.Where(item => item >= (TotalCount - SelectedRows.Count));
1091-
if (items.Any())
1089+
PageIndex = GetSafePageIndex();
1090+
var items = PageItemsSource.Where(item => item >= (TotalCount - SelectedRows.Count)).ToList();
1091+
if (items.Count > 0)
10921092
{
10931093
_pageItems = Math.Min(_pageItems, items.Min());
10941094
}
@@ -1150,16 +1150,18 @@ private void QueryDynamicItems(QueryPageOptions queryOption, IDynamicObjectConte
11501150
{
11511151
TotalCount = items.Count();
11521152
PageCount = (int)Math.Ceiling(TotalCount * 1.0 / Math.Max(1, _pageItems));
1153-
PageIndex = Math.Max(1, Math.Min(PageIndex, int.Parse(Math.Ceiling((TotalCount - SelectedRows.Count) * 1d / _pageItems).ToString())));
1153+
PageIndex = GetSafePageIndex();
11541154
items = items.Skip((PageIndex - 1) * _pageItems).Take(_pageItems);
11551155
}
1156-
QueryItems = items.Cast<TItem>();
1156+
QueryItems = items.Cast<TItem>().ToList();
11571157

11581158
// 重置选中行
11591159
ResetSelectedRows(QueryItems);
11601160
}
11611161
}
11621162

1163+
private int GetSafePageIndex() => Math.Max(1, Math.Min(PageIndex, (int)Math.Ceiling((TotalCount - SelectedRows.Count) * 1.0 / _pageItems)));
1164+
11631165
private async Task ExecuteExportAsync(Func<Task<bool>> callback)
11641166
{
11651167
if (BeforeExportCallback != null)

src/BootstrapBlazor/Components/Validate/ValidateBase.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ protected virtual bool IsRequired() => ShowRequired ?? FieldIdentifier
251251
.Build();
252252

253253
/// <summary>
254-
/// SetParametersAsync 方法
254+
/// <inheritdoc/>
255255
/// </summary>
256256
/// <param name="parameters"></param>
257257
/// <returns></returns>
@@ -274,7 +274,7 @@ public override Task SetParametersAsync(ParameterView parameters)
274274
}
275275

276276
/// <summary>
277-
/// OnInitialized 方法
277+
/// <inheritdoc/>
278278
/// </summary>
279279
protected override void OnInitialized()
280280
{
@@ -291,7 +291,7 @@ protected override void OnInitialized()
291291
}
292292

293293
/// <summary>
294-
/// OnParametersSet 方法
294+
/// <inheritdoc/>
295295
/// </summary>
296296
protected override void OnParametersSet()
297297
{
@@ -326,6 +326,26 @@ protected override void OnParametersSet()
326326
}
327327
}
328328

329+
/// <summary>
330+
/// <inheritdoc/>
331+
/// </summary>
332+
/// <returns></returns>
333+
protected override bool ShouldRender()
334+
{
335+
if (ValidateForm == null)
336+
{
337+
return true;
338+
}
339+
340+
if (_shouldRender is true)
341+
{
342+
_shouldRender = false;
343+
return true;
344+
}
345+
346+
return _shouldRender ?? true;
347+
}
348+
329349
/// <summary>
330350
/// <inheritdoc/>
331351
/// </summary>
@@ -346,6 +366,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
346366
await ShowValidResult();
347367
}
348368
}
369+
370+
if (_shouldRender == false)
371+
{
372+
_shouldRender = null;
373+
}
349374
}
350375

351376
private string? _defaultRequiredErrorMessage;
@@ -456,6 +481,8 @@ private void ValidateType(ValidationContext context, List<ValidationResult> resu
456481
}
457482
}
458483

484+
private bool? _shouldRender = null;
485+
459486
/// <summary>
460487
/// 显示/隐藏验证结果方法
461488
/// </summary>
@@ -480,6 +507,7 @@ public virtual Task ToggleMessage(IReadOnlyCollection<ValidationResult> results)
480507
}
481508

482509
// 必须刷新一次 UI 保证状态正确
510+
_shouldRender = true;
483511
StateHasChanged();
484512
return Task.CompletedTask;
485513
}

test/UnitTest/Components/ValidateFormTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,9 +542,9 @@ public async Task ValidateFromCode_Ok()
542542
});
543543
Assert.Contains("form-control valid", cut.Markup);
544544

545-
var form = cut.Instance;
546-
await cut.InvokeAsync(() => form.Validate());
547-
Assert.Contains("form-control valid is-invalid", cut.Markup);
545+
var form = cut.Find("form");
546+
await cut.InvokeAsync(() => form.Submit());
547+
Assert.Contains("form-control invalid is-invalid", cut.Markup);
548548
}
549549

550550
[Fact]
@@ -566,7 +566,7 @@ public async Task Validate_Service_Ok()
566566
var msg = cut.FindComponent<MockInput<string>>().Instance.GetErrorMessage();
567567
Assert.Equal(HasServiceAttribute.Success, msg);
568568
}
569-
569+
570570
[Fact]
571571
public async Task TestService_Ok()
572572
{
@@ -771,7 +771,7 @@ private class HasService
771771
{
772772
[HasService]
773773
public string? Tag { get; set; }
774-
774+
775775
[TestValidateRule]
776776
public string? Tag2 { get; set; }
777777
}

0 commit comments

Comments
 (0)