import React, {PureComponent} from 'react'; import autobind from 'autobind-decorator'; import Button from '../base/button'; import Modal from '../base/modal'; import ModalBody from '../base/modal-body'; import ModalHeader from '../base/modal-header'; import ModalFooter from '../base/modal-footer'; @autobind class PromptModal extends PureComponent { constructor (props) { super(props); this.state = { headerName: 'Not Set', defaultValue: '', submitName: 'Not Set', selectText: false, upperCase: false, hint: null, inputType: 'text', hints: [] }; } _done (rawValue) { const value = this.state.upperCase ? rawValue.toUpperCase() : rawValue; this._onSubmitCallback && this._onSubmitCallback(value); this.hide(); } _setInputRef (n) { this._input = n; } _setModalRef (n) { this.modal = n; } _handleSelectHint (hint) { this._done(hint); } _handleSubmit (e) { e.preventDefault(); this._done(this._input.value); } hide () { this.modal.hide(); } show (options) { const { headerName, defaultValue, submitName, selectText, upperCase, hint, inputType, placeholder, label, hints } = options; 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, upperCase, hint, inputType, label, hints }); }); } _renderHintButton (hint) { return ( {hint} ); } render () { const { submitName, headerName, hint, inputType, placeholder, label, upperCase, hints } = this.state; const input = ( ); let sanitizedHints = []; if (Array.isArray(hints)) { sanitizedHints = hints.slice(0, 15).map(this._renderHintButton); } return ( {headerName} {label ? {label}{input} : input} {sanitizedHints} {hint ? `* ${hint}` : ''} {submitName || 'Submit'} ); } } PromptModal.propTypes = {}; export default PromptModal;