From 3f39cd9a3f034cc1753c75a2f06d4508c43de5a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=8C=E4=BA=9A=E7=9A=84=E8=A5=BF=E7=BA=A2=E6=9F=BF?= <97817985+nova1751@users.noreply.github.com> Date: Wed, 5 Jun 2024 20:19:25 +0800 Subject: [PATCH] fix: fix the preview img size (#354) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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: 二货机器人 --- src/Image.tsx | 3 +-- src/Preview.tsx | 18 +++++++------- tests/preview.test.tsx | 53 +++++++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/src/Image.tsx b/src/Image.tsx index 56674f4..71e2e1f 100644 --- a/src/Image.tsx +++ b/src/Image.tsx @@ -240,8 +240,7 @@ const ImageInternal: CompoundedComponent = props => { mousePosition={mousePosition} src={src} alt={alt} - width={width} - height={height} + imageInfo={{ width, height }} fallback={fallback} getContainer={getPreviewContainer} icons={icons} diff --git a/src/Preview.tsx b/src/Preview.tsx index d83f825..15d3781 100644 --- a/src/Preview.tsx +++ b/src/Preview.tsx @@ -4,14 +4,14 @@ import Dialog from 'rc-dialog'; import addEventListener from 'rc-util/lib/Dom/addEventListener'; import KeyCode from 'rc-util/lib/KeyCode'; import React, { useContext, useEffect, useRef, useState } from 'react'; +import type { ImgInfo } from './Image'; +import Operations from './Operations'; import { PreviewGroupContext } from './context'; import type { TransformAction, TransformType } from './hooks/useImageTransform'; import useImageTransform from './hooks/useImageTransform'; import useMouseEvent from './hooks/useMouseEvent'; import useStatus from './hooks/useStatus'; import useTouchEvent from './hooks/useTouchEvent'; -import type { ImgInfo } from './Image'; -import Operations from './Operations'; import { BASE_SCALE_RATIO } from './previewConfig'; export type ToolbarRenderInfoType = { @@ -43,8 +43,10 @@ export interface PreviewProps extends Omit { imgCommonProps?: React.ImgHTMLAttributes; src?: string; alt?: string; - width?: number | string; - height?: number | string; + imageInfo?: { + width: number | string; + height: number | string; + }; fallback?: string; movable?: boolean; rootClassName?: string; @@ -107,8 +109,7 @@ const Preview: React.FC = props => { prefixCls, src, alt, - width, - height, + imageInfo, fallback, movable = true, onClose, @@ -203,7 +204,7 @@ const Preview: React.FC = props => { }; const onReset = () => { - resetTransform("reset"); + resetTransform('reset'); }; const onSwitchLeft = (event?: React.MouseEvent) => { @@ -288,8 +289,7 @@ const Preview: React.FC = props => { const image = { url: src, alt, - width, - height, + ...imageInfo, }; return ( diff --git a/tests/preview.test.tsx b/tests/preview.test.tsx index 661aa87..df39a64 100644 --- a/tests/preview.test.tsx +++ b/tests/preview.test.tsx @@ -796,13 +796,27 @@ describe('Preview', () => { printImage(image); return ( <> -
actions.onFlipY()}>{icons.flipYIcon}
-
actions.onFlipX()}>{icons.flipXIcon}
-
actions.onZoomIn()}>{icons.zoomInIcon}
-
actions.onZoomOut()}>{icons.zoomOutIcon}
-
actions.onRotateLeft()}>{icons.rotateLeftIcon}
-
actions.onRotateRight()}>{icons.rotateRightIcon}
-
actions.onReset()}>reset
+
actions.onFlipY()}> + {icons.flipYIcon} +
+
actions.onFlipX()}> + {icons.flipXIcon} +
+
actions.onZoomIn()}> + {icons.zoomInIcon} +
+
actions.onZoomOut()}> + {icons.zoomOutIcon} +
+
actions.onRotateLeft()}> + {icons.rotateLeftIcon} +
+
actions.onRotateRight()}> + {icons.rotateRightIcon} +
+
actions.onReset()}> + reset +
); }, @@ -839,9 +853,9 @@ describe('Preview', () => { jest.runAllTimers(); }); expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({ - transform: 'translate3d(-206px, -142px, 0) scale3d(-1.5, -1.5, 1) rotate(-90deg)', + transform: 'translate3d(-256px, -192px, 0) scale3d(-1.5, -1.5, 1) rotate(-90deg)', }); - + // reset fireEvent.click(document.getElementById('reset')); act(() => { @@ -940,4 +954,25 @@ describe('Preview', () => { onVisibleChange.mockRestore(); }); + + it('not modify preview image size', () => { + render( + , + ); + + act(() => { + jest.runAllTimers(); + }); + + const previewImg = document.querySelector('.rc-image-preview-img-wrapper img'); + expect(previewImg).not.toHaveAttribute('width'); + expect(previewImg).not.toHaveAttribute('height'); + }); });