-
-
Notifications
You must be signed in to change notification settings - Fork 103
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
feat: add ref forwarding to internal img element. Closes #372 #379
Open
liamli-0822
wants to merge
7
commits into
react-component:master
Choose a base branch
from
liamli-0822:feat/ref-forwarding
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b88ff56
feat: add ref forwarding to internal img element. Closes #373
liamli-0822 0b6f315
Update src/Image.tsx
liamli-0822 338cb86
feat: add nativeElement property to Image component ref
liamli-0822 f8720c3
Merge remote-tracking branch 'origin/master' into feat/ref-forwarding
liamli-0822 14a74e3
update tests
liamli-0822 26a8205
Merge branch 'feat/ref-forwarding' of https://github.com/liamli-0822/…
liamli-0822 b04e15e
update tests
liamli-0822 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { act, fireEvent, render } from '@testing-library/react'; | ||
import React from 'react'; | ||
import Image, { ImageRef } from '../src'; | ||
|
||
describe('Image ref forwarding', () => { | ||
// 测试对象类型的 ref | ||
it('should provide access to internal img element via nativeElement', () => { | ||
const ref = React.createRef<ImageRef>(); | ||
const { container } = render( | ||
<Image | ||
ref={ref} | ||
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" | ||
alt="test image" | ||
/>, | ||
); | ||
|
||
// 确保 ref.current.nativeElement 指向正确的 img 元素 | ||
expect(ref.current).not.toBeNull(); | ||
expect(ref.current?.nativeElement).toBe(container.querySelector('.rc-image-img')); | ||
expect(ref.current?.nativeElement?.tagName).toBe('IMG'); | ||
expect(ref.current?.nativeElement?.alt).toBe('test image'); | ||
}); | ||
|
||
// 测试回调类型的 ref | ||
it('should work with callback ref', () => { | ||
let imgRef: ImageRef | null = null; | ||
const callbackRef = (el: ImageRef | null) => { | ||
imgRef = el; | ||
}; | ||
|
||
const { container } = render( | ||
<Image | ||
ref={callbackRef} | ||
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" | ||
/>, | ||
); | ||
|
||
// 确保回调 ref 被调用,且 nativeElement 指向正确的 img 元素 | ||
expect(imgRef).not.toBeNull(); | ||
expect(imgRef?.nativeElement).toBe(container.querySelector('.rc-image-img')); | ||
}); | ||
|
||
// 测试通过 nativeElement 访问 img 元素的属性和方法 | ||
it('should allow access to img element properties and methods via nativeElement', () => { | ||
const ref = React.createRef<ImageRef>(); | ||
render( | ||
<Image | ||
ref={ref} | ||
width={200} | ||
height={100} | ||
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" | ||
/>, | ||
); | ||
|
||
// 确保可以通过 ref.nativeElement 访问 img 元素的属性 | ||
expect(ref.current?.nativeElement?.width).toBe(200); | ||
expect(ref.current?.nativeElement?.height).toBe(100); | ||
|
||
// 可以测试调用 img 元素的方法 | ||
// 注意:某些方法可能在 jsdom 环境中不可用,根据实际情况调整 | ||
}); | ||
|
||
// 测试 ref.nativeElement 在组件重新渲染时保持稳定 | ||
it('should maintain stable nativeElement reference across re-renders', () => { | ||
const ref = React.createRef<ImageRef>(); | ||
const { rerender } = render( | ||
<Image | ||
ref={ref} | ||
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" | ||
/>, | ||
); | ||
|
||
const initialImgElement = ref.current?.nativeElement; | ||
expect(initialImgElement).not.toBeNull(); | ||
|
||
// 重新渲染组件,但保持 ref 不变 | ||
rerender( | ||
<Image | ||
ref={ref} | ||
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" | ||
alt="updated alt" | ||
/>, | ||
); | ||
|
||
// 确保 ref.nativeElement 引用的还是同一个 img 元素 | ||
expect(ref.current?.nativeElement).toBe(initialImgElement); | ||
expect(ref.current?.nativeElement?.alt).toBe('updated alt'); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
集成内部和外部引用的处理函数
handleRef
函数巧妙地处理了内部引用的存储和对getImgRef
的调用,确保了与现有功能的兼容性。但是,这里有一个潜在的问题:当 img 元素为 null 时直接返回了,但没有更新imageElementRef.current
。应该考虑在这种情况下将imageElementRef.current
也设置为 null。const handleRef = (img: HTMLImageElement | null) => { if (!img) { + imageElementRef.current = null; return; } // 保存到内部引用 imageElementRef.current = img; getImgRef(img); };
📝 Committable suggestion