diff --git a/src/Dialog/index.tsx b/src/Dialog/index.tsx index 3dc6a661..33983508 100644 --- a/src/Dialog/index.tsx +++ b/src/Dialog/index.tsx @@ -48,6 +48,7 @@ export default function Dialog(props: IDialogPropTypes) { const contentRef = useRef(); const [animatedVisible, setAnimatedVisible] = React.useState(visible); + const [originFocusEl, setOriginFocusEl] = React.useState(); // ========================== Init ========================== const ariaId = useId(); @@ -59,7 +60,11 @@ export default function Dialog(props: IDialogPropTypes) { } function focusDialogContent() { - if (!contains(wrapperRef.current, document.activeElement)) { + if (originFocusEl) { + originFocusEl.focus(); + } else if (contains(wrapperRef.current, document.activeElement)) { + setOriginFocusEl(document.activeElement as HTMLElement); + } else { contentRef.current?.focus(); } } diff --git a/src/DialogWrap.tsx b/src/DialogWrap.tsx index 344a9631..8fcc77f9 100644 --- a/src/DialogWrap.tsx +++ b/src/DialogWrap.tsx @@ -1,7 +1,8 @@ -import * as React from 'react'; import Portal from '@rc-component/portal'; +import * as React from 'react'; import Dialog from './Dialog'; import type { IDialogPropTypes } from './IDialogPropTypes'; +import { isNil } from './util'; // fix issue #10656 /* @@ -41,7 +42,7 @@ const DialogWrap: React.FC = (props: IDialogPropTypes) => { { expect(wrapper.find('.rc-dialog-footer').text()).toBe('test'); }); - // 失效了,需要修复 - it.skip('support input autoFocus', () => { + it('support input autoFocus', () => { render(