Commit 61183cb7 authored by Valeriy Panov's avatar Valeriy Panov Committed by Matija Obreza
Browse files

Confirmation dialog

parent 7668297b
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import Button from 'material-ui/Button';
import Dialog, {
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
} from 'material-ui/Dialog';
interface IConfirmProps extends React.ClassAttributes<any> {
message: string;
description: string;
confirmLabel?: string;
abortLabel?: string;
}
function PromiseWrapper() {
this.with = (callback) => new Promise((resolve, reject) => {
this.resolve = () => {
resolve();
callback();
};
this.reject = () => {
reject();
callback();
};
});
}
class Confirm extends React.Component<IConfirmProps, any> {
public promise = new PromiseWrapper();
private resolve = () => {
this.promise.resolve();
}
private reject = () => {
this.promise.reject();
}
public render() {
const {message, description, confirmLabel = 'OK', abortLabel = 'Cancel'} = this.props;
return (
<Dialog
open
onClose={ this.resolve }
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">{ message }</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
{ description }
</DialogContentText>
</DialogContent>
<DialogActions>
<Button onClick={ this.resolve } color="primary">
{ confirmLabel }
</Button>
<Button onClick={ this.reject } color="primary" autoFocus>
{ abortLabel }
</Button>
</DialogActions>
</Dialog>
);
}
}
export default (message, options = {}) => {
const props: any = {message, ...options};
const wrapper = document.body.appendChild(document.createElement('div'));
const component = ReactDOM.render(React.createElement(Confirm, props), wrapper);
const cleanup = () => {
ReactDOM.unmountComponentAtNode(wrapper);
setTimeout(() => wrapper.remove());
};
return component.promise.with(cleanup);
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment