Skip to content

fix: pass clicked image index as current to PreviewGroup onOpenChange - #540

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/preview-group-onopenchange-current
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/preview-group-onopenchange-current

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

Image.PreviewGroup calls preview.onOpenChange(open, { current }). When a preview is opened by clicking an image, current was the index that was selected before the click, not the index of the image that was clicked.

<Image.PreviewGroup preview={{ onOpenChange }}>
  <Image src="src1" /><Image src="src2" /><Image src="src3" />
</Image.PreviewGroup>
// click the third image: onOpenChange(true, { current: 0 })   // expected { current: 2 }

The click handler sets the new index with setCurrent(index) and then calls triggerShowPreview(true), which reads current from the render that is still the old one. triggerShowPreview now takes the new index as an optional second argument and passes it to onOpenChange; the click handler passes the index it just resolved. Calls without an index (closing the preview, or open controlled from outside) still report the current index as before.

Test: onOpenChange should receive clicked image index as current in tests/previewGroup.test.tsx clicks the third image (true, { current: 2 }), closes the preview (false, { current: 2 }) and then opens the second image (true, { current: 1 }). It fails on master (received current: 0 where 2 is expected) and passes now. npx rc-test passes (8 suites, 85 tests), tsc --noEmit is clean, Prettier is clean on the changed files, and ESLint reports the same 2 hook-dependency warnings in PreviewGroup.tsx as on master.

Summary by CodeRabbit

  • 问题修复
    • 打开图片预览时,状态变化回调现在会收到所选图片对应的索引;关闭预览时也会保留该索引,确保回调信息与当前预览图片一致。

@vercel

vercel Bot commented Dec 1, 2026

Copy link
Copy Markdown

@mokshupadhyay is attempting to deploy a commit to the afc163's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

预览组现在会在打开预览时,将被点击图片的索引传给 onOpenChange。新增测试验证打开、关闭以及再次打开时回调收到的索引。

Changes

预览索引回调

层 / 文件 摘要
传递并验证目标索引
src/PreviewGroup.tsx, tests/previewGroup.test.tsx
triggerShowPreview 支持可选的目标索引。打开预览时,回调收到目标索引;未提供目标索引时,回调使用当前索引。测试覆盖打开、关闭和再次打开时的索引。

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: vagusx, emilyyyliu

Merge Risk: Minimal · up to 3f5fb

Opening an image preview now reports the clicked image's index to the open-change callback, and closing keeps the existing behavior. No merge-blocking risk is evident.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: pass clicked image index as current to PreviewGroup onOpenChange”描述了主要变更:点击图片打开预览时,onOpenChange 回调应收到被点击图片的索引。原始摘要显示 triggerShowPreview 新增可选索引参数,并从图片点击处传入该索引,同时新增测试覆盖该行为。标题与变更集直接相关,且具体说明了修复内容。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

兔子轻点第三张,
索引随回调传到场。
关闭预览仍记当前号,
再点第二张更新目标。
测试逐项核对每次调用。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ec6b9680-2e39-48ca-883f-639cbfbea44d
Commits

Reviewing files that changed from the base of the PR and between b1a0b0a and 3f5fb9b.

Files selected for processing: 2

  • src/PreviewGroup.tsx
  • tests/previewGroup.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant