I need a dialog in my react project, but I can't find a good way to implement it. I had searched other implementations, such as react-modal-dialog, react-modal.
but I don't think they do the best way, the most confusing part is the encapsulation.
Take react-modal as the example:
var App = React.createClass({
getInitialState: function() {
return { modalIsOpen: false };
},
openModal: function() {
this.setState({modalIsOpen: true});
},
closeModal: function() {
this.setState({modalIsOpen: false});
},
render: function() {
return (
<div>
<button onClick={this.openModal}>Open Modal</button>
<Modal
isOpen={this.state.modalIsOpen}
onRequestClose={this.closeModal}
style={customStyles} >
<h2>Hello</h2>
<button onClick={this.closeModal}>close</button>
<div>I am a modal</div>
<form>
<input />
<button>tab navigation</button>
<button>stays</button>
<button>inside</button>
<button>the modal</button>
</form>
</Modal>
</div>
);
}
});
The codes above show the way to use Modal, but you can find that, we have to define the onRequestClose by ourselves every time. I know the reason is, that Modal can be closed by itself or its parent view.
But I want to know, is it better to call Modal.close() in parent view (because of the encapsulation)? If so, could you give me more details of implementation?