insomnia/app/components/base/PromptModal.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-04-15 05:23:54 +00:00
import React, {Component, PropTypes} from 'react'
import Modal from './Modal'
import ModalBody from './ModalBody'
import ModalHeader from './ModalHeader'
import ModalFooter from './ModalFooter'
2016-04-07 17:06:04 +00:00
class PromptModal extends Component {
2016-04-08 04:05:08 +00:00
_onSubmit (e) {
e.preventDefault();
this.props.onSubmit(this.refs.input.value);
this.refs.modal.close();
}
2016-04-09 19:24:33 +00:00
_setDefaultValueFromProps () {
if (this.props.defaultValue) {
this.refs.input.value = this.props.defaultValue;
}
2016-04-09 21:41:27 +00:00
this.refs.input.focus();
2016-04-12 06:10:46 +00:00
if (this.props.selectText) {
this.refs.input.select();
}
2016-04-09 19:24:33 +00:00
}
componentDidMount () {
this._setDefaultValueFromProps();
}
componentDidUpdate () {
this._setDefaultValueFromProps();
}
2016-04-07 17:06:04 +00:00
render () {
2016-04-15 05:23:54 +00:00
const {onClose, submitName, headerName, ...extraProps} = this.props;
2016-04-07 17:06:04 +00:00
return (
2016-04-15 05:23:54 +00:00
<Modal ref="modal" onClose={onClose} {...extraProps}>
2016-04-08 04:05:08 +00:00
<ModalHeader>{headerName}</ModalHeader>
2016-04-09 01:14:25 +00:00
<ModalBody className="wide">
2016-04-12 06:10:46 +00:00
<form onSubmit={this._onSubmit.bind(this)} className="wide pad">
<div className="form-control form-control--outlined form-control--wide">
2016-04-09 21:41:27 +00:00
<input ref="input" type="text"/>
2016-04-09 01:14:25 +00:00
</div>
2016-04-08 04:05:08 +00:00
</form>
</ModalBody>
<ModalFooter className="grid grid--end">
2016-04-09 01:14:25 +00:00
<button className="btn" onClick={() => this.refs.modal.close()}>Cancel</button>
2016-04-08 04:05:08 +00:00
<button className="btn" onClick={this._onSubmit.bind(this)}>
{submitName || 'Save'}
</button>
</ModalFooter>
2016-04-07 17:06:04 +00:00
</Modal>
)
}
}
PromptModal.propTypes = {
onSubmit: PropTypes.func.isRequired,
2016-04-08 04:05:08 +00:00
headerName: PropTypes.string.isRequired,
2016-04-09 19:24:33 +00:00
defaultValue: PropTypes.string,
2016-04-08 04:05:08 +00:00
submitName: PropTypes.string,
2016-04-12 06:10:46 +00:00
selectText: PropTypes.bool,
2016-04-07 17:06:04 +00:00
onClose: PropTypes.func
};
export default PromptModal;