Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/DialogWrap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
} = props;
const { scrollLock: _, ...restProps } = props;
const [animatedVisible, setAnimatedVisible] = React.useState<boolean>(visible);
const hasOpenedRef = React.useRef(visible);

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

Expand All @@ -42,11 +43,13 @@ const DialogWrap: React.FC<IDialogPropTypes> = (props) => {
React.useEffect(() => {
if (visible) {
setAnimatedVisible(true);
hasOpenedRef.current = true;
}
}, [visible]);

// Destroy on close will remove wrapped div
if (!forceRender && destroyOnHidden && !animatedVisible) {
// Use `hasOpenedRef` to check if component has ever been opened (after animation completes)
if (destroyOnHidden && hasOpenedRef.current && !visible && !animatedVisible) {
return null;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

Expand Down
26 changes: 26 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ describe('dialog', () => {

expect(document.querySelector<HTMLInputElement>('.test-input')).toHaveValue('');
});

it('should destroy even when forceRender is true', () => {
const Demo: React.FC<Partial<DialogProps>> = (props) => (
<Dialog forceRender destroyOnHidden {...props}>
<input className="test-force-destroy" />
</Dialog>
);

const { rerender } = render(<Demo visible={false} />);

// Force render, but not visible yet - should still render (forceRender)
expect(document.querySelectorAll('.test-force-destroy')).toHaveLength(1);

// Show
rerender(<Demo visible />);
act(() => {
jest.runAllTimers();
});

// Hide - should destroy because destroyOnHidden is true
rerender(<Demo visible={false} />);
act(() => {
jest.runAllTimers();
});
expect(document.querySelectorAll('.test-force-destroy')).toHaveLength(0);
});
});

it('esc to close', () => {
Expand Down
Loading