Last active
January 6, 2021 08:49
-
-
Save ever-dev/514b5be43ea5aaa41b57e48d3a1c1e35 to your computer and use it in GitHub Desktop.
Create a function that renders a dialog when calling a function.
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 * as React from "react"; | |
import * as ReactDOM from "react-dom"; | |
import { ConfirmDialog } from "./dialog"; | |
export function renderConfirmDialog( | |
title: string, | |
subTitle: string, | |
confirmLabel: string, | |
cancelLabel: string, | |
): Promise<boolean> { | |
const wrapper = document.body.appendChild(document.createElement("div")); | |
const promise = new Promise((resolve, reject) => { | |
try { | |
ReactDOM.render( | |
<ConfirmDialog | |
reject={reject} | |
resolve={resolve} | |
title={title} | |
subText={subTitle} | |
confirmLabel={confirmLabel} | |
cancelLabel={cancelLabel} | |
/>, | |
wrapper, | |
); | |
} catch (e) { | |
console.error(e); | |
throw e; | |
} | |
}); | |
function dispose() { | |
setTimeout(() => { | |
ReactDOM.unmountComponentAtNode(wrapper); | |
setTimeout(() => wrapper.remove()); | |
}, 1000); | |
} | |
return promise.then( | |
() => { | |
dispose(); | |
return true; | |
}, | |
() => { | |
dispose(); | |
return Promise.reject(false); | |
}, | |
); | |
} |
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
renderConfirmDialog( | |
"title", | |
"content", | |
"Yes", | |
"No", | |
) | |
.then(() => { | |
// process yes | |
}) | |
.catch(() => { | |
// process no | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment