insomnia/app/components/PromptModal.js

92 lines
2.2 KiB
JavaScript
Raw Normal View History

import React from 'react';
import Modal from './base/Modal';
import ModalBody from './base/ModalBody';
import ModalHeader from './base/ModalHeader';
import ModalFooter from './base/ModalFooter';
import ModalComponent from './lib/ModalComponent';
2016-07-14 22:48:56 +00:00
class PromptModal extends ModalComponent {
constructor (props) {
super(props);
this.state = {
headerName: 'Not Set',
defaultValue: '',
submitName: 'Not Set',
2016-07-19 20:21:33 +00:00
selectText: false,
hint: null
2016-07-14 22:48:56 +00:00
};
}
_onSubmit (e) {
e.preventDefault();
this._onSubmitCallback && this._onSubmitCallback(this.refs.input.value);
this.hide();
}
_setDefaultValueFromState () {
if (this.state.defaultValue) {
this.refs.input.value = this.state.defaultValue;
}
this.refs.input.focus();
if (this.state.selectText) {
this.refs.input.select();
}
}
2016-07-19 20:21:33 +00:00
show ({headerName, defaultValue, submitName, selectText, hint}) {
2016-07-14 22:48:56 +00:00
super.show();
return new Promise(resolve => {
this._onSubmitCallback = resolve;
this.setState({
headerName,
defaultValue,
submitName,
2016-07-19 20:21:33 +00:00
selectText,
hint
2016-07-14 22:48:56 +00:00
})
});
}
componentDidUpdate () {
this._setDefaultValueFromState();
}
render () {
const {extraProps} = this.props;
2016-07-19 20:21:33 +00:00
const {submitName, headerName, hint} = this.state;
2016-07-14 22:48:56 +00:00
return (
<Modal ref="modal" {...extraProps}>
<ModalHeader>{headerName}</ModalHeader>
<ModalBody className="wide">
<form onSubmit={e => this._onSubmit(e)} className="wide pad">
<div className="form-control form-control--outlined form-control--wide">
<input ref="input" type="text"/>
</div>
</form>
</ModalBody>
<ModalFooter>
<div className="pull-right">
2016-07-19 20:21:33 +00:00
<button className="btn" onClick={() => this.hide()}>
Cancel
</button>
2016-07-14 22:48:56 +00:00
<button className="btn" onClick={this._onSubmit.bind(this)}>
{submitName || 'Save'}
</button>
</div>
2016-07-19 20:21:33 +00:00
<div className="pad faint italic txt-sm tall">{hint ? `* ${hint}` : ''}</div>
2016-07-14 22:48:56 +00:00
</ModalFooter>
</Modal>
)
}
}
PromptModal.propTypes = {};
export default PromptModal;