Skip to content

Commit f8eda32

Browse files
authored
Merge a2ab585 into 646efc7
2 parents 646efc7 + a2ab585 commit f8eda32

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/Dialog/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export default function Dialog(props: IDialogPropTypes) {
4848
const contentRef = useRef<ContentRef>();
4949

5050
const [animatedVisible, setAnimatedVisible] = React.useState(visible);
51+
const [originFocusEl, setOriginFocusEl] = React.useState<HTMLElement>();
5152

5253
// ========================== Init ==========================
5354
const ariaId = useId();
@@ -59,7 +60,11 @@ export default function Dialog(props: IDialogPropTypes) {
5960
}
6061

6162
function focusDialogContent() {
62-
if (!contains(wrapperRef.current, document.activeElement)) {
63+
if (originFocusEl) {
64+
originFocusEl.focus();
65+
} else if (contains(wrapperRef.current, document.activeElement)) {
66+
setOriginFocusEl(document.activeElement as HTMLElement);
67+
} else {
6368
contentRef.current?.focus();
6469
}
6570
}

src/DialogWrap.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import * as React from 'react';
21
import Portal from '@rc-component/portal';
2+
import * as React from 'react';
33
import Dialog from './Dialog';
44
import type { IDialogPropTypes } from './IDialogPropTypes';
5+
import { isNil } from './util';
56

67
// fix issue #10656
78
/*
@@ -41,7 +42,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
4142
<Portal
4243
open={visible || forceRender || animatedVisible}
4344
autoDestroy={false}
44-
getContainer={getContainer}
45+
getContainer={isNil(getContainer) ? 'body' : getContainer}
4546
autoLock={visible || animatedVisible}
4647
>
4748
<Dialog

src/util.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ export function offset(el: Element) {
3737
pos.top += getScroll(w, true);
3838
return pos;
3939
}
40+
41+
export function isNil(val: any) {
42+
return val === undefined || val === null;
43+
}

tests/index.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ describe('dialog', () => {
222222
expect(wrapper.find('.rc-dialog-footer').text()).toBe('test');
223223
});
224224

225-
// 失效了,需要修复
226-
it.skip('support input autoFocus', () => {
225+
it('support input autoFocus', () => {
227226
render(
228227
<Dialog visible>
229228
<input autoFocus />

0 commit comments

Comments
 (0)