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
39 changes: 12 additions & 27 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,31 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
}

// >>> Content
const contentClickRef = useRef(false);
const contentTimeoutRef = useRef<ReturnType<typeof setTimeout>>(null);

// We need record content click in case content popup out of dialog
const onContentMouseDown: React.MouseEventHandler = () => {
clearTimeout(contentTimeoutRef.current);
contentClickRef.current = true;
};

const onContentMouseUp: React.MouseEventHandler = () => {
contentTimeoutRef.current = setTimeout(() => {
contentClickRef.current = false;
});
};
const mouseDownOnMaskRef = useRef(false);

// >>> Wrapper
// Close only when element not on dialog
let onWrapperClick: (e: React.SyntheticEvent) => void = null;
if (maskClosable) {
onWrapperClick = (e) => {
if (contentClickRef.current) {
contentClickRef.current = false;
} else if (wrapperRef.current === e.target) {
if (
onClose &&
wrapperRef.current === e.target &&
mouseDownOnMaskRef.current
) {
onInternalClose(e);
}
};
}

function onWrapperMouseDown(e: React.MouseEvent) {
mouseDownOnMaskRef.current = e.target === wrapperRef.current;
}

// ========================= Effect =========================
useEffect(() => {
if (visible) {
mouseDownOnMaskRef.current = false;
setAnimatedVisible(true);
saveLastOutSideActiveElementRef();

Expand All @@ -158,14 +152,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
}
}, [visible]);

// Remove direct should also check the scroll bar update
useEffect(
() => () => {
clearTimeout(contentTimeoutRef.current);
},
[],
);

const mergedStyle: React.CSSProperties = {
zIndex,
...wrapStyle,
Expand All @@ -192,14 +178,13 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
className={clsx(`${prefixCls}-wrap`, wrapClassName, modalClassNames?.wrapper)}
ref={wrapperRef}
onClick={onWrapperClick}
onMouseDown={onWrapperMouseDown}
style={mergedStyle}
{...wrapProps}
>
<Content
{...props}
isFixedPos={isFixedPos}
onMouseDown={onContentMouseDown}
onMouseUp={onContentMouseUp}
ref={contentRef}
closable={closable}
ariaId={ariaId}
Expand Down
5 changes: 4 additions & 1 deletion tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ describe('dialog', () => {
const { rerender } = render(<Dialog onClose={onClose} visible />);

// Mask close
fireEvent.click(document.querySelector('.rc-dialog-wrap'));
const mask = document.querySelector('.rc-dialog-wrap');
fireEvent.mouseDown(mask);
fireEvent.mouseUp(mask);
fireEvent.click(mask);
jest.runAllTimers();
expect(onClose).toHaveBeenCalled();
onClose.mockReset();
Expand Down
60 changes: 60 additions & 0 deletions tests/mask-closable.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import { render, fireEvent, act } from '@testing-library/react';
import Dialog from '../src';

describe('Dialog.MaskClosable', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.useRealTimers();
});

it('should close when click on mask', () => {
const onClose = jest.fn();
render(
<Dialog visible maskClosable onClose={onClose}>
Content
</Dialog>
);

act(() => {
jest.runAllTimers();
});

const mask = document.querySelector('.rc-dialog-wrap');
if (!mask) throw new Error('Mask not found');

fireEvent.mouseDown(mask);
fireEvent.mouseUp(mask);
fireEvent.click(mask);
expect(onClose).toHaveBeenCalled();
});

it('should not close when dragging from content to mask', () => {
const onClose = jest.fn();
const { getByText } = render(
<Dialog visible maskClosable onClose={onClose}>
Content
</Dialog>
);

act(() => {
jest.runAllTimers();
});

const content = getByText('Content');
const mask = document.querySelector('.rc-dialog-wrap');
if (!mask) throw new Error('Mask not found');

// Simulate mouse down on content
fireEvent.mouseDown(content);
// Simulate mouse up on mask
fireEvent.mouseUp(mask);
// Simulate click on mask (since click follows mouseup)
fireEvent.click(mask);

expect(onClose).not.toHaveBeenCalled();
});
});