2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-01 18:59:47 +02:00
|
|
|
import React from 'react';
|
|
|
|
import ReactAddons from 'react/addons';
|
|
|
|
|
|
|
|
import Modal from 'react-bootstrap/lib/Modal';
|
|
|
|
|
|
|
|
let ModalWrapper = React.createClass({
|
2015-06-09 16:10:38 +02:00
|
|
|
propTypes: {
|
2015-10-29 16:23:02 +01:00
|
|
|
trigger: React.PropTypes.element,
|
2015-08-05 17:32:35 +02:00
|
|
|
title: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.arrayOf(React.PropTypes.element),
|
|
|
|
React.PropTypes.element,
|
|
|
|
React.PropTypes.string
|
|
|
|
]).isRequired,
|
2015-06-09 16:10:38 +02:00
|
|
|
handleSuccess: React.PropTypes.func.isRequired,
|
2015-08-05 17:32:35 +02:00
|
|
|
children: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.arrayOf(React.PropTypes.element),
|
|
|
|
React.PropTypes.element
|
2015-10-20 15:10:18 +02:00
|
|
|
])
|
2015-06-09 16:10:38 +02:00
|
|
|
},
|
2015-06-01 18:59:47 +02:00
|
|
|
|
2015-08-05 17:32:35 +02:00
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
showModal: false
|
|
|
|
};
|
2015-07-14 16:29:01 +02:00
|
|
|
},
|
|
|
|
|
2015-08-05 17:32:35 +02:00
|
|
|
show() {
|
|
|
|
this.setState({
|
|
|
|
showModal: true
|
|
|
|
});
|
2015-06-09 16:10:38 +02:00
|
|
|
},
|
2015-06-05 11:06:36 +02:00
|
|
|
|
2015-08-05 17:32:35 +02:00
|
|
|
hide() {
|
|
|
|
this.setState({
|
|
|
|
showModal: false
|
|
|
|
});
|
|
|
|
},
|
2015-06-01 18:59:47 +02:00
|
|
|
|
2015-10-29 16:23:02 +01:00
|
|
|
handleSuccess(response) {
|
2015-06-09 16:10:38 +02:00
|
|
|
this.props.handleSuccess(response);
|
2015-08-05 17:32:35 +02:00
|
|
|
this.hide();
|
2015-06-02 11:38:18 +02:00
|
|
|
},
|
2015-06-05 11:06:36 +02:00
|
|
|
|
2015-06-01 18:59:47 +02:00
|
|
|
renderChildren() {
|
2015-06-05 11:06:36 +02:00
|
|
|
return ReactAddons.Children.map(this.props.children, (child) => {
|
2015-06-01 18:59:47 +02:00
|
|
|
return ReactAddons.addons.cloneWithProps(child, {
|
2015-06-02 11:38:18 +02:00
|
|
|
handleSuccess: this.handleSuccess
|
2015-06-01 18:59:47 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-06-05 11:06:36 +02:00
|
|
|
|
2015-06-01 18:59:47 +02:00
|
|
|
render() {
|
2015-10-29 16:23:02 +01:00
|
|
|
const { trigger, title } = this.props;
|
2015-10-20 15:10:18 +02:00
|
|
|
|
2015-10-29 16:23:02 +01:00
|
|
|
// If the trigger component exists, we add the ModalWrapper's show() as its onClick method.
|
|
|
|
// The trigger component should, in most cases, be a button.
|
2015-10-30 11:13:32 +01:00
|
|
|
const clonedTrigger = React.isValidElement(trigger) ? React.cloneElement(trigger, {onClick: this.show})
|
|
|
|
: null;
|
2015-06-01 18:59:47 +02:00
|
|
|
return (
|
2015-08-05 17:32:35 +02:00
|
|
|
<span>
|
2015-10-29 16:23:02 +01:00
|
|
|
{clonedTrigger}
|
2015-08-05 17:32:35 +02:00
|
|
|
<Modal show={this.state.showModal} onHide={this.hide}>
|
2015-10-14 12:16:49 +02:00
|
|
|
<Modal.Header closeButton>
|
2015-08-05 17:32:35 +02:00
|
|
|
<Modal.Title>
|
2015-10-29 16:23:02 +01:00
|
|
|
{title}
|
2015-08-05 17:32:35 +02:00
|
|
|
</Modal.Title>
|
|
|
|
</Modal.Header>
|
2015-10-14 12:16:49 +02:00
|
|
|
<div className="modal-body" >
|
2015-08-05 17:32:35 +02:00
|
|
|
{this.renderChildren()}
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
</span>
|
2015-06-05 11:06:36 +02:00
|
|
|
);
|
2015-06-01 18:59:47 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default ModalWrapper;
|