-
-
Save vitaLee/1dece923e429fe50c2ec42000253e3e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import withModal from 'withModal'; | |
function MyPage({ openModal, closeModal }) { | |
const onClick = () => openModal(<div>Modal content. <button onClick={this.closeModal}>close</button></div>); | |
return ( | |
<p> | |
<h1>Title</h1> | |
<button onClick={onClick}>open modal</button> | |
</p> | |
); | |
} | |
export default withModal(MyPage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function withModal(component) => { | |
return class withModal extends React.Component { | |
static displayName = `withModal(${component.displayName || component.name || 'Component'})`; | |
state = { modal: null }; | |
render() { | |
if (this.state.modal) { | |
return ( | |
<div> | |
{this.renderComponent()} | |
<Modal isOpen={true}>{this.state.modal}</Modal> | |
</div> | |
); | |
} | |
return this.renderComponent(); | |
} | |
renderComponent() { | |
return React.createElement(component, { ...this.props, openModal: this.openModal, closeModal: this.closeModal }); | |
} | |
closeModal = () => { | |
this.setState({ modal: null }); | |
}; | |
openModal = (modal) => { | |
this.setState({ modal }); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment