Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: should be closed when press esc after click portal #273

Merged
merged 3 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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]);
Copy link
Member

Choose a reason for hiding this comment

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

这个会不会整个声明周期绑定、解绑一次好一点?


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();
});
});
Loading