Skip to content

feat: support ref parameter for Image component #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

SimerW
Copy link

@SimerW SimerW commented Oct 10, 2024

#372

Summary by CodeRabbit

  • 新功能

    • 改进了 ImageInternal 组件的结构和功能,支持通过 ref 直接与 <img> 元素交互。
    • 更新了组件的类型定义,增强了类型安全性。
  • 文档

    • 更新了接口和组件的签名,以支持更灵活的类型定义。

Copy link

vercel bot commented Oct 10, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
image ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 10, 2024 11:43am

Copy link

coderabbitai bot commented Oct 10, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

此次更改主要集中在 Image.tsx 文件中,增强了 ImageInternal 组件的结构和功能。更新了导入语句,引入了 React 的 forwardRefMutableRefObject,使组件能够使用 refs。CompoundedComponent 接口被修改以接受第二个泛型类型参数 T,提供更灵活的类型定义。ImageInternal 组件现在使用 forwardRef 定义,允许父组件直接与 <img> 元素的 ref 交互,同时调整了 ref 的处理方式,以确保内部和外部 refs 正确设置。

Changes

文件路径 更改摘要
src/Image.tsx 更新导入语句,引入 forwardRefMutableRefObject;修改 CompoundedComponent 接口以支持第二个泛型;使用 forwardRef 定义 ImageInternal 组件;调整 <img> 元素的 ref 处理;显式设置 ImageInternal 的返回类型。

Poem

在兔子洞中欢声笑语,
图像组件更灵活如风,
引用转发如春风拂面,
让父组件轻松相连。
兔子们欢庆这变化,
让每张图片更精彩! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 20afe8d and 54e3a40.

📒 Files selected for processing (1)
  • src/Image.tsx (4 hunks)
🧰 Additional context used
🔇 Additional comments (3)
src/Image.tsx (3)

7-7: 导入语句更新正确。

引入了 forwardRefMutableRefObject,为组件支持 ref 提供了必要的依赖。


72-74: 接口 CompoundedComponent 更新合理。

添加了泛型参数 T 并扩展了 React.ForwardRefExoticComponent,以支持 ref 的类型定义。


269-269: 类型断言更新正确。

ImageInternal 类型断言为 CompoundedComponent<ImageProps, HTMLImageElement>,与接口定义一致。

src/Image.tsx Outdated
Comment on lines 76 to 79
const ImageInternal = forwardRef<
HTMLImageElement,
ImageProps
>((props, ref: MutableRefObject<HTMLImageElement>) => {
Copy link

@coderabbitai coderabbitai bot Oct 10, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

修正 ref 参数的类型定义。

forwardRef 中,ref 参数的类型应为 React.Ref<HTMLImageElement>,而不是 MutableRefObject<HTMLImageElement>。这样可以确保类型定义与 React 的 forwardRef 类型一致。

您可以应用以下修改:

>((props, ref: MutableRefObject<HTMLImageElement>) => {
+>((props, ref: React.Ref<HTMLImageElement>) => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const ImageInternal = forwardRef<
HTMLImageElement,
ImageProps
>((props, ref: MutableRefObject<HTMLImageElement>) => {
const ImageInternal = forwardRef<
HTMLImageElement,
ImageProps
>((props, ref: React.Ref<HTMLImageElement>) => {

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link

Choose a reason for hiding this comment

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

@afc163 感谢您的认可!
(^・ω・^)


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

src/Image.tsx Outdated
Comment on lines 212 to 220
ref={(img) => {
getImgRef(img);

if (ref) {
ref.current = img
}
}}
Copy link

@coderabbitai coderabbitai bot Oct 10, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

正确地转发 ref,处理函数和对象类型的 ref

当前实现只处理了 ref 为对象的情况,如果用户传递的是函数类型的 ref,将无法正确处理。应同时支持函数和对象类型的 ref

可以将代码修改为:

ref={(img) => {
  getImgRef(img);

- if (ref) {
-   ref.current = img
- }
+ if (typeof ref === 'function') {
+   ref(img);
+ } else if (ref) {
+   ref.current = img;
+ }
}}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ref={(img) => {
getImgRef(img);
if (ref) {
ref.current = img
}
}}
ref={(img) => {
getImgRef(img);
if (typeof ref === 'function') {
ref(img);
} else if (ref) {
ref.current = img;
}
}}

Copy link
Member

Choose a reason for hiding this comment

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

+1

Copy link

Choose a reason for hiding this comment

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

@afc163,很高兴我的建议对您有帮助!

(_/)
(^_^)
('')('')


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Copy link

codecov bot commented Oct 11, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 99.37%. Comparing base (20afe8d) to head (7f8c787).

Files with missing lines Patch % Lines
src/Image.tsx 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #373      +/-   ##
==========================================
- Coverage   99.78%   99.37%   -0.42%     
==========================================
  Files          15       15              
  Lines         470      477       +7     
  Branches      134      136       +2     
==========================================
+ Hits          469      474       +5     
- Misses          1        3       +2     

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

@SimerW SimerW closed this Oct 12, 2024
liamli-0822 added a commit to liamli-0822/image that referenced this pull request Mar 4, 2025
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.

2 participants