Files
SCM-Manager/scm-ui/ui-components/src/modals/ConfirmAlert.tsx

62 lines
1.4 KiB
TypeScript
Raw Normal View History

import * as React from "react";
import ReactDOM from "react-dom";
import Modal from "./Modal";
2019-10-29 09:49:04 +01:00
import classNames from "classnames";
2019-02-01 11:09:07 +01:00
type Button = {
2019-10-29 09:49:04 +01:00
className?: string;
label: string;
onClick: () => void | null;
};
type Props = {
title: string;
message: string;
buttons: Button[];
};
class ConfirmAlert extends React.Component<Props> {
2019-02-01 11:09:07 +01:00
handleClickButton = (button: Button) => {
if (button.onClick) {
button.onClick();
}
this.close();
};
close = () => {
const container = document.getElementById("modalRoot");
if (container) {
ReactDOM.unmountComponentAtNode(container);
}
};
render() {
const { title, message, buttons } = this.props;
2019-02-07 13:53:15 +01:00
const body = <>{message}</>;
const footer = (
<div className="field is-grouped">
{buttons.map((button, i) => (
<p className="control">
2019-10-29 09:49:04 +01:00
<a className={classNames("button", "is-info", button.className)} key={i} onClick={() => this.handleClickButton(button)}>
2019-02-01 10:02:19 +01:00
{button.label}
</a>
2019-02-07 13:53:15 +01:00
</p>
))}
</div>
2019-02-01 10:02:19 +01:00
);
2019-01-31 10:50:15 +01:00
2019-10-21 10:57:56 +02:00
return <Modal title={title} closeFunction={() => this.close()} body={body} active={true} footer={footer} />;
}
}
export function confirmAlert(properties: Props) {
const root = document.getElementById("modalRoot");
2019-02-07 13:53:15 +01:00
if (root) {
ReactDOM.render(<ConfirmAlert {...properties} />, root);
2019-02-01 08:47:38 +01:00
}
}
export default ConfirmAlert;