Skip to content

Commit

Permalink
fix: update zoomTo handling (react-cropper#562)
Browse files Browse the repository at this point in the history
* added useEffect for props.zoomTo

* added useEffect for props.zoomTo

* semantic changes

* the typo is fixed
  • Loading branch information
rizvanua authored Apr 25, 2021
1 parent ef09949 commit 4b71ab9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/react-cropper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ const ReactCropper = React.forwardRef<ReactCropperElement | HTMLImageElement, Re
};
}, [combinedRef]);

/**
* do zooming when param zoomTo is changed (passing data to cropperjs)
*/
useEffect(() => {
const currentRef: any = combinedRef.current;
if(currentRef && currentRef?.cropper?.canvasData && typeof props?.zoomTo === 'number') {
const cropper = currentRef.cropper;
cropper.zoomTo(props.zoomTo);
}
},[props.zoomTo]);

/**
* re-render when src changes
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/react-cropper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe('Cropper Render Tests', () => {
rerender(<Cropper src={newImage} />);
expect(ref.src).toEqual(newImage);
});

test('renders cropper with zoomTo prop', async () => {
const onInitialized = jest.fn();
render(<Cropper src={image} onInitialized={onInitialized} zoomTo={1} />);
await waitFor(() => expect(onInitialized).toHaveBeenCalledTimes(1));
});
});

describe('Test Cropper Methods', () => {
Expand Down

0 comments on commit 4b71ab9

Please sign in to comment.