Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,22 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
COMMON_PROPS.map(prop => props[prop]),
);

// `srcSet` & `sizes` describe the thumbnail, and would win over `preview.src` in the preview image
const previewImgCommonProps = useMemo(() => {
if (previewSrc === undefined) {
return imgCommonProps;
}
const { srcSet, sizes, ...rest } = imgCommonProps;
return rest;
}, [previewSrc, imgCommonProps]);

// ========================== Register ==========================
const registerData: ImageElementProps = useMemo(
() => ({
...imgCommonProps,
...previewImgCommonProps,
src,
}),
[src, imgCommonProps],
[src, previewImgCommonProps],
);

const imageId = useRegisterImage(canPreview, registerData);
Expand Down Expand Up @@ -307,7 +316,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
alt={alt}
imageInfo={{ width, height }}
fallback={fallback}
imgCommonProps={imgCommonProps}
imgCommonProps={previewImgCommonProps}
{...restProps}
classNames={classNames?.popup}
styles={styles?.popup}
Expand Down
54 changes: 54 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,60 @@ describe('Preview', () => {
expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute('src', previewSrc);
});

it('previewSrc should not use the srcSet and sizes of the thumbnail', () => {
const { container } = render(
<Image
src="thumb-100.png"
srcSet="thumb-200.png 2x"
sizes="100px"
preview={{ src: 'big-500.png' }}
/>,
);

expect(container.querySelector('.rc-image-img')).toHaveAttribute('srcset', 'thumb-200.png 2x');

fireEvent.click(container.querySelector('.rc-image'));
act(() => {
jest.runAllTimers();
});

const previewImg = document.querySelector('.rc-image-preview-img');
expect(previewImg).toHaveAttribute('src', 'big-500.png');
expect(previewImg).not.toHaveAttribute('srcset');
expect(previewImg).not.toHaveAttribute('sizes');
});

it('previewSrc should not use the srcSet of the thumbnail in PreviewGroup', () => {
const { container } = render(
<Image.PreviewGroup>
<Image src="thumb-100.png" srcSet="thumb-200.png 2x" preview={{ src: 'big-500.png' }} />
</Image.PreviewGroup>,
);

fireEvent.click(container.querySelector('.rc-image'));
act(() => {
jest.runAllTimers();
});

const previewImg = document.querySelector('.rc-image-preview-img');
expect(previewImg).toHaveAttribute('src', 'big-500.png');
expect(previewImg).not.toHaveAttribute('srcset');
});

it('should keep srcSet in the preview without previewSrc', () => {
const { container } = render(<Image src="thumb-100.png" srcSet="thumb-200.png 2x" />);

fireEvent.click(container.querySelector('.rc-image'));
act(() => {
jest.runAllTimers();
});

expect(document.querySelector('.rc-image-preview-img')).toHaveAttribute(
'srcset',
'thumb-200.png 2x',
);
});

it('Customize preview props', () => {
const src = 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png';
render(
Expand Down