insomnia/app/components/base/PromptModal.js

60 lines
1.6 KiB
JavaScript
Raw Normal View History

2016-04-07 17:06:04 +00:00
import React, {Component, PropTypes} from 'react';
import {Modal, ModalHeader, ModalBody, ModalFooter} from './Modal'
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;
}
}
componentDidMount () {
this._setDefaultValueFromProps();
}
componentDidUpdate () {
this._setDefaultValueFromProps();
}
2016-04-07 17:06:04 +00:00
render () {
2016-04-08 04:05:08 +00:00
const {visible, onClose, submitName, headerName} = this.props;
2016-04-07 17:06:04 +00:00
return (
2016-04-08 04:05:08 +00:00
<Modal ref="modal" onClose={onClose} visible={visible}>
<ModalHeader>{headerName}</ModalHeader>
2016-04-09 01:14:25 +00:00
<ModalBody className="wide">
<form onSubmit={this._onSubmit.bind(this)} className="wide">
<div className="form-control form-control--outlined">
2016-04-09 19:24:33 +00:00
<input ref="input" type="text" autoFocus={true}/>
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
visible: PropTypes.bool.isRequired,
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-07 17:06:04 +00:00
onClose: PropTypes.func
};
export default PromptModal;