import React, { PureComponent } from 'react'; import ReactDOM from 'react-dom'; import classNames from 'classnames'; const KEYBOARD_ENTER_KEY = 13; const KEYBOARD_ESCAPE_KEY = 27; interface ModalProps { isOpen?: boolean; withBackdrop?: boolean; onSubmit: (data?: AckProblemData) => Promise | any; onClose?: () => void; } interface ModalState { value: string; error: boolean; message: string; } export interface AckProblemData { message: string; closeProblem?: boolean; action?: number; } export class Modal extends PureComponent { modalContainer: HTMLElement; constructor(props) { super(props); this.state = { value: '', error: false, message: '', }; this.modalContainer = document.body; } handleChange = (event: React.ChangeEvent) => { this.setState({ value: event.target.value, error: false }); } handleKeyUp = (event: React.KeyboardEvent) => { if (event.which === KEYBOARD_ENTER_KEY || event.key === 'Enter') { this.submit(); } else if (event.which === KEYBOARD_ESCAPE_KEY || event.key === 'Escape') { this.dismiss(); } } handleBackdropClick = () => { this.dismiss(); } dismiss = () => { this.setState({ value: '', error: false, message: '' }); this.props.onClose(); } submit = () => { if (!this.state.value) { return this.setState({ error: true, message: 'Enter message text' }); } this.props.onSubmit({ message: this.state.value }).then(() => { this.dismiss(); }); } render() { if (!this.props.isOpen || !this.modalContainer) { return null; } const inputClass = classNames('gf-form-input', { 'zbx-ack-error': this.state.error }); const modalNode = (

Acknowledge Problem

); const modalNodeWithBackdrop = [ modalNode,
]; const modal = this.props.withBackdrop ? modalNodeWithBackdrop : modalNode; return ReactDOM.createPortal(modal, this.modalContainer); } }