2019-12-07 16:58:52 +05:30
|
|
|
import React from 'react';
|
2019-06-30 19:02:50 +05:30
|
|
|
import { TransitionGroup, CSSTransition } from 'react-transition-group';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { browserHistory } from 'app/services/history';
|
2019-06-30 19:02:50 +05:30
|
|
|
import { connect } from 'react-redux';
|
2019-12-08 00:32:00 +05:30
|
|
|
import { RootState } from 'app/reducers';
|
2016-06-04 01:08:59 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
import { PopupConfig } from './reducer';
|
|
|
|
import { destroy } from './actions';
|
2016-05-01 23:20:55 +05:30
|
|
|
import styles from './popup.scss';
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export class PopupStack extends React.Component<{
|
|
|
|
popups: PopupConfig[];
|
|
|
|
destroy: (popup: PopupConfig) => void;
|
|
|
|
}> {
|
|
|
|
unlistenTransition: () => void;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-10 13:17:32 +05:30
|
|
|
componentDidMount() {
|
2019-11-27 14:33:32 +05:30
|
|
|
document.addEventListener('keyup', this.onKeyPress);
|
|
|
|
this.unlistenTransition = browserHistory.listen(this.onRouteLeave);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('keyup', this.onKeyPress);
|
|
|
|
this.unlistenTransition();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { popups } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<TransitionGroup>
|
|
|
|
{popups.map((popup, index) => {
|
|
|
|
const { Popup } = popup;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CSSTransition
|
|
|
|
key={index}
|
2019-12-29 17:27:44 +05:30
|
|
|
classNames={{
|
2019-11-27 14:33:32 +05:30
|
|
|
enter: styles.trEnter,
|
|
|
|
enterActive: styles.trEnterActive,
|
|
|
|
exit: styles.trExit,
|
|
|
|
exitActive: styles.trExitActive,
|
|
|
|
}}
|
|
|
|
timeout={500}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={styles.overlay}
|
|
|
|
onClick={this.onOverlayClick(popup)}
|
|
|
|
>
|
|
|
|
<Popup onClose={this.onClose(popup)} />
|
|
|
|
</div>
|
|
|
|
</CSSTransition>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</TransitionGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
onClose(popup: PopupConfig) {
|
2019-11-27 14:33:32 +05:30
|
|
|
return this.props.destroy.bind(null, popup);
|
|
|
|
}
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
onOverlayClick(popup: PopupConfig) {
|
|
|
|
return (event: React.MouseEvent) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (event.target !== event.currentTarget || popup.disableOverlayClose) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.props.destroy(popup);
|
2016-05-01 23:20:55 +05:30
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
popStack() {
|
2019-12-07 16:58:52 +05:30
|
|
|
const [popup] = this.props.popups.slice(-1);
|
2016-07-26 10:50:37 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (popup && !popup.disableOverlayClose) {
|
|
|
|
this.props.destroy(popup);
|
2016-07-26 10:50:37 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
}
|
2016-07-26 10:50:37 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
onKeyPress = (event: KeyboardEvent) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (event.which === 27) {
|
|
|
|
// ESC key
|
|
|
|
this.popStack();
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
2016-05-01 23:20:55 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
onRouteLeave = nextLocation => {
|
|
|
|
if (nextLocation) {
|
|
|
|
this.popStack();
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
2019-11-27 14:33:32 +05:30
|
|
|
};
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
export default connect(
|
2019-12-07 16:58:52 +05:30
|
|
|
(state: RootState) => ({
|
2019-11-27 14:33:32 +05:30
|
|
|
...state.popup,
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
destroy,
|
|
|
|
},
|
2019-06-30 19:02:50 +05:30
|
|
|
)(PopupStack);
|