2017-07-15 06:26:43 +00:00
|
|
|
import closeIcon from 'assets/images/icon-x.svg';
|
2017-09-25 02:06:28 +00:00
|
|
|
import React, { Component } from 'react';
|
2017-07-15 06:26:43 +00:00
|
|
|
import './Modal.scss';
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export interface IButton {
|
|
|
|
text: string;
|
|
|
|
type?:
|
|
|
|
| 'default'
|
|
|
|
| 'primary'
|
|
|
|
| 'success'
|
|
|
|
| 'info'
|
|
|
|
| 'warning'
|
|
|
|
| 'danger'
|
|
|
|
| 'link';
|
|
|
|
disabled?: boolean;
|
|
|
|
onClick?(): void;
|
|
|
|
}
|
|
|
|
interface Props {
|
|
|
|
isOpen?: boolean;
|
|
|
|
title: string;
|
|
|
|
disableButtons?: boolean;
|
|
|
|
children: any;
|
|
|
|
buttons: IButton[];
|
|
|
|
handleClose(): void;
|
|
|
|
}
|
2017-07-15 06:26:43 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export default class Modal extends Component<Props, {}> {
|
|
|
|
public componentDidMount() {
|
2017-07-15 06:26:43 +00:00
|
|
|
this.updateBodyClass();
|
2017-09-25 02:06:28 +00:00
|
|
|
document.addEventListener('keydown', this.escapeListner);
|
2017-07-15 06:26:43 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public componentDidUpdate() {
|
2017-07-15 06:26:43 +00:00
|
|
|
this.updateBodyClass();
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public updateBodyClass() {
|
2017-07-15 06:26:43 +00:00
|
|
|
document.body.classList.toggle('no-scroll', !!this.props.isOpen);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public componentWillUnmount() {
|
|
|
|
document.removeEventListener('keydown', this.escapeListner);
|
2017-07-15 06:26:43 +00:00
|
|
|
document.body.classList.remove('no-scroll');
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
|
|
|
const { isOpen, title, children, buttons, handleClose } = this.props;
|
|
|
|
const hasButtons = buttons && buttons.length;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div className={`Modalshade ${isOpen ? 'is-open' : ''}`} />
|
|
|
|
<div className={`Modal ${isOpen ? 'is-open' : ''}`}>
|
|
|
|
<div className="Modal-header">
|
2017-10-05 19:22:41 +00:00
|
|
|
<h2 className="Modal-header-title">{title}</h2>
|
2017-09-25 02:06:28 +00:00
|
|
|
<button className="Modal-header-close" onClick={handleClose}>
|
|
|
|
<img className="Modal-header-close-icon" src={closeIcon} />
|
|
|
|
</button>
|
|
|
|
</div>
|
2017-10-05 19:22:41 +00:00
|
|
|
<div className="Modal-content">{isOpen && children}</div>
|
|
|
|
{hasButtons && (
|
|
|
|
<div className="Modal-footer">{this.renderButtons()}</div>
|
|
|
|
)}
|
2017-09-25 02:06:28 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
private escapeListner = (ev: KeyboardEvent) => {
|
2017-07-15 06:26:43 +00:00
|
|
|
// Don't trigger if they hit escape while on an input
|
|
|
|
if (ev.target) {
|
|
|
|
if (
|
2017-09-25 02:06:28 +00:00
|
|
|
(ev.target as HTMLElement).tagName === 'INPUT' ||
|
|
|
|
(ev.target as HTMLElement).tagName === 'SELECT' ||
|
|
|
|
(ev.target as HTMLElement).tagName === 'TEXTAREA' ||
|
|
|
|
(ev.target as HTMLElement).isContentEditable
|
2017-07-15 06:26:43 +00:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.key === 'Escape' || ev.keyCode === 27) {
|
|
|
|
this.props.handleClose();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
private renderButtons = () => {
|
2017-08-31 04:00:31 +00:00
|
|
|
const { disableButtons, buttons } = this.props;
|
|
|
|
|
|
|
|
return buttons.map((btn, idx) => {
|
2017-07-15 06:26:43 +00:00
|
|
|
let btnClass = 'Modal-footer-btn btn';
|
|
|
|
|
|
|
|
if (btn.type) {
|
|
|
|
btnClass += ` btn-${btn.type}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-08-23 06:57:18 +00:00
|
|
|
<button
|
|
|
|
className={btnClass}
|
|
|
|
onClick={btn.onClick}
|
|
|
|
key={idx}
|
2017-08-31 04:00:31 +00:00
|
|
|
disabled={disableButtons || btn.disabled}
|
2017-08-23 06:57:18 +00:00
|
|
|
>
|
2017-07-15 06:26:43 +00:00
|
|
|
{btn.text}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
});
|
2017-08-31 04:00:31 +00:00
|
|
|
};
|
2017-07-15 06:26:43 +00:00
|
|
|
}
|