Spaces:
Running
Running
File size: 1,780 Bytes
6bcb42f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import ModalComponent from '../components/modal/modal.jsx';
class Modal extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'addEventListeners',
'removeEventListeners',
'handlePopState',
'pushHistory'
]);
this.addEventListeners();
}
componentDidMount () {
// Add a history event only if it's not currently for our modal. This
// avoids polluting the history with many entries. We only need one.
this.pushHistory(this.id, (history.state === null || history.state !== this.id));
}
componentWillUnmount () {
this.removeEventListeners();
}
addEventListeners () {
window.addEventListener('popstate', this.handlePopState);
}
removeEventListeners () {
window.removeEventListener('popstate', this.handlePopState);
}
handlePopState () {
// Whenever someone navigates, we want to be closed
this.props.onRequestClose();
}
get id () {
return `modal-${this.props.id}`;
}
pushHistory (state, push) {
if (push) return history.pushState(state, this.id, null);
history.replaceState(state, this.id, null);
}
render () {
return <ModalComponent {...this.props} />;
}
}
Modal.propTypes = {
id: PropTypes.string.isRequired,
isRtl: PropTypes.bool,
onRequestClose: PropTypes.func,
onRequestOpen: PropTypes.func,
scrollable: PropTypes.bool
};
const mapStateToProps = state => ({
isRtl: state.locales.isRtl
});
export default connect(
mapStateToProps
)(Modal);
|