confirmAlert.tsx 2.73 KB
Newer Older
Maxym Borodenko's avatar
Maxym Borodenko committed
1 2
import * as React from 'react';
import * as ReactDOM from 'react-dom';
Matija Obreza's avatar
Matija Obreza committed
3 4 5 6 7 8 9
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';

Maxym Borodenko's avatar
Maxym Borodenko committed
10 11

interface IConfirmProps extends React.ClassAttributes<any> {
12 13
    message?: string;
    description?: string;
Maxym Borodenko's avatar
Maxym Borodenko committed
14 15
    confirmLabel?: string;
    abortLabel?: string;
16
    confirmOnly?: boolean;
Maxym Borodenko's avatar
Maxym Borodenko committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
}

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() {
45
        const {message, description, confirmLabel = 'OK', abortLabel = 'Cancel', confirmOnly = false} = this.props;
Maxym Borodenko's avatar
Maxym Borodenko committed
46 47 48 49 50 51 52 53 54

        return (
            <Dialog
                open
                onClose={ this.resolve }
                aria-labelledby="alert-dialog-title"
                aria-describedby="alert-dialog-description"
            >
                <DialogTitle id="alert-dialog-title">{ message }</DialogTitle>
55 56 57 58 59 60 61
                { description && (
                    <DialogContent>
                        <DialogContentText id="alert-dialog-description">
                            { description }
                        </DialogContentText>
                    </DialogContent>
                ) }
Maxym Borodenko's avatar
Maxym Borodenko committed
62
                <DialogActions>
63 64 65 66 67 68
                    { ! confirmOnly && (
                        <Button onClick={ this.reject } autoFocus>
                            { abortLabel }
                        </Button>
                    ) }
                    <Button onClick={ this.resolve } color="primary" autoFocus={ confirmOnly }>
Maxym Borodenko's avatar
Maxym Borodenko committed
69 70 71 72 73 74 75 76 77
                      { confirmLabel }
                    </Button>
                </DialogActions>
            </Dialog>
        );
    }

}

78
export default (message: any, options: IConfirmProps = {}) => {
Maxym Borodenko's avatar
Maxym Borodenko committed
79 80 81 82 83 84 85 86 87 88 89
    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);
};