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: dialog dont close when mouseDown in content and mouseUp in wrapper #210

Merged
merged 1 commit into from
Dec 3, 2020
Merged
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
9 changes: 6 additions & 3 deletions src/Dialog/Content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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 @@ -43,7 +44,8 @@ const Content = React.forwardRef<ContentRef, ContentProps>((props, ref) => {
ariaId,
onClose,
onVisibleChanged,
onClick,
onMouseDown,
onMouseUp,
mousePosition,
} = props;

Expand Down Expand Up @@ -145,7 +147,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" />
<MemoChildren visible={visible}>
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
20 changes: 20 additions & 0 deletions tests/portal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,27 @@ 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();
});

it('dialog dont close when mouseDown in content and mouseUp in wrap', () => {
const onClose = jest.fn();

const wrapper = mount(
<Dialog onClose={onClose} visible>
content
</Dialog>,
);

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

wrapper.find('.rc-dialog-content').simulate('mousedown');
wrapper.find('.rc-dialog-wrap').simulate('mouseup');
expect(onClose).not.toHaveBeenCalled();
});
});