Skip to content

Commit 1d713bc

Browse files
committed
refactor: 拆分 ShowAddButton 方法
1 parent b482895 commit 1d713bc

File tree

9 files changed

+32
-17
lines changed

9 files changed

+32
-17
lines changed

src/BootstrapBlazor/Components/Upload/AvatarUpload.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
<div @attributes="@AdditionalAttributes" class="@ClassString" id="@Id">
1010
<div class="upload-body is-avatar">
11-
@if (IsUploadButtonAtFirst && CheckCanUpload())
11+
@if (IsUploadButtonAtFirst && ShowAddButton())
1212
{
1313
@RenderAdd
1414
}
@@ -39,7 +39,7 @@
3939
}
4040
</div>
4141
}
42-
@if (!IsUploadButtonAtFirst && CheckCanUpload())
42+
@if (!IsUploadButtonAtFirst && ShowAddButton())
4343
{
4444
@RenderAdd
4545
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ public partial class AvatarUpload<TValue>
9696
return null;
9797
}
9898

99-
var state = item?.IsValid ?? IsValid;
99+
if(item == null)
100+
{
101+
return null;
102+
}
103+
104+
var state = item.IsValid ?? IsValid;
100105
if (state == null)
101106
{
102107
return null;

src/BootstrapBlazor/Components/Upload/ButtonUpload.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<BootstrapLabel required="@Required" ShowLabelTooltip="ShowLabelTooltip" Value="@DisplayText" />
88
}
99
<div @attributes="@AdditionalAttributes" class="@ClassString" id="@Id">
10-
<Button class="@BrowserButtonClassString" IsDisabled="@(!CheckCanUpload())" Size="Size" Icon="@BrowserButtonIcon" Text="@BrowserButtonText" Color="@BrowserButtonColor">
10+
<Button class="@BrowserButtonClassString" IsDisabled="@CheckStatus()" Size="Size" Icon="@BrowserButtonIcon" Text="@BrowserButtonText" Color="@BrowserButtonColor">
1111
@ChildContent
1212
</Button>
1313
@if (ShowUploadFileList)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,6 @@ protected override void OnParametersSet()
208208
BrowserButtonText ??= Localizer[nameof(BrowserButtonText)];
209209
BrowserButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.ButtonUploadBrowserButtonIcon);
210210
}
211+
212+
private bool CheckStatus() => IsDisabled || CanUpload() == false;
211213
}

src/BootstrapBlazor/Components/Upload/CardUpload.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
<div @attributes="@AdditionalAttributes" class="@ClassString" id="@Id" data-bb-previewer-id="@PreviewerId">
1010
<div class="@BodyClassString">
11-
@if (IsUploadButtonAtFirst && CheckCanUpload())
11+
@if (IsUploadButtonAtFirst && ShowAddButton())
1212
{
1313
@RenderAdd
1414
}
@@ -70,7 +70,7 @@
7070
</span>
7171
</div>
7272
}
73-
@if (!IsUploadButtonAtFirst && CheckCanUpload())
73+
@if (!IsUploadButtonAtFirst && ShowAddButton())
7474
{
7575
@RenderAdd
7676
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class CardUpload<TValue>
1919
.AddClass("is-invalid", item.Code != 0)
2020
.Build();
2121
private string? ItemClassString => CssBuilder.Default("upload-item")
22-
.AddClass("disabled", CheckCanUpload() == false)
22+
.AddClass("disabled", CanUpload() == false)
2323
.Build();
2424

2525
private string? BodyClassString => CssBuilder.Default("upload-body is-card")

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,16 @@ public partial class DropUpload
204204
private IStringLocalizer<UploadBase<string>>? Localizer { get; set; }
205205

206206
private string? ClassString => CssBuilder.Default("upload is-drop")
207-
.AddClass("disabled", CheckCanUpload() == false)
207+
.AddClass("disabled", CanUpload() == false)
208208
.AddClassFromAttributes(AdditionalAttributes)
209209
.Build();
210210

211211
private string? BodyClassString => CssBuilder.Default("upload-drop-body")
212-
.AddClass("btn-browser", CheckCanUpload())
212+
.AddClass("btn-browser", CanUpload())
213213
.Build();
214214

215215
private string? TextClassString => CssBuilder.Default("upload-drop-text")
216-
.AddClass("text-muted", CheckCanUpload() == false)
216+
.AddClass("text-muted", CanUpload() == false)
217217
.Build();
218218

219219
/// <summary>

src/BootstrapBlazor/Components/Upload/UploadBase.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,8 @@ protected List<UploadFile> GetUploadFiles()
260260
/// <para>Check whether can upload file.</para>
261261
/// </summary>
262262
/// <returns></returns>
263-
protected bool CheckCanUpload()
263+
protected bool CanUpload()
264264
{
265-
if (IsDisabled)
266-
{
267-
return false;
268-
}
269-
270265
// 允许多上传
271266
if (IsMultiple)
272267
{
@@ -277,6 +272,20 @@ protected bool CheckCanUpload()
277272
return Files.Count == 0;
278273
}
279274

275+
/// <summary>
276+
/// 判断是否显示新建按钮
277+
/// </summary>
278+
/// <returns></returns>
279+
protected bool ShowAddButton()
280+
{
281+
if (IsDisabled)
282+
{
283+
return Files.Count == 0;
284+
}
285+
286+
return CanUpload();
287+
}
288+
280289
/// <summary>
281290
/// 清空上传列表方法
282291
/// <para>Clear the upload files collection.</para>

test/UnitTest/Components/UploadAvatarTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

66
using Microsoft.AspNetCore.Components.Forms;
7-
using System.ComponentModel.DataAnnotations;
87

98
namespace UnitTest.Components;
109

0 commit comments

Comments
 (0)