Skip to content

Instantly share code, notes, and snippets.

@efalayi
Last active October 21, 2019 23:52
Show Gist options
  • Select an option

  • Save efalayi/95fdb6bf8bad4cc3e52771431272c109 to your computer and use it in GitHub Desktop.

Select an option

Save efalayi/95fdb6bf8bad4cc3e52771431272c109 to your computer and use it in GitHub Desktop.
import React from 'react'
import { connect } from 'react-redux'
import ReactModal from 'react-modal'
import { default as modalTypes } from './components/Modals'
const MODAL_TYPES = {
'alert': modalTypes.alertModal,
'confirm': modalTypes.confirmModal,
'delete': modalTypes.deleteModal,
'prompt': modalTypes.promptModal
}
const mapStateToProps = state => ({
...state.modal
})
class ModalContainer extends React.Component {
constructor(props) {
super(props)
this.state = {
modalIsOpen: props.modalProps.open
}
this.closeModal = this.closeModal.bind(this)
}
componentWillReceiveProps(nextProps) {
if (nextProps.modalProps.open !== this.props.modalProps.open) {
this.setState({
modalIsOpen: nextProps.modalProps.open
})
}
}
closeModal() {
this.props.hideModal()
}
render() {
if (!this.props.modalType) {
return null
}
const SpecifiedModal = MODAL_TYPES[this.props.modalType]
return (
<div>
<ReactModal
isOpen={this.state.modalIsOpen}
onRequestClose={this.closeModal}
contentLabel="Example Modal"
ariaHideApp={false}
overlayClassName="modal fade show"
bodyOpenClassName="modal-open"
className="modal-dialog modal-dialog-centered"
>
<SpecifiedModal
closeModal={this.closeModal}
{...this.props.modalProps}
/>
</ReactModal>
</div>
)
}
}
export default connect(mapStateToProps, null)(ModalContainer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment