Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default () => (
| closeIcon | React.ReactNode | - | Custom close icon |
| src | string | - | Customize preview src |
| movable | boolean | true | Enable drag |
| wheel | boolean | true | Enable mouse wheel zoom |
| scaleStep | number | 0.5 | The number to which the scale is increased or decreased |
| minScale | number | 1 | Min scale |
| maxScale | number | 50 | Max scale |
Expand Down Expand Up @@ -122,6 +123,7 @@ export default () => (
| --- | --- | --- | --- |
| open | boolean | - | Whether the preview is open or not |
| movable | boolean | true | Enable drag |
| wheel | boolean | true | Enable mouse wheel zoom |
| current | number | - | Current index |
| closeIcon | React.ReactNode | - | Custom close icon |
| scaleStep | number | 0.5 | The number to which the scale is increased or decreased |
Expand Down
5 changes: 5 additions & 0 deletions src/Preview/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ export interface InternalPreviewConfig {
minScale?: number;
maxScale?: number;

/** Whether to enable mouse wheel zoom. Default is true. */
wheel?: boolean;

// Display
motionName?: string;
open?: boolean;
Expand Down Expand Up @@ -192,6 +195,7 @@ const Preview: React.FC<PreviewProps> = props => {
styles = {},
mousePosition,
zIndex,
wheel = true,
} = props;

const imgRef = useRef<HTMLImageElement>();
Expand All @@ -215,6 +219,7 @@ const Preview: React.FC<PreviewProps> = props => {
transform,
updateTransform,
dispatchZoomChange,
wheel,
);
const { isTouching, onTouchStart, onTouchMove, onTouchEnd } = useTouchEvent(
imgRef,
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useMouseEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default function useMouseEvent(
transform: TransformType,
updateTransform: UpdateTransformFunc,
dispatchZoomChange: DispatchZoomChangeFunc,
wheel: boolean = true,
) {
const { rotate, scale, x, y } = transform;

Expand Down Expand Up @@ -83,7 +84,7 @@ export default function useMouseEvent(
};

const onWheel = (event: React.WheelEvent<HTMLImageElement>) => {
if (!open || event.deltaY == 0) return;
if (!open || !wheel || event.deltaY == 0) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Use strict equality === instead of loose equality == for comparing event.deltaY with 0. This is a standard TypeScript best practice to avoid type coercion and ensure type safety.

Suggested change
if (!open || !wheel || event.deltaY == 0) return;
if (!open || !wheel || event.deltaY === 0) return;

// Scale ratio depends on the deltaY size
const scaleRatio = Math.abs(event.deltaY / 100);
// Limit the maximum scale ratio
Expand Down
39 changes: 39 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,45 @@ describe('Preview', () => {
});
});

it('should not zoom with wheel when wheel is false', () => {
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{
open: true,
wheel: false,
}}
/>,
);

expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, 1, 1) rotate(0deg)',
});

// Wheel zoom should not work when wheel is false
fireEvent.wheel(document.querySelector('.rc-image-preview-img'), {
deltaY: -50,
});

act(() => {
jest.runAllTimers();
});

// Scale should remain 1
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(0px, 0px, 0) scale3d(1, 1, 1) rotate(0deg)',
});

// But zoom in button should still work
fireEvent.click(document.querySelectorAll('.rc-image-preview-actions-action')[5]);
act(() => {
jest.runAllTimers();
});
expect(document.querySelector('.rc-image-preview-img')).toHaveStyle({
transform: 'translate3d(-256px, -192px, 0) scale3d(1.5, 1.5, 1) rotate(0deg)',
});
});

it('scaleStep = 1', () => {
const { container } = render(
<Image
Expand Down