Skip to content

Commit 3f39cd9

Browse files
nova1751zombieJ
andauthored
fix: fix the preview img size (#354)
* fix: fix the preview img size * fix: update * fix: update * test: update test file * test: update test file * fix: update * fix: the bug * test: add test case --------- Co-authored-by: 二货机器人 <[email protected]>
1 parent 4fe15d0 commit 3f39cd9

File tree

3 files changed

+54
-20
lines changed

3 files changed

+54
-20
lines changed

src/Image.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
240240
mousePosition={mousePosition}
241241
src={src}
242242
alt={alt}
243-
width={width}
244-
height={height}
243+
imageInfo={{ width, height }}
245244
fallback={fallback}
246245
getContainer={getPreviewContainer}
247246
icons={icons}

src/Preview.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import Dialog from 'rc-dialog';
44
import addEventListener from 'rc-util/lib/Dom/addEventListener';
55
import KeyCode from 'rc-util/lib/KeyCode';
66
import React, { useContext, useEffect, useRef, useState } from 'react';
7+
import type { ImgInfo } from './Image';
8+
import Operations from './Operations';
79
import { PreviewGroupContext } from './context';
810
import type { TransformAction, TransformType } from './hooks/useImageTransform';
911
import useImageTransform from './hooks/useImageTransform';
1012
import useMouseEvent from './hooks/useMouseEvent';
1113
import useStatus from './hooks/useStatus';
1214
import useTouchEvent from './hooks/useTouchEvent';
13-
import type { ImgInfo } from './Image';
14-
import Operations from './Operations';
1515
import { BASE_SCALE_RATIO } from './previewConfig';
1616

1717
export type ToolbarRenderInfoType = {
@@ -43,8 +43,10 @@ export interface PreviewProps extends Omit<IDialogPropTypes, 'onClose'> {
4343
imgCommonProps?: React.ImgHTMLAttributes<HTMLImageElement>;
4444
src?: string;
4545
alt?: string;
46-
width?: number | string;
47-
height?: number | string;
46+
imageInfo?: {
47+
width: number | string;
48+
height: number | string;
49+
};
4850
fallback?: string;
4951
movable?: boolean;
5052
rootClassName?: string;
@@ -107,8 +109,7 @@ const Preview: React.FC<PreviewProps> = props => {
107109
prefixCls,
108110
src,
109111
alt,
110-
width,
111-
height,
112+
imageInfo,
112113
fallback,
113114
movable = true,
114115
onClose,
@@ -203,7 +204,7 @@ const Preview: React.FC<PreviewProps> = props => {
203204
};
204205

205206
const onReset = () => {
206-
resetTransform("reset");
207+
resetTransform('reset');
207208
};
208209

209210
const onSwitchLeft = (event?: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
@@ -288,8 +289,7 @@ const Preview: React.FC<PreviewProps> = props => {
288289
const image = {
289290
url: src,
290291
alt,
291-
width,
292-
height,
292+
...imageInfo,
293293
};
294294

295295
return (

tests/preview.test.tsx

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -796,13 +796,27 @@ describe('Preview', () => {
796796
printImage(image);
797797
return (
798798
<>
799-
<div id="flipY" onClick={() => actions.onFlipY()}>{icons.flipYIcon}</div>
800-
<div id="flipX" onClick={() => actions.onFlipX()}>{icons.flipXIcon}</div>
801-
<div id="zoomIn" onClick={() => actions.onZoomIn()}>{icons.zoomInIcon}</div>
802-
<div id="zoomOut" onClick={() => actions.onZoomOut()}>{icons.zoomOutIcon}</div>
803-
<div id="rotateLeft" onClick={() => actions.onRotateLeft()}>{icons.rotateLeftIcon}</div>
804-
<div id="rotateRight" onClick={() => actions.onRotateRight()}>{icons.rotateRightIcon}</div>
805-
<div id="reset" onClick={() => actions.onReset()}>reset</div>
799+
<div id="flipY" onClick={() => actions.onFlipY()}>
800+
{icons.flipYIcon}
801+
</div>
802+
<div id="flipX" onClick={() => actions.onFlipX()}>
803+
{icons.flipXIcon}
804+
</div>
805+
<div id="zoomIn" onClick={() => actions.onZoomIn()}>
806+
{icons.zoomInIcon}
807+
</div>
808+
<div id="zoomOut" onClick={() => actions.onZoomOut()}>
809+
{icons.zoomOutIcon}
810+
</div>
811+
<div id="rotateLeft" onClick={() => actions.onRotateLeft()}>
812+
{icons.rotateLeftIcon}
813+
</div>
814+
<div id="rotateRight" onClick={() => actions.onRotateRight()}>
815+
{icons.rotateRightIcon}
816+
</div>
817+
<div id="reset" onClick={() => actions.onReset()}>
818+
reset
819+
</div>
806820
</>
807821
);
808822
},
@@ -839,9 +853,9 @@ describe('Preview', () => {
839853
jest.runAllTimers();
840854
});
841855
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
842-
transform: 'translate3d(-206px, -142px, 0) scale3d(-1.5, -1.5, 1) rotate(-90deg)',
856+
transform: 'translate3d(-256px, -192px, 0) scale3d(-1.5, -1.5, 1) rotate(-90deg)',
843857
});
844-
858+
845859
// reset
846860
fireEvent.click(document.getElementById('reset'));
847861
act(() => {
@@ -940,4 +954,25 @@ describe('Preview', () => {
940954

941955
onVisibleChange.mockRestore();
942956
});
957+
958+
it('not modify preview image size', () => {
959+
render(
960+
<Image
961+
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
962+
width={20}
963+
height={20}
964+
preview={{
965+
visible: true,
966+
}}
967+
/>,
968+
);
969+
970+
act(() => {
971+
jest.runAllTimers();
972+
});
973+
974+
const previewImg = document.querySelector('.rc-image-preview-img-wrapper img');
975+
expect(previewImg).not.toHaveAttribute('width');
976+
expect(previewImg).not.toHaveAttribute('height');
977+
});
943978
});

0 commit comments

Comments
 (0)