Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: destroyOnClose fails with forceRender #232

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/DialogWrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ import { IDialogPropTypes } from './IDialogPropTypes';

const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
const { visible, getContainer, forceRender, destroyOnClose = false, afterClose } = props;
const [forceRenderTime, setForceRenderTime] = React.useState<boolean>(forceRender);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forceRenderTime 是啥含义?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该是forceRender的映射,后续进行状态管理

const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);

React.useEffect(() => {
if (visible) {
setAnimatedVisible(true);
if (destroyOnClose) {
setForceRenderTime(false);
}
}
}, [visible]);

React.useEffect(() => {
setForceRenderTime(forceRender);
}, [forceRender]);

// 渲染在当前 dom 里;
if (getContainer === false) {
return (
Expand All @@ -33,15 +41,16 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props: IDialogPropTypes) => {
}

// Destroy on close will remove wrapped div
if (!forceRender && destroyOnClose && !animatedVisible) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是直接把 !forceRender 去掉就行了?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接去掉会导致forceRender这个属性失效😅

if (!forceRenderTime && destroyOnClose && !animatedVisible) {
return null;
}

return (
<Portal visible={visible} forceRender={forceRender} getContainer={getContainer}>
<Portal visible={visible} forceRender={forceRenderTime} getContainer={getContainer}>
{(childProps: IDialogChildProps) => (
<Dialog
{...props}
forceRender={forceRenderTime}
destroyOnClose={destroyOnClose}
afterClose={() => {
afterClose?.();
Expand Down
23 changes: 23 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,29 @@ describe('dialog', () => {
});
});

it('destroy component when forceRender is true', () => {
const wrapper = mount(
<Dialog destroyOnClose forceRender>
<div>forceRender destroyOnClose</div>
</Dialog>,
);

expect(wrapper.find('.rc-dialog-body > div').text()).toEqual('forceRender destroyOnClose');

// Hide
wrapper.setProps({ visible: true });
jest.runAllTimers();
wrapper.update();

// Show
wrapper.setProps({ visible: false });
jest.runAllTimers();
wrapper.update();

expect(wrapper.find('.rc-dialog-body > div')).toHaveLength(0);
wrapper.unmount();
});

it('esc to close', () => {
const onClose = jest.fn();
const wrapper = mount(<Dialog onClose={onClose} visible />);
Expand Down