1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00
onion/js/components/ascribe_modal/modal_wrapper.js

111 lines
3.0 KiB
JavaScript
Raw Normal View History

'use strict';
2015-06-01 18:59:47 +02:00
import React from 'react';
import classNames from 'classnames';
2015-06-01 18:59:47 +02:00
import Modal from 'react-bootstrap/lib/Modal';
let ModalWrapper = React.createClass({
propTypes: {
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element
]).isRequired,
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
bodyClassNames: React.PropTypes.string,
2016-02-05 10:38:59 +01:00
handleCancel: React.PropTypes.func,
handleSuccess: React.PropTypes.func,
trigger: React.PropTypes.element
},
2015-06-01 18:59:47 +02:00
getInitialState() {
return {
showModal: false
};
},
show() {
this.setState({
showModal: true
});
},
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();
},
handleSuccess(response) {
2016-02-05 10:38:59 +01:00
if (typeof this.props.handleSuccess === 'function') {
this.props.handleSuccess(response);
}
this.hide();
2015-06-02 11:38:18 +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, {
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-01 18:59:47 +02:00
render() {
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.
// 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 (
<span>
{clonedTrigger}
<Modal
onHide={this.handleCancel}
show={this.state.showModal}>
2015-10-14 12:16:49 +02:00
<Modal.Header closeButton>
<Modal.Title>
{title}
</Modal.Title>
</Modal.Header>
<div className={classNames('modal-body', bodyClassNames)}>
{this.renderChildren()}
</div>
</Modal>
</span>
);
2015-06-01 18:59:47 +02:00
}
});
export default ModalWrapper;