Skip to content

Commit fd8fde4

Browse files
fix(CardUpload): not allow preview when set ShowZoomButton to false (#7715)
* CardUpload_FixeZoom 修正CardUpload组件允许上传多张图片时,当ShowZoomButton为false时,单击上传好的图片时预览不正确 * refactor: 统一标签宽度 * style: 调整样式 * refactor: 增加 is-preview 样式 * refactor: 根据 is-preview 检查是否可预览 * test: 更新单元测试 --------- Co-authored-by: Argo Zhang <argo@live.ca>
1 parent 662be40 commit fd8fde4

File tree

8 files changed

+20
-17
lines changed

8 files changed

+20
-17
lines changed

src/BootstrapBlazor.Server/Components/Samples/UploadAvatars.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Introduction="@Localizer["AvatarUploadIntro"]"
1616
Name="Normal">
1717
<section ignore>
18-
<div class="row g-3">
18+
<div class="row g-3" style="--bb-input-group-label-width: 186px;">
1919
<div class="col-12 col-sm-6">
2020
<BootstrapInputGroup>
2121
<BootstrapInputGroupLabel DisplayText="IsDisabled"></BootstrapInputGroupLabel>

src/BootstrapBlazor.Server/Components/Samples/UploadButtons.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Introduction="@Localizer["ButtonUploadIntro"]"
1616
Name="Normal">
1717
<section ignore>
18-
<div class="row g-3">
18+
<div class="row g-3" style="--bb-input-group-label-width: 186px;">
1919
<div class="col-12 col-sm-4">
2020
<BootstrapInputGroup>
2121
<BootstrapInputGroupLabel DisplayText="IsDisabled"></BootstrapInputGroupLabel>

src/BootstrapBlazor.Server/Components/Samples/UploadCards.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Introduction="@Localizer["ButtonUploadIntro"]"
1616
Name="Normal">
1717
<section ignore>
18-
<div class="row g-3">
18+
<div class="row g-3" style="--bb-input-group-label-width: 186px;">
1919
<div class="col-12 col-sm-6 col-xl-3">
2020
<BootstrapInputGroup>
2121
<BootstrapInputGroupLabel DisplayText="IsDisabled"></BootstrapInputGroupLabel>

src/BootstrapBlazor.Server/Components/Samples/UploadDrops.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<DemoBlock Title="@Localizer["DropUploadTitle"]" Introduction="@Localizer["DropUploadIntro"]" Name="Normal">
1515
<section ignore>
16-
<div class="row g-3">
16+
<div class="row g-3" style="--bb-input-group-label-width: 186px;">
1717
<div class="col-12 col-sm-6">
1818
<BootstrapInputGroup>
1919
<BootstrapInputGroupLabel DisplayText="IsDisabled"></BootstrapInputGroupLabel>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public partial class CardUpload<TValue>
1818
.Build();
1919

2020
private string? GetItemClassString(UploadFile item) => CssBuilder.Default(ItemClassString)
21+
.AddClass("is-preview", ShowZoomButton)
2122
.AddClass("is-valid", item is { Uploaded: true, Code: 0 })
2223
.AddClass("is-invalid", item.Code != 0)
2324
.Build();

src/BootstrapBlazor/Components/Upload/InputUpload.razor.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,11 @@
179179
width: var(--bb-upload-card-width);
180180
height: var(--bb-upload-card-height);
181181
position: relative;
182-
cursor: pointer;
183182
overflow: hidden;
183+
184+
&.is-preview img {
185+
cursor: pointer;
186+
}
184187
}
185188

186189
.upload .upload-body.is-card .upload-item .upload-item-actions {

src/BootstrapBlazor/wwwroot/modules/upload.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,21 @@ export function init(id) {
6262
const event = new Event('change', { bubbles: true })
6363
inputFile.dispatchEvent(event)
6464
})
65-
6665
const getIndex = target => {
6766
let index = 0;
68-
let button = target;
69-
if (button.tagName === 'IMG') {
70-
button = button.closest('.upload-item').querySelector('.btn-zoom');
71-
}
72-
if (button) {
73-
const buttons = [...el.querySelectorAll('.btn-zoom')]
74-
index = buttons.indexOf(button);
75-
}
67+
let item = target.closest('.upload-item');
68+
const items = [...el.querySelectorAll('.upload-item')]
69+
index = items.indexOf(item);
7670
return index;
7771
};
7872

7973
EventHandler.on(el, 'click', '.btn-zoom, .upload-item-body-image', e => {
80-
const prev = Data.get(el.getAttribute('data-bb-previewer-id'));
81-
prev.viewer.updatePrevList([...el.querySelectorAll('.upload-body img')].map(v => v.src));
82-
prev.viewer.show(getIndex(e.delegateTarget));
74+
const zoom = e.delegateTarget.closest('.upload-item').classList.contains('is-preview');
75+
if (zoom) {
76+
const prev = Data.get(el.getAttribute('data-bb-previewer-id'));
77+
prev.viewer.updatePrevList([...el.querySelectorAll('.upload-body img')].map(v => v.src));
78+
prev.viewer.show(getIndex(e.delegateTarget));
79+
}
8380
})
8481
}
8582

test/UnitTest/Components/UploadCardTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public async Task CardUpload_Ok()
3939
cut.Contains("bb-previewer collapse active");
4040
cut.Contains("aria-label=\"zoom\"");
4141
cut.Contains("aria-label=\"delete\"");
42+
cut.Contains("is-preview");
4243

4344
cut.Render(pb =>
4445
{
@@ -129,6 +130,7 @@ await cut.InvokeAsync(() => input.Instance.OnChange.InvokeAsync(new InputFileCha
129130
});
130131
cut.DoesNotContain("aria-label=\"zoom\"");
131132
cut.DoesNotContain("aria-label=\"delete\"");
133+
cut.DoesNotContain("is-preview");
132134
}
133135

134136
[Fact]

0 commit comments

Comments
 (0)