Skip to content

Commit d42dc89

Browse files
authored
chore: adjust keyboard event handling logic (#558)
1 parent 5f1e192 commit d42dc89

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

src/Drawer.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ import type { DrawerClassNames, DrawerStyles } from './inter';
1515
export type Placement = 'left' | 'top' | 'right' | 'bottom';
1616

1717
export interface DrawerProps
18-
extends Omit<DrawerPopupProps, 'prefixCls' | 'inline' | 'scrollLocker'>,
18+
extends
19+
Omit<DrawerPopupProps, 'prefixCls' | 'inline' | 'scrollLocker'>,
1920
DrawerPanelEvents,
2021
DrawerPanelAccessibility {
2122
prefixCls?: string;
2223
open?: boolean;
23-
onClose?: (e: React.MouseEvent | React.KeyboardEvent) => void;
24+
onClose?: (e: React.MouseEvent | React.KeyboardEvent | KeyboardEvent) => void;
2425
destroyOnHidden?: boolean;
2526
getContainer?: PortalProps['getContainer'];
2627
panelRef?: React.Ref<HTMLDivElement>;
@@ -73,6 +74,7 @@ const Drawer: React.FC<DrawerProps> = props => {
7374
onClick,
7475
onKeyDown,
7576
onKeyUp,
77+
onClose,
7678
resizable,
7779
defaultSize,
7880

@@ -159,13 +161,21 @@ const Drawer: React.FC<DrawerProps> = props => {
159161
...eventHandlers,
160162
};
161163

164+
const onEsc: PortalProps['onEsc'] = ({ top, event }) => {
165+
if (top && keyboard) {
166+
event.stopPropagation();
167+
onClose?.(event);
168+
}
169+
};
170+
162171
return (
163172
<RefContext.Provider value={refContext}>
164173
<Portal
165174
open={mergedOpen || forceRender || animatedVisible}
166175
autoDestroy={false}
167176
getContainer={getContainer}
168177
autoLock={mask && (mergedOpen || animatedVisible)}
178+
onEsc={onEsc}
169179
>
170180
<DrawerPopup {...drawerPopupProps} />
171181
</Portal>

src/DrawerPopup.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ export interface PushConfig {
3131
}
3232

3333
export interface DrawerPopupProps
34-
extends DrawerPanelEvents,
35-
DrawerPanelAccessibility {
34+
extends DrawerPanelEvents, DrawerPanelAccessibility {
3635
prefixCls: string;
3736
open?: boolean;
3837
inline?: boolean;
@@ -106,7 +105,6 @@ const DrawerPopup: React.ForwardRefRenderFunction<
106105
push,
107106
forceRender,
108107
autoFocus,
109-
keyboard,
110108

111109
// classNames
112110
classNames: drawerClassNames,
@@ -174,15 +172,6 @@ const DrawerPopup: React.ForwardRefRenderFunction<
174172
}
175173
break;
176174
}
177-
178-
// Close
179-
case KeyCode.ESC: {
180-
if (onClose && keyboard) {
181-
event.stopPropagation();
182-
onClose(event);
183-
}
184-
break;
185-
}
186175
}
187176
};
188177

tests/index.spec.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,22 +299,20 @@ describe('rc-drawer-menu', () => {
299299
describe('keyboard', () => {
300300
it('ESC to exit', () => {
301301
const onClose = jest.fn();
302-
const { container } = render(
303-
<Drawer open getContainer={false} onClose={onClose} />,
304-
);
305-
fireEvent.keyDown(container.querySelector('.rc-drawer-section'), {
306-
keyCode: KeyCode.ESC,
302+
render(<Drawer open getContainer={false} onClose={onClose} />);
303+
fireEvent.keyDown(window, {
304+
key: 'Escape',
307305
});
308306
expect(onClose).toHaveBeenCalled();
309307
});
310308

311309
it('disable ESC to exit', () => {
312310
const onClose = jest.fn();
313-
const { container } = render(
311+
render(
314312
<Drawer open getContainer={false} onClose={onClose} keyboard={false} />,
315313
);
316-
fireEvent.keyDown(container.querySelector('.rc-drawer-section'), {
317-
keyCode: KeyCode.ESC,
314+
fireEvent.keyDown(window, {
315+
key: 'Escape',
318316
});
319317
expect(onClose).not.toHaveBeenCalled();
320318
});

0 commit comments

Comments
 (0)