1
0
mirror of https://github.com/ascribe/onion.git synced 2024-11-15 09:35:10 +01:00
onion/js/components/ascribe_modal/modal_wrapper.js

78 lines
2.0 KiB
JavaScript
Raw Normal View History

'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({
propTypes: {
trigger: React.PropTypes.element.isRequired,
title: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
React.PropTypes.element,
React.PropTypes.string
]).isRequired,
handleSuccess: React.PropTypes.func.isRequired,
children: React.PropTypes.oneOfType([
React.PropTypes.arrayOf(React.PropTypes.element),
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
handleSuccess(response){
this.props.handleSuccess(response);
this.hide();
2015-06-02 11:38:18 +02:00
},
2015-06-01 18:59:47 +02:00
renderChildren() {
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-01 18:59:47 +02:00
render() {
2015-08-05 18:00:44 +02:00
// this adds the onClick method show of modal_wrapper to the trigger component
// which is in most cases a button.
let trigger = React.cloneElement(this.props.trigger, {onClick: this.show});
2015-06-01 18:59:47 +02:00
return (
<span>
{trigger}
<Modal show={this.state.showModal} onHide={this.hide}>
<Modal.Header closeButton>
<Modal.Title>
{this.props.title}
</Modal.Title>
</Modal.Header>
<div className="modal-body">
{this.renderChildren()}
</div>
</Modal>
</span>
);
2015-06-01 18:59:47 +02:00
}
});
export default ModalWrapper;