close all dialogs on ESC or click outside

pull/869/head
Audric Ackermann 5 years ago
parent 8767a7a829
commit 99133437d6
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -24,6 +24,17 @@
}; };
}, },
registerEvents() {
this.unregisterEvents();
document.addEventListener('mousedown', this.props.onClickClose, false);
document.addEventListener('keyup', this.props.onClickClose, false);
},
unregisterEvents() {
document.removeEventListener('mousedown', this.props.onClickClose, false);
document.removeEventListener('keyup', this.props.onClickClose, false);
},
render() { render() {
this.$('.session-confirm-wrapper').remove(); this.$('.session-confirm-wrapper').remove();
@ -32,25 +43,29 @@
Component: window.Signal.Components.SessionConfirm, Component: window.Signal.Components.SessionConfirm,
props: this.props, props: this.props,
}); });
this.registerEvents();
this.$el.prepend(this.confirmView.el); this.$el.prepend(this.confirmView.el);
}, },
ok() { ok() {
this.$('.session-confirm-wrapper').remove(); this.$('.session-confirm-wrapper').remove();
this.unregisterEvents();
if (this.props.resolve) { if (this.props.resolve) {
this.props.resolve(); this.props.resolve();
} }
}, },
cancel() { cancel() {
this.$('.session-confirm-wrapper').remove(); this.$('.session-confirm-wrapper').remove();
this.unregisterEvents();
if (this.props.reject) { if (this.props.reject) {
this.props.reject(); this.props.reject();
} }
}, },
onKeyup(event) { onKeyup(event) {
if (event.key === 'Escape' || event.key === 'Esc') { if (event.key === 'Escape' || event.key === 'Esc') {
this.cancel(); this.unregisterEvents();
this.props.onClickClose();
} }
}, },
}); });

@ -36,6 +36,8 @@ export class SessionModal extends React.PureComponent<Props, State> {
headerReverse: false, headerReverse: false,
}; };
private node: HTMLDivElement | null;
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.state = { this.state = {
@ -44,10 +46,27 @@ export class SessionModal extends React.PureComponent<Props, State> {
this.close = this.close.bind(this); this.close = this.close.bind(this);
this.onKeyUp = this.onKeyUp.bind(this); this.onKeyUp = this.onKeyUp.bind(this);
this.node = null;
window.addEventListener('keyup', this.onKeyUp); window.addEventListener('keyup', this.onKeyUp);
} }
public componentWillMount() {
document.addEventListener('mousedown', this.handleClick, false);
}
public componentWillUnmount() {
document.removeEventListener('mousedown', this.handleClick, false);
}
public handleClick = (e: any) => {
if (this.node && this.node.contains(e.target)) {
return;
}
this.close();
};
public render() { public render() {
const { const {
title, title,
@ -59,7 +78,7 @@ export class SessionModal extends React.PureComponent<Props, State> {
const { isVisible } = this.state; const { isVisible } = this.state;
return isVisible ? ( return isVisible ? (
<div className={'session-modal'}> <div ref={node => (this.node = node)} className={'session-modal'}>
{showHeader ? ( {showHeader ? (
<> <>
<div <div

Loading…
Cancel
Save