import React from 'react'; import PropTypes from 'prop-types'; import {get} from 'lodash'; import {gettext} from '../../utils'; import {Modal} from '../index'; import {Button} from '../UI'; import './style.scss'; import {KEYCODES} from '../../constants'; export class ModalWithForm extends React.Component { constructor(props) { super(props); this.state = {submitting: false}; this.submit = this.submit.bind(this); this.onHide = this.onHide.bind(this); this.dom = {form: null}; this.handleKeydown = this.handleKeydown.bind(this); } componentDidMount() { document.addEventListener('keydown', this.handleKeydown); } componentWillUnmount() { document.removeEventListener('keydown', this.handleKeydown); } handleKeydown(event) { if (event.keyCode === KEYCODES.ESCAPE) { event.preventDefault(); this.onHide(); } } getFormInstance() { return this.dom.form; } submit() { this.setState({submitting: true}); // Call the submit method of the form Component this.getFormInstance() .submit() .then( this.props.onHide, () => { this.props.enableSaveInModal(); this.setState({submitting: false}); } ); } onHide() { const form = this.getFormInstance(); // If the form Component has an 'onHide' property, // then call that now if ('onHide' in form.props) { form.props.onHide( this.props.original, get(this.props, 'modalProps') || {} ); } this.props.onHide(); } render() { const { show, onHide, large, fill, fullscreen, white, title, initialValues, original, updates, enableSaveInModal, disableSaveInModal, cancelButtonText, canSave, saveButtonText, form, modalProps, resolve, } = this.props; const Form = form; return (

{title}

this.dom.form = node} modalProps={modalProps} resolve={resolve} />