Skip to content

Commit

Permalink
fix: dialog dont close when mouseDown in content and mouseUp in wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
littleee committed Nov 2, 2020
1 parent f500946 commit 32bc281
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/Dialog/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export interface ContentProps extends IDialogChildProps {
motionName: string;
ariaId: string;
onVisibleChanged: (visible: boolean) => void;
onClick: React.MouseEventHandler;
onMouseDown: React.MouseEventHandler;
onMouseUp: React.MouseEventHandler;
}

export interface ContentRef {
Expand Down Expand Up @@ -42,7 +43,8 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
ariaId,
onClose,
onVisibleChanged,
onClick,
onMouseDown,
onMouseUp,
mousePosition,
} = props;

Expand Down Expand Up @@ -144,7 +146,8 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
ref={motionRef}
style={{ ...motionStyle, ...style, ...contentStyle }}
className={classNames(prefixCls, className, motionClassName)}
onClick={onClick}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
>
<div tabIndex={0} ref={sentinelStartRef} style={sentinelStyle} aria-hidden="true" />
{modalRender ? modalRender(content) : content}
Expand Down
14 changes: 9 additions & 5 deletions src/Dialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,25 @@ export default function Dialog(props: IDialogChildProps) {
const contentTimeoutRef = useRef<number>();

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

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

// >>> Wrapper
// Close only when element not on dialog
let onWrapperClick: (e: React.SyntheticEvent) => void = null;
if (maskClosable) {
onWrapperClick = (e) => {
if (
!contentClickRef.current &&
if(contentClickRef.current) {
contentClickRef.current = false;
} else if (
!contains(contentRef.current.getDOM(), e.target as HTMLElement)
) {
onInternalClose(e);
Expand Down Expand Up @@ -174,7 +177,8 @@ export default function Dialog(props: IDialogChildProps) {
>
<Content
{...props}
onClick={onContentClick}
onMouseDown={onContentMouseDown}
onMouseUp={onContentMouseUp}
ref={contentRef}
closable={closable}
ariaId={ariaIdRef.current}
Expand Down
2 changes: 2 additions & 0 deletions tests/portal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ describe('Dialog.Portal', () => {
jest.runAllTimers();
});

wrapper.find('.rc-dialog-content').simulate('mousedown');
wrapper.find('.rc-select-item-option-content').simulate('click');
wrapper.find('.rc-dialog-content').simulate('mouseup');
expect(onClose).not.toHaveBeenCalled();
});
});

0 comments on commit 32bc281

Please sign in to comment.