Created
December 11, 2018 19:02
-
-
Save pokidovea/74e078382a76a6f9debc686078e94e80 to your computer and use it in GitHub Desktop.
A confirmation dialog on React
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 React from 'react' | |
import ReactDOM from 'react-dom' | |
import PropTypes from 'prop-types' | |
import { Modal, ModalHeader, ModalBody, ModalFooter, Button } from 'reactstrap' | |
const ConfirmationModal = props => { | |
return ( | |
<Modal isOpen={true} toggle={props.onNo} backdrop={true}> | |
<ModalHeader toggle={props.onNo}>{props.title}</ModalHeader> | |
<ModalBody>{props.content}</ModalBody> | |
<ModalFooter> | |
<Button color="primary" onClick={props.onYes}> | |
Yes | |
</Button>{' '} | |
<Button color="secondary" onClick={props.onNo}> | |
No | |
</Button> | |
</ModalFooter> | |
</Modal> | |
) | |
} | |
ConfirmationModal.propTypes = { | |
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]), | |
title: PropTypes.string.isRequired, | |
onYes: PropTypes.func.isRequired, | |
onNo: PropTypes.func.isRequired, | |
} | |
export default function confirmation(title, content) { | |
return new Promise((resolve, reject) => { | |
const wrapper = document.body.appendChild(document.createElement('div')) | |
const close = () => { | |
ReactDOM.unmountComponentAtNode(wrapper) | |
return setTimeout(() => { | |
return wrapper.remove() | |
}) | |
} | |
const onYes = event => { | |
event.stopPropagation() | |
resolve() | |
close() | |
} | |
const onNo = event => { | |
event.stopPropagation() | |
reject() | |
close() | |
} | |
ReactDOM.render( | |
<ConfirmationModal title={title} content={content} onYes={onYes} onNo={onNo} />, | |
wrapper | |
) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment