简体   繁体   English

使用React的模式

[英]Modals with React

I haven't managed to wrap my head around how to make use of existing modal libraries with React. 我还没想好如何在React中利用现有的模式库。 For reference, I'm using the awesome remodal . 作为参考,我使用了很棒的remodal

component.js.jsx component.js.jsx

openNewModal: function () {
  // The OpenModal component is wrapped around the modal's class which keeps it hidden until we show it here.
  var modal = $(ReactDOM.findDOMNode(this.refs.modal)).remodal();
  modal.open();
}

render: function () {
  return(
    <div onClick={this.openNewModal}>
      <OpenModal ref="modal" />
    </div>
  );
}

modal.js.jsx modal.js.jsx

handleSubmit: function(e) {
  e.preventDefault();
},

render: function () {
  return(
    <form onSubmit={this.handleSubmit}>
      ...
    </form>
  );
}

The modal opens up just fine. 模态打开就好了。 However, the binding on the onSubmit doesn't work. 但是, onSubmit上的绑定无效。 At first, I thought I'm doing something wrong. 起初,我以为我做错了什么。 I added an onClick handler somewhere in the modal.js.jsx but still nothing would fire. 我在modal.js.jsx中的某个地方添加了onClick处理程序,但是仍然不会触发任何操作。

The interesting part is that, if I executed the binding immediately it worked, as in <form onSubmit={this.handleSubmit()}> . 有趣的是,如果我立即执行绑定,它就会起作用,如<form onSubmit={this.handleSubmit()}> Which means that React fine up to the point that I actually open() the modal. 这意味着React可以达到我实际上open()模态的程度。

Is there a simple solution/example for modals and React? 模态和React是否有简单的解决方案/示例?

The interesting part is that, if I executed the binding immediately it worked, as in . 有趣的是,如果我立即执行绑定,则它会起作用,如中所示。 Which means that React fine up to the point that I actually open() the modal. 这意味着React可以达到我实际上打开()模态的程度。

This happened because you explicitly added the click event once the modal actually placed in DOM. 发生这种情况的原因是,将模式实际放置在DOM中后,您显式添加了click事件。 I think the solution could be in this way. 我认为解决方案可以是这种方式。 Please add your modal mockup in the component.js.jsx like - 请将您的模式样机添加到component.js.jsx中,例如-

render: function () {
  return(
    <div onClick={this.openNewModal}>
      <div class="remodal" data-remodal-id="modal">
  <button data-remodal-action="close" class="remodal-close"></button>
  <h1>Remodal</h1>
  <p>
    Responsive, lightweight, fast, synchronized with CSS animations, fully customizable modal window plugin with declarative configuration and hash tracking.
  </p>
  <br>
  <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
  <button data-remodal-action="confirm" class="remodal-confirm" onSubmit={this.handleSubmit}>OK</button>
</div>
    </div>
  );
}

Then hide the modal mockup with CSS(if it is not hided bydefault by remodaljs). 然后用CSS隐藏模式样机(如果remodaljs默认不隐藏它)。 Then add your event(handleSubmit) to the ok button or to the form if modal is opened in form. 然后将您的事件(handleSubmit)添加到“确定”按钮或表单中,如果已在表单中打开模式。

In openNewModal function instead of remodal the modal just open the modal. 在openNewModal函数而不是重新模态中,模态只是打开模态。

Modal mockup is copied from remodal example. 模态模型是从重模示例中复制的。 Do replace with your requirement.To be true i didn't have knowledge on remodal but i do faced the same problem while using the bootstrap modal. 不要用您的要求代替。说实话,我不了解重整模态,但在使用引导模态时确实遇到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM