Skip to content

Commit 80e65ea

Browse files
committed
refactor: 更新 MaxFileCount 逻辑
1 parent 8c7060a commit 80e65ea

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/BootstrapBlazor/Components/Upload/UploadBase.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,20 @@ protected override void OnParametersSet()
143143
/// <returns></returns>
144144
protected async Task OnFileChange(InputFileChangeEventArgs args)
145145
{
146-
var fileCount = MaxFileCount ?? 0;
147-
fileCount = Math.Max(fileCount, args.FileCount);
146+
var fileCount = args.FileCount;
147+
if (MaxFileCount.HasValue)
148+
{
149+
fileCount = MaxFileCount.Value;
150+
}
151+
152+
// 计算剩余可上传数量
153+
fileCount = fileCount - Files.Count;
154+
if (fileCount <= 0)
155+
{
156+
// 如果剩余可上传数量小于等于 0 则不允许继续上传
157+
return;
158+
}
159+
148160
var items = args.GetMultipleFiles(args.FileCount).Take(fileCount).Select(f =>
149161
{
150162
var file = new UploadFile()

test/UnitTest/Components/UploadInputTest.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileCha
190190
// 重置后不应该包含新上传的文件
191191
await cut.InvokeAsync(() => cut.Instance.Reset());
192192
Assert.DoesNotContain("test3.png;test4.png", cut.Markup);
193+
194+
cut.SetParametersAndRender(pb =>
195+
{
196+
pb.Add(a => a.DefaultFileList,
197+
[
198+
new UploadFile() { FileName = "test5.png" },
199+
new UploadFile() { FileName = "test6.png" }
200+
]);
201+
pb.Add(a => a.Value,
202+
[
203+
new MockBrowserFile("test5.png"),
204+
new MockBrowserFile("test6.png")
205+
]);
206+
});
207+
Assert.Contains("test5.png;test6.png", cut.Markup);
208+
await cut.InvokeAsync(() => cut.Instance.Reset());
209+
Assert.DoesNotContain("test5.png;test6.png", cut.Markup);
193210
}
194211

195212
[Fact]
@@ -233,7 +250,7 @@ public async Task MaxFileCount_Ok()
233250
var cut = Context.RenderComponent<InputUpload<string>>(pb =>
234251
{
235252
pb.Add(a => a.IsMultiple, true);
236-
pb.Add(a => a.MaxFileCount, 4);
253+
pb.Add(a => a.MaxFileCount, 2);
237254
});
238255

239256
var input = cut.FindComponent<InputFile>();

0 commit comments

Comments
 (0)