accounts-frontend/src/components/ui/popup/PopupStack.js

112 lines
3.0 KiB
JavaScript
Raw Normal View History

import PropTypes from 'prop-types';
import React, { Component } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import { browserHistory } from 'services/history';
import { connect } from 'react-redux';
import { destroy } from 'components/ui/popup/actions';
2016-06-04 01:08:59 +05:30
import styles from './popup.scss';
export class PopupStack extends Component {
static propTypes = {
popups: PropTypes.arrayOf(
PropTypes.shape({
type: PropTypes.func,
props: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
})
),
destroy: PropTypes.func.isRequired
};
2016-07-26 10:50:37 +05:30
componentWillMount() {
document.addEventListener('keyup', this.onKeyPress);
this.unlistenTransition = browserHistory.listen(this.onRouteLeave);
2016-07-26 10:50:37 +05:30
}
componentWillUnmount() {
document.removeEventListener('keyup', this.onKeyPress);
this.unlistenTransition();
2016-07-26 10:50:37 +05:30
}
render() {
const { popups } = this.props;
return (
<TransitionGroup>
{popups.map((popup, index) => {
const { Popup } = popup;
return (
<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
>
<div
className={styles.overlay}
onClick={this.onOverlayClick(popup)}
>
<Popup onClose={this.onClose(popup)} />
</div>
</CSSTransition>
);
})}
</TransitionGroup>
);
}
onClose(popup) {
return this.props.destroy.bind(null, popup);
}
2016-07-26 10:10:45 +05:30
onOverlayClick(popup) {
return (event) => {
if (
event.target !== event.currentTarget
|| popup.disableOverlayClose
) {
return;
}
event.preventDefault();
this.props.destroy(popup);
};
}
2016-07-26 10:50:37 +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) => {
if (event.which === 27) {
// ESC key
this.popStack();
}
};
2016-07-26 10:50:37 +05:30
onRouteLeave = (nextLocation) => {
if (nextLocation) {
this.popStack();
2016-07-26 10:50:37 +05:30
}
};
}
export default connect(
(state) => ({
...state.popup
}),
{
destroy
}
)(PopupStack);