Skip to content

Commit

Permalink
fix: should be closed when press esc after click portal (#273)
Browse files Browse the repository at this point in the history
* fix: should be closed when press esc after click portal

* test: add case

* test: add called times
  • Loading branch information
linxianxi authored Aug 1, 2023
1 parent 3931701 commit c4d0cf3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Operations.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Portal from '@rc-component/portal';
import classnames from 'classnames';
import CSSMotion from 'rc-motion';
import KeyCode from 'rc-util/lib/KeyCode';
import * as React from 'react';
import { useContext } from 'react';
import { PreviewGroupContext } from './context';
Expand Down Expand Up @@ -75,6 +76,22 @@ const Operations: React.FC<OperationsProps> = props => {
const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right, flipX, flipY } = icons;
const toolClassName = `${prefixCls}-operations-operation`;

React.useEffect(() => {
const onKeyDown = (e: KeyboardEvent) => {
if (e.keyCode === KeyCode.ESC) {
onClose();
}
};

if (visible) {
window.addEventListener('keydown', onKeyDown);
}

return () => {
window.removeEventListener('keydown', onKeyDown);
};
}, [visible]);

const tools = [
{
icon: flipY,
Expand Down
30 changes: 30 additions & 0 deletions tests/preview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,34 @@ describe('Preview', () => {

expect(document.querySelector('video')).toBeTruthy();
});

it('should be closed when press esc after click portal', () => {
const onVisibleChange = jest.fn();
const { container } = render(
<Image
src="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
preview={{
onVisibleChange,
}}
/>,
);

fireEvent.click(container.querySelector('.rc-image'));
act(() => {
jest.runAllTimers();
});

expect(document.querySelector('.rc-image-preview')).toBeTruthy();

expect(onVisibleChange).toBeCalledWith(true, false);

fireEvent.click(document.querySelector('.rc-image-preview-operations'));

fireEvent.keyDown(window, { key: 'Escape', keyCode: 27 });

expect(onVisibleChange).toBeCalledWith(false, true);
expect(onVisibleChange).toBeCalledTimes(2);

onVisibleChange.mockRestore();
});
});

1 comment on commit c4d0cf3

@vercel
Copy link

@vercel vercel bot commented on c4d0cf3 Aug 1, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.