Skip to content

Instantly share code, notes, and snippets.

@ever-dev
Last active January 6, 2021 08:49
Show Gist options
  • Save ever-dev/514b5be43ea5aaa41b57e48d3a1c1e35 to your computer and use it in GitHub Desktop.
Save ever-dev/514b5be43ea5aaa41b57e48d3a1c1e35 to your computer and use it in GitHub Desktop.
Create a function that renders a dialog when calling a function.
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);
},
);
}
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