Skip to content

feat(AvatarUpload): preview when click backdrop#7368

Merged
ArgoZhang merged 21 commits intomainfrom
momiji/avatar-upload-add-zoom-func
Dec 19, 2025
Merged

feat(AvatarUpload): preview when click backdrop#7368
ArgoZhang merged 21 commits intomainfrom
momiji/avatar-upload-add-zoom-func

Conversation

@momijijin
Copy link
Copy Markdown
Collaborator

@momijijin momijijin commented Dec 18, 2025

  1. Make the hover style of the delete button more prominent,
  2. Rewrite zoom onclick event and set stopPropagation.

Link issues

fixes #7340

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Refine avatar upload preview behavior and delete control styling.

New Features:

  • Enable clicking the avatar upload action area to open the preview for the corresponding image based on its identifier.

Bug Fixes:

  • Align the avatar preview with the correct file by using the file's validate identifier to determine the current index and preventing delete clicks from triggering preview.

Enhancements:

  • Improve the visual prominence and hover feedback of the avatar delete button in the upload list.

1. Make the hover style of the delete button more prominent,
2. Rewrite zoom onclick event and set stopPropagation.
@bb-auto
Copy link
Copy Markdown

bb-auto bot commented Dec 18, 2025

Thanks for your PR, @momijijin. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@bb-auto bb-auto bot requested a review from ArgoZhang December 18, 2025 10:35
@bb-auto bb-auto bot added the enhancement New feature or request label Dec 18, 2025
@bb-auto bb-auto bot added this to the v10.1.0 milestone Dec 18, 2025
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Dec 18, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors AvatarUpload preview behavior to open the correct image based on the clicked item’s validation id and enhances the avatar delete button styling and interaction to avoid accidental preview triggers.

Sequence diagram for updated avatar preview and delete interactions

sequenceDiagram
    actor User
    participant AvatarUploadComponent
    participant PreviewMethod
    participant JSInterop

    rect rgb(230,230,250)
        User->>AvatarUploadComponent: Click action area (not delete)
        AvatarUploadComponent->>PreviewMethod: Preview(item.ValidateId)
        PreviewMethod->>PreviewMethod: Validate ShowPreviewList
        PreviewMethod->>PreviewMethod: Check id is not null/whitespace
        PreviewMethod->>PreviewMethod: Find file where ValidateId == id
        alt File found
            PreviewMethod->>JSInterop: InvokeVoidAsync preview(PreviewerId, matchedIndex)
        else File not found
            PreviewMethod-->>AvatarUploadComponent: No action
        end
    end

    rect rgb(230,255,230)
        User->>AvatarUploadComponent: Click delete icon
        AvatarUploadComponent->>AvatarUploadComponent: stopPropagation on delete click
        AvatarUploadComponent->>AvatarUploadComponent: OnFileDelete(item)
        AvatarUploadComponent-->>User: File removed without triggering preview
    end
Loading

File-Level Changes

Change Details Files
Align avatar preview to the clicked avatar item rather than always opening the first item.
  • Change Preview method signature to accept a validation id parameter.
  • Guard preview invocation by ensuring the id is non-empty and exists in the Files collection.
  • Compute the index of the file with the matching validation id and pass that index to the JS preview helper.
src/BootstrapBlazor/Components/Upload/AvatarUpload.razor.cs
Trigger preview from the avatar item action area while preventing delete clicks from bubbling and opening the preview.
  • Attach a click handler on the upload item actions container to call Preview with the item’s ValidateId.
  • Add stopPropagation to the delete button click to prevent it from triggering the parent preview handler.
  • Keep delete behavior wired to OnFileDelete for the selected item.
src/BootstrapBlazor/Components/Upload/AvatarUpload.razor
Make the avatar delete button visually more prominent and clearer on hover.
  • Add padding, rounded border radius, and a danger-colored border to the delete control when the avatar item is hovered.
  • Introduce a hover style for the delete control that inverts colors (white text on danger background) to emphasize interactivity.
src/BootstrapBlazor/Components/Upload/InputUpload.razor.scss

Assessment against linked issues

Issue Objective Addressed Explanation
#7340 Add a feature to AvatarUpload that allows viewing the uploaded image at full/original size, similar to CardUpload’s original image preview.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

sourcery-ai[bot]
sourcery-ai bot previously approved these changes Dec 18, 2025
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • In Preview(string id) you traverse Files up to three times (Any, First, then IndexOf); consider using a single pass (e.g., var index = Files.FindIndex(f => f.ValidateId == id); and checking index >= 0) to avoid redundant enumeration.
  • The new Preview(string id) signature changes the public API; if this method is used elsewhere, consider adding an overload or a default parameter-less wrapper to avoid breaking existing consumers.
  • In the Razor markup, the @onclick="@(() => Preview(item.ValidateId ?? ""))" handler could be simplified to avoid the empty-string fallback and unnecessary lambda allocation, e.g., by passing item.ValidateId! and letting the Preview method handle null checks.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `Preview(string id)` you traverse `Files` up to three times (`Any`, `First`, then `IndexOf`); consider using a single pass (e.g., `var index = Files.FindIndex(f => f.ValidateId == id);` and checking `index >= 0`) to avoid redundant enumeration.
- The new `Preview(string id)` signature changes the public API; if this method is used elsewhere, consider adding an overload or a default parameter-less wrapper to avoid breaking existing consumers.
- In the Razor markup, the `@onclick="@(() => Preview(item.ValidateId ?? ""))"` handler could be simplified to avoid the empty-string fallback and unnecessary lambda allocation, e.g., by passing `item.ValidateId!` and letting the `Preview` method handle null checks.

## Individual Comments

### Comment 1
<location> `src/BootstrapBlazor/Components/Upload/AvatarUpload.razor.cs:157-161` </location>
<code_context>
     /// </summary>
     /// <returns></returns>
-    public async Task Preview()
+    public async Task Preview(string id)
     {
-        if(ShowPreviewList)
+        if(ShowPreviewList && !string.IsNullOrWhiteSpace(id) && Files.Any(r => r.ValidateId == id))
         {
-            await InvokeVoidAsync("preview", PreviewerId, 0);
+            await InvokeVoidAsync("preview", PreviewerId, Files.IndexOf(Files.First(r => r.ValidateId == id)));
         }
     }
</code_context>

<issue_to_address>
**suggestion:** Avoid double enumeration over `Files` when resolving the index for preview.

`Any` + `First` + `IndexOf` walks `Files` multiple times for the same id. Instead, compute the index in a single pass (e.g., `FindIndex` if `Files` is a `List<T>`, or a `for` loop) and pass that index directly to `preview`.
</issue_to_address>

### Comment 2
<location> `src/BootstrapBlazor/Components/Upload/AvatarUpload.razor:19-20` </location>
<code_context>
                 <Avatar Url="@item.PrevUrl" />
-                <div class="upload-item-actions">
-                    <span class="upload-item-delete" @onclick="@(e => OnFileDelete(item))">
+                <div class="upload-item-actions" @onclick="@(() => Preview(item.ValidateId ?? ""))">
+                    <span class="upload-item-delete" @onclick="@(e => OnFileDelete(item))"
+                          @onclick:stopPropagation="true">
                         <i class="@DeleteIcon"></i>
</code_context>

<issue_to_address>
**suggestion:** Consider passing the nullable id directly instead of normalizing to an empty string.

`Preview` currently requires a non-null `string`, so the call site converts `null` to `""`, which is then rejected by `string.IsNullOrWhiteSpace`. Instead, let `Preview` accept `string?` and pass `item.ValidateId` directly, so null handling is centralized and you avoid using a magic empty string at the call site.

Suggested implementation:

```
                <Avatar Url="@item.PrevUrl" />
                <div class="upload-item-actions" @onclick="@(() => Preview(item.ValidateId))">

```

To fully implement the suggestion, you will also need to:
1. Update the `Preview` method signature (likely in the `@code` block of `AvatarUpload.razor` or in `AvatarUpload.razor.cs`) from:
   ```csharp
   private void Preview(string validateId)
   ```
   to:
   ```csharp
   private void Preview(string? validateId)
   ```
2. Adjust the implementation of `Preview` to handle `null` (and optionally whitespace) centrally, e.g.:
   ```csharp
   private void Preview(string? validateId)
   {
       if (string.IsNullOrWhiteSpace(validateId))
       {
           return;
       }

       // existing preview logic using validateId
   }
   ```
3. Ensure all other call sites of `Preview` now pass either `string?` directly or are updated to avoid normalizing to `""` in the caller.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3c92843) to head (7ca214f).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #7368   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          747       747           
  Lines        32765     32776   +11     
  Branches      4541      4543    +2     
=========================================
+ Hits         32765     32776   +11     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ArgoZhang ArgoZhang changed the title refactor(AvatarUpload): Rewrite zoom event,align the current item index. feat(AvatarUpload): preview when click backdrop Dec 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AvatarUpload这个组件,能否加一个功能,就是查看上传后的图像大小,类CardUpload组件查看原图一样

2 participants