Skip to content

Commit

Permalink
Prevent close if user mouseDown trigger on the dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Apr 5, 2019
1 parent a701a7c commit b4dc00b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-dialog",
"version": "7.3.0",
"version": "7.3.1",
"description": "dialog ui component for react",
"keywords": [
"react",
Expand Down
22 changes: 20 additions & 2 deletions src/Dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
private sentinelEnd: HTMLElement;
private bodyIsOverflowing: boolean;
private scrollbarWidth: number;
private dialogMouseDown: boolean;
private timeoutId: number;

componentWillMount() {
this.inTransition = false;
Expand Down Expand Up @@ -114,6 +116,7 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
if (this.props.visible || this.inTransition) {
this.removeScrollingEffect();
}
clearTimeout(this.timeoutId);
}

tryFocus() {
Expand All @@ -136,12 +139,25 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
afterClose();
}
}

onDialogMouseDown = () => {
this.dialogMouseDown = true;
}

onMaskMouseUp: React.MouseEventHandler<HTMLDivElement> = () => {
if (this.dialogMouseDown) {
this.timeoutId = setTimeout(() => {
this.dialogMouseDown = false;
}, 0);
}
}

onMaskClick = (e: React.MouseEvent<HTMLDivElement>) => {
// android trigger click on open (fastclick??)
if (Date.now() - this.openTime < 300) {
return;
}
if (e.target === e.currentTarget) {
if (e.target === e.currentTarget && !this.dialogMouseDown) {
this.close(e);
}
}
Expand Down Expand Up @@ -222,6 +238,7 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
style={style}
className={`${prefixCls} ${props.className || ''}`}
visible={props.visible}
onMouseDown={this.onDialogMouseDown}
>
<div tabIndex={0} ref={this.saveRef('sentinelStart')} style={sentinelStyle}>
sentinelStart
Expand Down Expand Up @@ -402,7 +419,8 @@ export default class Dialog extends React.Component<IDialogPropTypes, any> {
onKeyDown={this.onKeyDown}
className={`${prefixCls}-wrap ${props.wrapClassName || ''}`}
ref={this.saveRef('wrap')}
onClick={maskClosable ? this.onMaskClick : undefined}
onClick={maskClosable ? this.onMaskClick : null}
onMouseUp={maskClosable ? this.onMaskMouseUp : null}
role="dialog"
aria-labelledby={props.title ? this.titleId : null}
style={style}
Expand Down
1 change: 1 addition & 0 deletions src/LazyRenderBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface ILazyRenderBoxPropTypes {
hiddenClassName?: string;
role?: string;
style?: {};
onMouseDown?: React.MouseEventHandler<HTMLDivElement>;
}

export default class LazyRenderBox extends React.Component<ILazyRenderBoxPropTypes, any> {
Expand Down
13 changes: 12 additions & 1 deletion tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,16 @@ describe('dialog', () => {
</Dialog>
),container)
expect($('.rc-dialog-body > div').text()).to.be('forceRender element')
})
});

it('should not close if mouse down in dialog', () => {
dialog.setState({
visible: true,
});
const dialogBody = $('.rc-dialog-body')[0];
Simulate.mouseDown(dialogBody);
const wrapper = $('.rc-dialog-wrap')[0];
Simulate.mouseUp(wrapper);
expect(dialog.state.visible).to.be(true);
});
});

0 comments on commit b4dc00b

Please sign in to comment.