2016-03-17 18:08:03 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-03-24 10:56:47 +09:00
|
|
|
import { Button } from 'react-bootstrap';
|
2016-02-25 21:05:53 +09:00
|
|
|
import Modal from 'react-modal';
|
|
|
|
|
|
|
|
const customStyles = {
|
|
|
|
content : {
|
|
|
|
top : '50%',
|
|
|
|
left : '50%',
|
|
|
|
right : 'auto',
|
|
|
|
bottom : 'auto',
|
|
|
|
marginRight : '-50%',
|
|
|
|
transform : 'translate(-50%, -50%)'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 10:56:47 +09:00
|
|
|
class ModalTrigger extends Component {
|
2016-02-25 21:05:53 +09:00
|
|
|
|
2016-03-17 18:08:03 +09:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.openModal = this.openModal.bind(this);
|
|
|
|
this.closeModal = this.closeModal.bind(this);
|
|
|
|
this.state = {
|
|
|
|
modalIsOpen: false
|
|
|
|
};
|
|
|
|
}
|
2016-02-25 21:05:53 +09:00
|
|
|
|
2016-03-18 10:50:40 +09:00
|
|
|
openModal() {
|
2016-02-25 21:05:53 +09:00
|
|
|
this.setState({modalIsOpen: true});
|
2016-03-17 18:08:03 +09:00
|
|
|
}
|
2016-02-25 21:05:53 +09:00
|
|
|
|
2016-03-18 10:50:40 +09:00
|
|
|
closeModal() {
|
2016-02-25 21:05:53 +09:00
|
|
|
this.setState({modalIsOpen: false});
|
2016-03-17 18:08:03 +09:00
|
|
|
}
|
2016-02-25 21:05:53 +09:00
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
|
2016-02-25 21:32:13 +09:00
|
|
|
// see http://stackoverflow.com/a/32371612/649299
|
|
|
|
const childrenWithProps = React.Children.map(this.props.children, (child) => {
|
2016-02-26 10:42:57 +09:00
|
|
|
|
2016-03-18 10:50:40 +09:00
|
|
|
// if child component already has a successCallback, create new callback
|
2016-02-26 10:42:57 +09:00
|
|
|
// that both calls original callback and also closes modal
|
2016-03-18 10:50:40 +09:00
|
|
|
let successCallback;
|
|
|
|
if (child.props.successCallback) {
|
|
|
|
successCallback = (document) => {
|
|
|
|
child.props.successCallback(document);
|
2016-02-26 10:42:57 +09:00
|
|
|
this.closeModal();
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-18 10:50:40 +09:00
|
|
|
successCallback = this.closeModal;
|
2016-02-26 10:42:57 +09:00
|
|
|
}
|
|
|
|
|
2016-03-18 10:50:40 +09:00
|
|
|
return React.cloneElement(child, { successCallback: successCallback });
|
2016-02-26 10:42:57 +09:00
|
|
|
|
2016-02-25 21:32:13 +09:00
|
|
|
});
|
|
|
|
|
2016-03-24 10:56:47 +09:00
|
|
|
const triggerComponent = React.cloneElement(this.props.component, { onClick: this.openModal });
|
|
|
|
|
2016-02-25 21:05:53 +09:00
|
|
|
return (
|
|
|
|
<div className="new-post-button">
|
2016-03-24 10:56:47 +09:00
|
|
|
{triggerComponent}
|
2016-02-25 21:05:53 +09:00
|
|
|
<Modal
|
|
|
|
isOpen={this.state.modalIsOpen}
|
|
|
|
onRequestClose={this.closeModal}
|
2016-03-18 10:50:40 +09:00
|
|
|
style={customStyles}
|
|
|
|
>
|
2016-02-25 21:32:13 +09:00
|
|
|
{childrenWithProps}
|
2016-02-25 21:05:53 +09:00
|
|
|
</Modal>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2016-03-17 18:08:03 +09:00
|
|
|
};
|
|
|
|
|
2016-03-24 10:56:47 +09:00
|
|
|
ModalTrigger.propTypes = {
|
|
|
|
component: React.PropTypes.object.isRequired
|
2016-03-17 18:08:03 +09:00
|
|
|
}
|
2016-02-25 21:05:53 +09:00
|
|
|
|
2016-03-24 10:56:47 +09:00
|
|
|
module.exports = ModalTrigger;
|
|
|
|
export default ModalTrigger;
|