Vulcan/packages/nova-core/lib/components/ModalButton.jsx

82 lines
1.9 KiB
React
Raw Normal View History

import React, { PropTypes, Component } from 'react';
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%)'
}
};
class ModalButton extends Component {
2016-02-25 21:05:53 +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
openModal(event) {
2016-02-25 21:05:53 +09:00
event.preventDefault();
this.setState({modalIsOpen: true});
}
2016-02-25 21:05:53 +09:00
closeModal(event) {
event.preventDefault();
2016-02-25 21:05:53 +09:00
this.setState({modalIsOpen: false});
}
2016-02-25 21:05:53 +09:00
render() {
const Component = this.props.component;
2016-02-25 21:32:13 +09:00
// see http://stackoverflow.com/a/32371612/649299
const childrenWithProps = React.Children.map(this.props.children, (child) => {
// if child component already has a callback, create new callback
// that both calls original callback and also closes modal
let callback;
if (child.props.callback) {
callback = (document) => {
child.props.callback(document);
this.closeModal();
}
} else {
callback = this.closeModal;
}
return React.cloneElement(child, { successCallback: callback });
2016-02-25 21:32:13 +09:00
});
2016-02-25 21:05:53 +09:00
return (
<div className="new-post-button">
<button onClick={this.openModal} className={this.props.className}>{this.props.label}</button>
<Modal
isOpen={this.state.modalIsOpen}
onRequestClose={this.closeModal}
style={customStyles} >
2016-02-25 21:32:13 +09:00
{childrenWithProps}
2016-02-25 21:05:53 +09:00
</Modal>
</div>
)
}
};
ModalButton.propTypes = {
label: React.PropTypes.string.isRequired,
className: React.PropTypes.string
}
2016-02-25 21:05:53 +09:00
module.exports = ModalButton;
export default ModalButton;