Skip to content

Commit d185715

Browse files
authored
fix: dialog dont close when mouseDown in content and mouseUp in wrapper (#210)
test: add test
1 parent cba467a commit d185715

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/Dialog/Content/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export interface ContentProps extends IDialogChildProps {
1212
motionName: string;
1313
ariaId: string;
1414
onVisibleChanged: (visible: boolean) => void;
15-
onClick: React.MouseEventHandler;
15+
onMouseDown: React.MouseEventHandler;
16+
onMouseUp: React.MouseEventHandler;
1617
}
1718

1819
export interface ContentRef {
@@ -43,7 +44,8 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
4344
ariaId,
4445
onClose,
4546
onVisibleChanged,
46-
onClick,
47+
onMouseDown,
48+
onMouseUp,
4749
mousePosition,
4850
} = props;
4951

@@ -145,7 +147,8 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
145147
ref={motionRef}
146148
style={{ ...motionStyle, ...style, ...contentStyle }}
147149
className={classNames(prefixCls, className, motionClassName)}
148-
onClick={onClick}
150+
onMouseDown={onMouseDown}
151+
onMouseUp={onMouseUp}
149152
>
150153
<div tabIndex={0} ref={sentinelStartRef} style={sentinelStyle} aria-hidden="true" />
151154
<MemoChildren visible={visible}>

src/Dialog/index.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,25 @@ export default function Dialog(props: IDialogChildProps) {
9393
const contentTimeoutRef = useRef<number>();
9494

9595
// We need record content click incase content popup out of dialog
96-
const onContentClick: React.MouseEventHandler = () => {
96+
const onContentMouseDown: React.MouseEventHandler = () => {
9797
clearTimeout(contentTimeoutRef.current);
9898
contentClickRef.current = true;
99+
}
99100

101+
const onContentMouseUp: React.MouseEventHandler = () => {
100102
contentTimeoutRef.current = setTimeout(() => {
101103
contentClickRef.current = false;
102104
});
103-
};
105+
}
104106

105107
// >>> Wrapper
106108
// Close only when element not on dialog
107109
let onWrapperClick: (e: React.SyntheticEvent) => void = null;
108110
if (maskClosable) {
109111
onWrapperClick = (e) => {
110-
if (
111-
!contentClickRef.current &&
112+
if(contentClickRef.current) {
113+
contentClickRef.current = false;
114+
} else if (
112115
!contains(contentRef.current.getDOM(), e.target as HTMLElement)
113116
) {
114117
onInternalClose(e);
@@ -174,7 +177,8 @@ export default function Dialog(props: IDialogChildProps) {
174177
>
175178
<Content
176179
{...props}
177-
onClick={onContentClick}
180+
onMouseDown={onContentMouseDown}
181+
onMouseUp={onContentMouseUp}
178182
ref={contentRef}
179183
closable={closable}
180184
ariaId={ariaIdRef.current}

tests/portal.spec.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,27 @@ describe('Dialog.Portal', () => {
3333
jest.runAllTimers();
3434
});
3535

36+
wrapper.find('.rc-dialog-content').simulate('mousedown');
3637
wrapper.find('.rc-select-item-option-content').simulate('click');
38+
wrapper.find('.rc-dialog-content').simulate('mouseup');
39+
expect(onClose).not.toHaveBeenCalled();
40+
});
41+
42+
it('dialog dont close when mouseDown in content and mouseUp in wrap', () => {
43+
const onClose = jest.fn();
44+
45+
const wrapper = mount(
46+
<Dialog onClose={onClose} visible>
47+
content
48+
</Dialog>,
49+
);
50+
51+
act(() => {
52+
jest.runAllTimers();
53+
});
54+
55+
wrapper.find('.rc-dialog-content').simulate('mousedown');
56+
wrapper.find('.rc-dialog-wrap').simulate('mouseup');
3757
expect(onClose).not.toHaveBeenCalled();
3858
});
3959
});

0 commit comments

Comments
 (0)