-
-
Notifications
You must be signed in to change notification settings - Fork 385
feat(CardUpload): support ValidateForm validate function #7432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4a28824
1589a8e
b691fc6
20fa20c
dbecb66
9c86d53
0d1f430
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ public partial class CardUpload<TValue> | |||||||||||||||||||||||||||||||
| .AddClass("is-valid", item is { Uploaded: true, Code: 0 }) | ||||||||||||||||||||||||||||||||
| .AddClass("is-invalid", item.Code != 0) | ||||||||||||||||||||||||||||||||
| .Build(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private string? ItemClassString => CssBuilder.Default("upload-item") | ||||||||||||||||||||||||||||||||
| .AddClass("disabled", CanUpload() == false) | ||||||||||||||||||||||||||||||||
| .Build(); | ||||||||||||||||||||||||||||||||
|
|
@@ -162,6 +163,54 @@ protected override async Task TriggerOnChanged(UploadFile file) | |||||||||||||||||||||||||||||||
| await base.TriggerOnChanged(file); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private IReadOnlyCollection<ValidationResult> _results = []; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
| /// <param name="results"></param> | ||||||||||||||||||||||||||||||||
| public override async Task ToggleMessage(IReadOnlyCollection<ValidationResult> results) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| _results = results; | ||||||||||||||||||||||||||||||||
| IsValid = results.Count == 0; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| ValidateModule ??= await LoadValidateModule(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var invalidItems = IsInValidOnAddItem | ||||||||||||||||||||||||||||||||
| ? [new { Id = AddId, _results.First().ErrorMessage }] | ||||||||||||||||||||||||||||||||
| : _results.Select(i => new { Id = i.MemberNames.FirstOrDefault(), i.ErrorMessage }).ToList(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var items = IsInValidOnAddItem | ||||||||||||||||||||||||||||||||
| ? [AddId] | ||||||||||||||||||||||||||||||||
| : Files.Select(i => i.ValidateId).ToList(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| var addId = IsInValidOnAddItem ? null : AddId; | ||||||||||||||||||||||||||||||||
| await ValidateModule.InvokeVoidAsync("executeUpload", items, invalidItems, addId); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private bool IsInValidOnAddItem => Files.Count == 0 && _results.Count > 0; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||||||||||||||
| protected override ValueTask ShowValidResult() => ValueTask.CompletedTask; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
| /// <param name="validateId"></param> | ||||||||||||||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||||||||||||||
| protected override async ValueTask RemoveValidResult(string? validateId = null) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| if (!string.IsNullOrEmpty(validateId)) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| await base.RemoveValidResult(validateId); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private string? AddId => $"{Id}_new"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| /// <summary> | |
| /// <inheritdoc/> | |
| /// </summary> | |
| /// <returns></returns> | |
| public override async ValueTask DisposeAsync() | |
| { | |
| if (ValidateModule != null) | |
| { | |
| await ValidateModule.InvokeVoidAsync("disposeUpload", items); | |
| } | |
| await base.DisposeAsync(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new validation functionality added to CardUpload (ToggleMessage, ShowValidResult, RemoveValidResult methods) lacks test coverage. Similar functionality in AvatarUpload has comprehensive test coverage in test/UnitTest/Components/UploadAvatarTest.cs (lines 224-287). Consider adding similar tests to ensure the validation features work correctly.