import React, {PureComponent} from 'react'; import autobind from 'autobind-decorator'; import Modal from '../base/Modal'; import ModalBody from '../base/ModalBody'; import ModalHeader from '../base/ModalHeader'; import ModalFooter from '../base/ModalFooter'; @autobind class PromptModal extends PureComponent { constructor (props) { super(props); this.state = { headerName: 'Not Set', defaultValue: '', submitName: 'Not Set', selectText: false, hint: null, inputType: 'text' }; } _setInputRef (n) { this._input = n; } _setModalRef (n) { this.modal = n; } _handleSubmit (e) { e.preventDefault(); this._onSubmitCallback && this._onSubmitCallback(this._input.value); this.modal.hide(); } show ({headerName, defaultValue, submitName, selectText, hint, inputType, placeholder, label}) { this.modal.show(); // Need to do this after render because modal focuses itself too setTimeout(() => { this._input.value = defaultValue || ''; this._input.focus(); selectText && this._input.select(); }, 100); return new Promise(resolve => { this._onSubmitCallback = resolve; this.setState({ headerName, defaultValue, submitName, selectText, placeholder, hint, inputType, label }); }); } render () { const {extraProps} = this.props; const {submitName, headerName, hint, inputType, placeholder, label} = this.state; const input = ( ); return ( {headerName} {label ? {label}{input} : input} {hint ? `* ${hint}` : ''} {submitName || 'Submit'} ); } } PromptModal.propTypes = {}; export default PromptModal;