Skip to content

Commit

Permalink
fix(ImageViewer): when the fix is ​​uncontrolled, onClose is always t…
Browse files Browse the repository at this point in the history
…riggered (#3244)

* fix(imageviewer): when the fix is ​​uncontrolled, onClose is always triggered

* fix(imageviewer): fix props onClose may be undefined

* test(imageviewer): fix test example
  • Loading branch information
HaixingOoO authored Dec 2, 2024
1 parent 51d7123 commit 4f08a80
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/image-viewer/ImageViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const ImageViewer: React.FC<ImageViewerProps> = (originalProps) => {

const imageViewerAttach = useAttach('imageViewer', attach);
const [visible, setVisible] = useControlled(props, 'visible', (visible, context) => {
isFunction(props.onClose) && props.onClose(context);
!visible && props?.onClose?.(context);
});

const [visibled, setVisibled] = useState(false);
Expand Down
10 changes: 5 additions & 5 deletions src/image-viewer/__tests__/image-viewer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('ImageViewer', () => {
});

// 鼠标点击后,有元素
expect(onClose).toHaveBeenCalledTimes(1);
expect(onClose).toHaveBeenCalledTimes(0);
const imgModal = document.querySelector('.t-image-viewer__modal-pic');
expect(imgModal).toBeTruthy();

Expand All @@ -39,7 +39,7 @@ describe('ImageViewer', () => {
fireEvent.click(closeBtn);
});
// 点击后,没有元素存在
expect(onClose).toHaveBeenCalledTimes(2);
expect(onClose).toHaveBeenCalledTimes(1);
await mockTimeout(() => expect(document.querySelector('.t-image-viewer-preview-image')).toBeNull());
});

Expand Down Expand Up @@ -122,7 +122,7 @@ describe('ImageViewerMini', () => {
act(() => {
fireEvent.click(closeBtn);
});
expect(onClose).toHaveBeenCalledTimes(2);
expect(onClose).toHaveBeenCalledTimes(1);
});
});

Expand Down Expand Up @@ -153,7 +153,7 @@ describe('ImageViewerModal', () => {

// 模拟键盘事件
await user.type(document.body, '{Escape}');
expect(onClose).toHaveBeenCalledTimes(2);
expect(onClose).toHaveBeenCalledTimes(1);

await user.type(document.body, '{ArrowRight}');
expect(onIndexChange).toHaveBeenCalledTimes(1);
Expand All @@ -166,7 +166,7 @@ describe('ImageViewerModal', () => {
act(() => {
fireEvent.click(mask);
});
expect(onClose).toHaveBeenCalledTimes(3);
expect(onClose).toHaveBeenCalledTimes(2);
});

test('single', async () => {
Expand Down

0 comments on commit 4f08a80

Please sign in to comment.