import React, {PureComponent} from 'react'; import autobind from 'autobind-decorator'; import classnames from 'classnames'; import Modal from '../base/modal'; import ModalBody from '../base/modal-body'; import ModalHeader from '../base/modal-header'; import ModalFooter from '../base/modal-footer'; import Button from '../base/button'; import PromptButton from '../base/prompt-button'; @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._onComplete && this._onComplete(value); this.hide(); } _setInputRef (n) { this._input = n; } _setModalRef (n) { this.modal = n; } _handleSelectHint (hint) { this._done(hint); } _handleDeleteHint (hint) { this._onDeleteHint && this._onDeleteHint(hint); const hints = this.state.hints.filter(h => h !== hint); this.setState({hints}); } _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, onComplete, onDeleteHint } = 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); this._onComplete = onComplete; this._onDeleteHint = onDeleteHint; this.setState({ headerName, defaultValue, submitName, selectText, placeholder, upperCase, hint, inputType, label, hints }); } _renderHintButton (hint) { const classes = classnames( 'btn btn--outlined btn--super-duper-compact', 'margin-right-sm margin-top-sm inline-block' ); return (
); } 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 ? : input}
{sanitizedHints}
{hint ? `* ${hint}` : ''}
); } } PromptModal.propTypes = {}; export default PromptModal;