Skip to content

Commit efe2d0a

Browse files
committed
chore: adjust keyboard event handling logic
1 parent 5bc46d7 commit efe2d0a

File tree

7 files changed

+17
-20
lines changed

7 files changed

+17
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,4 @@ pnpm-lock.yaml
5050
.dumi/tmp-production
5151

5252
bun.lockb
53+
.vscode

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
},
5252
"dependencies": {
5353
"@rc-component/motion": "^1.1.3",
54-
"@rc-component/portal": "^2.0.0",
54+
"@rc-component/portal": "^2.1.0",
5555
"@rc-component/util": "^1.5.0",
5656
"clsx": "^2.1.1"
5757
},

src/Dialog/index.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { clsx } from 'clsx';
22
import contains from '@rc-component/util/lib/Dom/contains';
33
import useId from '@rc-component/util/lib/hooks/useId';
4-
import KeyCode from '@rc-component/util/lib/KeyCode';
54
import pickAttrs from '@rc-component/util/lib/pickAttrs';
65
import * as React from 'react';
76
import { useEffect, useRef } from 'react';
@@ -16,10 +15,7 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
1615
prefixCls = 'rc-dialog',
1716
zIndex,
1817
visible = false,
19-
keyboard = true,
2018
focusTriggerAfterClose = true,
21-
// scrollLocker,
22-
// Wrapper
2319
wrapStyle,
2420
wrapClassName,
2521
wrapProps,
@@ -142,14 +138,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
142138
};
143139
}
144140

145-
function onWrapperKeyDown(e: React.KeyboardEvent<HTMLDivElement>) {
146-
if (keyboard && e.keyCode === KeyCode.ESC) {
147-
e.stopPropagation();
148-
onInternalClose(e);
149-
return;
150-
}
151-
}
152-
153141
// ========================= Effect =========================
154142
useEffect(() => {
155143
if (visible) {
@@ -201,7 +189,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
201189
className={modalClassNames?.mask}
202190
/>
203191
<div
204-
onKeyDown={onWrapperKeyDown}
205192
className={clsx(`${prefixCls}-wrap`, wrapClassName, modalClassNames?.wrapper)}
206193
ref={wrapperRef}
207194
onClick={onWrapperClick}

src/DialogWrap.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Portal from '@rc-component/portal';
1+
import Portal, { type PortalProps } from '@rc-component/portal';
22
import * as React from 'react';
33
import { RefContext } from './context';
44
import Dialog from './Dialog';
@@ -22,11 +22,21 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
2222
afterClose,
2323
closable,
2424
panelRef,
25+
keyboard = true,
2526
} = props;
2627
const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);
2728

2829
const refContext = React.useMemo(() => ({ panel: panelRef }), [panelRef]);
2930

31+
const onEsc: PortalProps['onEsc'] = ({ top, event }) => {
32+
if (top && keyboard) {
33+
event.stopPropagation();
34+
// @ts-ignore
35+
props.onClose?.(event);
36+
return;
37+
}
38+
};
39+
3040
React.useEffect(() => {
3141
if (visible) {
3242
setAnimatedVisible(true);
@@ -42,6 +52,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
4252
<RefContext.Provider value={refContext}>
4353
<Portal
4454
open={visible || forceRender || animatedVisible}
55+
onEsc={onEsc}
4556
autoDestroy={false}
4657
getContainer={getContainer}
4758
autoLock={visible || animatedVisible}

tests/index.spec.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
22
import { fireEvent, render, act } from '@testing-library/react';
33
import { Provider } from '@rc-component/motion';
4-
import KeyCode from '@rc-component/util/lib/KeyCode';
54
import React, { cloneElement, useEffect } from 'react';
65
import type { DialogProps } from '../src';
76
import Dialog from '../src';
@@ -164,10 +163,8 @@ describe('dialog', () => {
164163
it('esc to close', () => {
165164
const onClose = jest.fn();
166165
render(<Dialog onClose={onClose} visible />);
167-
jest.runAllTimers();
168166

169-
fireEvent.keyDown(document.querySelector('.rc-dialog'), { keyCode: KeyCode.ESC });
170-
jest.runAllTimers();
167+
fireEvent.keyDown(window, { key: 'Escape' });
171168
expect(onClose).toHaveBeenCalled();
172169
});
173170

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"@rc-component/dialog": [
1818
"src/index.ts"
1919
]
20-
}
20+
},
21+
"types": ["node", "jest", "@testing-library/jest-dom"],
2122
},
2223
"include": [
2324
".dumirc.ts",

0 commit comments

Comments
 (0)