Skip to content
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
14 changes: 14 additions & 0 deletions src/app/components/dialog/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ function PutAway({
noTitle?: boolean;
onClick?: () => void;
}) {
React.useEffect(() => {
if (onClick) {
const closeOnEsc = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
onClick();
}
};

document.addEventListener('keydown', closeOnEsc);
return () => document.removeEventListener('keydown', closeOnEsc);
}
return () => null;
}, [onClick]);

return (
<button
className={cn('put-away', {'no-title-bar': noTitle})}
Expand Down
11 changes: 11 additions & 0 deletions test/src/components/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ describe('components/dialog', () => {

await user.click(pa);
});
it('pressing Escape is the same as pressing close button', async () => {
render(<Component />);
const b = screen.getByRole('button');

await user.click(b);
await screen.findByRole('button', {name: 'close'});
await userEvent.type(document.body, '{Enter}');
expect(screen.queryByRole('button', {name: 'close'})).toBeTruthy();
await userEvent.type(document.body, '{Escape}');
expect(screen.queryByRole('button', {name: 'close'})).not.toBeTruthy();
});
it('Handles nonmodal and no put-away', async () => {
render(<Component showPutAway={false} modal={false} />);
const b = screen.getByRole('button');
Expand Down