2015-06-05 11:06:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
2015-06-01 18:59:47 +02:00
|
|
|
import React from 'react';
|
2016-02-09 12:07:09 +01:00
|
|
|
import classNames from 'classnames';
|
2015-06-01 18:59:47 +02:00
|
|
|
|
|
|
|
import Modal from 'react-bootstrap/lib/Modal';
|
|
|
|
|
|
|
|
let ModalWrapper = React.createClass({
|
2015-06-09 16:10:38 +02:00
|
|
|
propTypes: {
|
2016-02-05 11:53:33 +01:00
|
|
|
children: React.PropTypes.oneOfType([
|
|
|
|
React.PropTypes.arrayOf(React.PropTypes.element),
|
|
|
|
React.PropTypes.element
|
|
|
|
]).isRequired,
|
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,
|
2016-02-05 10:38:59 +01:00
|
|
|
|
2016-02-09 12:07:09 +01:00
|
|
|
bodyClassNames: React.PropTypes.string,
|
2016-02-05 10:38:59 +01:00
|
|
|
handleCancel: React.PropTypes.func,
|
|
|
|
handleSuccess: React.PropTypes.func,
|
2016-02-05 11:53:33 +01:00
|
|
|
trigger: React.PropTypes.element
|
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
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
handleCancel() {
|
|
|
|
if (typeof this.props.handleCancel === 'function') {
|
|
|
|
this.props.handleCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hide();
|
|
|
|
},
|
|
|
|
|
2015-10-29 16:23:02 +01:00
|
|
|
handleSuccess(response) {
|
2016-02-05 10:38:59 +01:00
|
|
|
if (typeof this.props.handleSuccess === 'function') {
|
|
|
|
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() {
|
2016-02-05 10:38:59 +01:00
|
|
|
return React.Children.map(this.props.children, (child) => {
|
|
|
|
return React.cloneElement(child, {
|
2015-12-18 14:57:10 +01:00
|
|
|
handleSuccess: (response) => {
|
|
|
|
if (typeof child.props.handleSuccess === 'function') {
|
|
|
|
child.props.handleSuccess(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.handleSuccess(response);
|
|
|
|
}
|
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() {
|
2016-02-09 12:07:09 +01:00
|
|
|
const { bodyClassNames, trigger, title } = this.props;
|
2015-10-20 15:10:18 +02:00
|
|
|
|
2016-02-05 10:38:59 +01:00
|
|
|
// If the trigger component exists, we add the ModalWrapper's show() to its onClick method.
|
2015-10-29 16:23:02 +01:00
|
|
|
// The trigger component should, in most cases, be a button.
|
2016-02-05 10:38:59 +01:00
|
|
|
const clonedTrigger = React.isValidElement(trigger) ?
|
|
|
|
React.cloneElement(trigger, {
|
|
|
|
onClick: (...params) => {
|
|
|
|
if (typeof trigger.props.onClick === 'function') {
|
|
|
|
trigger.props.onClick(...params);
|
|
|
|
}
|
|
|
|
|
|
|
|
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}
|
2016-02-09 12:07:09 +01:00
|
|
|
<Modal
|
|
|
|
onHide={this.handleCancel}
|
|
|
|
show={this.state.showModal}>
|
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>
|
2016-02-09 12:07:09 +01:00
|
|
|
<div className={classNames('modal-body', bodyClassNames)}>
|
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;
|