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

[Cases] Close FilePreview with Escape key. #155592

Merged
merged 1 commit into from
Apr 24, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import React from 'react';

import { screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import type { AppMockRenderer } from '../../common/mock';

Expand Down Expand Up @@ -35,4 +36,23 @@ describe('FilePreview', () => {

expect(await screen.findByTestId('cases-files-image-preview')).toBeInTheDocument();
});

it('pressing escape calls closePreview', async () => {
const closePreview = jest.fn();

appMockRender.render(<FilePreview closePreview={closePreview} selectedFile={basicFileMock} />);

await waitFor(() =>
expect(appMockRender.getFilesClient().getDownloadHref).toHaveBeenCalledWith({
id: basicFileMock.id,
fileKind: constructFileKindIdByOwner(mockedTestProvidersOwner[0]),
})
);

expect(await screen.findByTestId('cases-files-image-preview')).toBeInTheDocument();

userEvent.keyboard('{esc}');

await waitFor(() => expect(closePreview).toHaveBeenCalled());
});
});
18 changes: 16 additions & 2 deletions x-pack/plugins/cases/public/components/files/file_preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React from 'react';
import React, { useEffect } from 'react';
import styled from 'styled-components';

import type { FileJSON } from '@kbn/shared-ux-file-types';

import { EuiOverlayMask, EuiFocusTrap, EuiImage } from '@elastic/eui';
import { EuiOverlayMask, EuiFocusTrap, EuiImage, keys } from '@elastic/eui';
import { useFilesContext } from '@kbn/shared-ux-file-context';

import type { Owner } from '../../../common/constants/types';
Expand All @@ -36,6 +36,20 @@ export const FilePreview = ({ closePreview, selectedFile }: FilePreviewProps) =>
const { client: filesClient } = useFilesContext();
const { owner } = useCasesContext();

useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

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

nit: You can use useEvent from react-use if you want.

const keyboardListener = (event: KeyboardEvent) => {
if (event.key === keys.ESCAPE || event.code === 'Escape') {
closePreview();
}
};

window.addEventListener('keyup', keyboardListener);

return () => {
window.removeEventListener('keyup', keyboardListener);
};
}, [closePreview]);

return (
<StyledOverlayMask>
<EuiFocusTrap onClickOutside={closePreview}>
Expand Down