2017-08-23 00:19:50 +05:30
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
2019-06-30 19:02:50 +05:30
|
|
|
import { TransitionGroup, CSSTransition } from 'react-transition-group';
|
2017-05-26 00:41:57 +05:30
|
|
|
import { browserHistory } from 'services/history';
|
2019-06-30 19:02:50 +05:30
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { destroy } from 'components/ui/popup/actions';
|
2016-06-04 01:08:59 +05:30
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
import styles from './popup.scss';
|
|
|
|
|
|
|
|
export class PopupStack extends Component {
|
|
|
|
static propTypes = {
|
2019-06-30 19:02:50 +05:30
|
|
|
popups: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
type: PropTypes.func,
|
|
|
|
props: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
|
|
|
|
})
|
|
|
|
),
|
2016-05-01 23:20:55 +05:30
|
|
|
destroy: PropTypes.func.isRequired
|
|
|
|
};
|
|
|
|
|
2016-07-26 10:50:37 +05:30
|
|
|
componentWillMount() {
|
|
|
|
document.addEventListener('keyup', this.onKeyPress);
|
2017-05-26 00:41:57 +05:30
|
|
|
this.unlistenTransition = browserHistory.listen(this.onRouteLeave);
|
2016-07-26 10:50:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('keyup', this.onKeyPress);
|
2016-07-27 00:58:58 +05:30
|
|
|
this.unlistenTransition();
|
2016-07-26 10:50:37 +05:30
|
|
|
}
|
|
|
|
|
2016-05-01 23:20:55 +05:30
|
|
|
render() {
|
2019-06-30 19:02:50 +05:30
|
|
|
const { popups } = this.props;
|
2016-05-01 23:20:55 +05:30
|
|
|
|
|
|
|
return (
|
2019-06-30 19:02:50 +05:30
|
|
|
<TransitionGroup>
|
2016-05-01 23:20:55 +05:30
|
|
|
{popups.map((popup, index) => {
|
2019-06-30 19:02:50 +05:30
|
|
|
const { Popup } = popup;
|
2016-05-01 23:20:55 +05:30
|
|
|
|
|
|
|
return (
|
2019-06-30 19:02:50 +05:30
|
|
|
<CSSTransition
|
|
|
|
key={index}
|
|
|
|
classNames={{
|
|
|
|
enter: styles.trEnter,
|
|
|
|
enterActive: styles.trEnterActive,
|
|
|
|
exit: styles.trExit,
|
|
|
|
exitActive: styles.trExitActive
|
|
|
|
}}
|
|
|
|
timeout={500}
|
2016-07-26 10:10:45 +05:30
|
|
|
>
|
2019-06-30 19:02:50 +05:30
|
|
|
<div
|
|
|
|
className={styles.overlay}
|
|
|
|
onClick={this.onOverlayClick(popup)}
|
|
|
|
>
|
|
|
|
<Popup onClose={this.onClose(popup)} />
|
|
|
|
</div>
|
|
|
|
</CSSTransition>
|
2016-05-01 23:20:55 +05:30
|
|
|
);
|
|
|
|
})}
|
2019-06-30 19:02:50 +05:30
|
|
|
</TransitionGroup>
|
2016-05-01 23:20:55 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClose(popup) {
|
|
|
|
return this.props.destroy.bind(null, popup);
|
|
|
|
}
|
2016-06-03 00:42:20 +05:30
|
|
|
|
2016-07-26 10:10:45 +05:30
|
|
|
onOverlayClick(popup) {
|
2016-06-03 00:42:20 +05:30
|
|
|
return (event) => {
|
2019-06-30 19:02:50 +05:30
|
|
|
if (
|
|
|
|
event.target !== event.currentTarget
|
|
|
|
|| popup.disableOverlayClose
|
|
|
|
) {
|
2016-06-03 00:42:20 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
this.props.destroy(popup);
|
|
|
|
};
|
|
|
|
}
|
2016-07-26 10:50:37 +05:30
|
|
|
|
2016-07-27 00:58:58 +05:30
|
|
|
popStack() {
|
|
|
|
const popup = this.props.popups.slice(-1)[0];
|
|
|
|
|
|
|
|
if (popup && !popup.disableOverlayClose) {
|
|
|
|
this.props.destroy(popup);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 10:50:37 +05:30
|
|
|
onKeyPress = (event) => {
|
2019-06-30 19:02:50 +05:30
|
|
|
if (event.which === 27) {
|
|
|
|
// ESC key
|
2016-07-27 00:58:58 +05:30
|
|
|
this.popStack();
|
|
|
|
}
|
|
|
|
};
|
2016-07-26 10:50:37 +05:30
|
|
|
|
2016-07-27 00:58:58 +05:30
|
|
|
onRouteLeave = (nextLocation) => {
|
|
|
|
if (nextLocation) {
|
|
|
|
this.popStack();
|
2016-07-26 10:50:37 +05:30
|
|
|
}
|
|
|
|
};
|
2016-05-01 23:20:55 +05:30
|
|
|
}
|
|
|
|
|
2019-06-30 19:02:50 +05:30
|
|
|
export default connect(
|
|
|
|
(state) => ({
|
|
|
|
...state.popup
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
destroy
|
|
|
|
}
|
|
|
|
)(PopupStack);
|