Skip to content

Commit 8c7060a

Browse files
committed
refactor: 更新 MaxFileCount 逻辑
1 parent 18f273d commit 8c7060a

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

src/BootstrapBlazor/Components/Upload/AvatarUpload.razor.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,15 @@ public partial class AvatarUpload<TValue>
8787

8888
private string? GetValidStatus(UploadFile? item = null)
8989
{
90-
if (ValidateForm == null)
90+
if(item == null || IsDisabled || ValidateForm == null)
9191
{
9292
return null;
9393
}
9494

95-
if (IsDisabled)
96-
{
97-
return null;
98-
}
99-
100-
if(item == null)
101-
{
102-
return null;
103-
}
104-
105-
var state = item.IsValid ?? IsValid;
106-
if (state == null)
107-
{
108-
return null;
109-
}
110-
111-
return state.Value ? "is-valid" : "is-invalid";
95+
var state = item.IsValid;
96+
return state.HasValue
97+
? state.Value ? "is-valid" : "is-invalid"
98+
: null;
11299
}
113100

114101
/// <summary>

src/BootstrapBlazor/Components/Upload/UploadBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ protected override void OnParametersSet()
143143
/// <returns></returns>
144144
protected async Task OnFileChange(InputFileChangeEventArgs args)
145145
{
146-
var fileCount = MaxFileCount ?? args.FileCount;
147-
var items = args.GetMultipleFiles(fileCount).Select(f =>
146+
var fileCount = MaxFileCount ?? 0;
147+
fileCount = Math.Max(fileCount, args.FileCount);
148+
var items = args.GetMultipleFiles(args.FileCount).Take(fileCount).Select(f =>
148149
{
149150
var file = new UploadFile()
150151
{

test/UnitTest/Components/UploadAvatarTest.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Microsoft.AspNetCore.Components.Forms;
77
using System.Runtime.CompilerServices;
8+
using System.Threading.Tasks;
89

910
namespace UnitTest.Components;
1011

@@ -94,22 +95,6 @@ await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileCha
9495
});
9596
}
9697

97-
[Fact]
98-
public void MaxFileCount_Ok()
99-
{
100-
var cut = Context.RenderComponent<AvatarUpload<string>>(pb =>
101-
{
102-
pb.Add(a => a.IsMultiple, true);
103-
pb.Add(a => a.MaxFileCount, 2);
104-
pb.Add(a => a.DefaultFileList,
105-
[
106-
new UploadFile { FileName = "Test-File" },
107-
new UploadFile { FileName = "Test-File" }
108-
]);
109-
});
110-
Assert.DoesNotContain(".upload-item-plus", cut.Markup);
111-
}
112-
11398
[Fact]
11499
public async Task AvatarUpload_ValidateForm_Ok()
115100
{

test/UnitTest/Components/UploadInputTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ public void InputUpload_IsMultiple()
227227
Assert.False(button.IsDisabled());
228228
}
229229

230+
[Fact]
231+
public async Task MaxFileCount_Ok()
232+
{
233+
var cut = Context.RenderComponent<InputUpload<string>>(pb =>
234+
{
235+
pb.Add(a => a.IsMultiple, true);
236+
pb.Add(a => a.MaxFileCount, 4);
237+
});
238+
239+
var input = cut.FindComponent<InputFile>();
240+
await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileChangeEventArgs(new List<MockBrowserFile>()
241+
{
242+
new("test1.png"), new("test2.png"), new("test3.png")
243+
})));
244+
cut.Contains("test1.png;test2.png");
245+
cut.DoesNotContain("test3.png");
246+
}
247+
230248
private class Person
231249
{
232250
[Required]

0 commit comments

Comments
 (0)