显示React Bootstrap的触发模式

时间:2015-08-05 15:33:13

标签: reactjs react-bootstrap

http://react-bootstrap.github.io/components.html#modals - 我正在寻找一种方法来查找'shown.bs.modal'而不是'show.bs.modal'的触发事件。

我没有看到任何关于某些bootstrap模态事件的文档:http://getbootstrap.com/javascript/#modals-events

var NewPerson = React.createClass({

  getInitialState: function(){
    return { showModal: false };
  },

  componentDidMount: function() {
    // removed irrelevant code
  },

  close: function(){
    this.setState({ showModal: false });
  },

  open: function(){
    this.setState({ showModal: true });
  },

  submit: function(e) {
    // removed irrelevant code
  },
  render: function() {
    var Button = ReactBootstrap.Button;
    var ButtonInput = ReactBootstrap.ButtonInput;
    var Modal = ReactBootstrap.Modal;

    return (
      <div>
        <Button
          className="pull-right bottom-20"
          bsStyle='success'
          bsSize='large'
          onClick={this.open}
        >
          Create New Person
        </Button>

        <Modal id="new-person-modal" show={this.state.showModal} onHide={this.close}>
          <Modal.Header closeButton>
            <Modal.Title>Create a New Person!</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            <form id="new-person-form" onSubmit={this.submit} accept-charset="UTF-8" method="POST" novalidate="novalidate">
              <div className="row">
                <div className="form-group col-md-12">
                  <input type="text" className="form-control" id="author" name="author" ref="author" placeholder="Author (First + Last)" required />
                </div>
              </div>
              <ButtonInput
                type="submit"
                value="Submit"
                bsStyle="success"
                bsSize="large"
                onSubmit={this.submit}
                disabled={this.state.disabled} />
            </form>
          </Modal.Body>
        </Modal>
      </div>
    );
  }
});

React.render(<NewPerson url="/person" />, document.getElementById('person-new'));

我甚至试图通过在jQuery中执行此操作来破解它,这也无法正常工作。

<script type="text/javascript">
$(document).ready(function() {
  $('#new-person-modal').on('shown.bs.modal', function (e) {
    console.log('modal displayed');
    // now can execute dynamic JS
  });
});
</script>

知道如何触发'on shows'事件吗?我在react-bootstrap源代码中找不到任何东西。

3 个答案:

答案 0 :(得分:9)

有一种正确的方法:使用onEntered事件。

<Modal
  onEntered  = { function(){ console.log( "Modal is Shown" ) }}
> 

当显示模态的转换结束时,将调用此事件。

总共有6个转换触发器:

<Modal
  onExit     = { function(){ console.log( "onExit    " ) }}
  onExiting  = { function(){ console.log( "onExiting " ) }}
  onExited   = { function(){ console.log( "onExited  " ) }}

  onEnter    = { function(){ console.log( "onEnter   " ) }}
  onEntering = { function(){ console.log( "onEntering" ) }}
  onEntered  = { function(){ console.log( "onEntered " ) }}
>

这是他们的所作所为:

onEnter     = Callback fired before the Modal transitions in

onEntering  = Callback fired as the Modal begins to transition in    

onEntered   = Callback fired after the Modal finishes transitioning in


onExit      = Callback fired right before the Modal transitions out

onExiting   = Callback fired as the Modal begins to transition out

onExited    = Callback fired after the Modal finishes transitioning out

在撰写本文时,此解决方案未记录在案。我正在提交拉取请求,希望他们能尽快更新文档。

答案 1 :(得分:3)

最简单的方法是创建一个非可视化组件,它将在渲染时调用传递的函数,并在Modal.Body内部的某处呈现此组件:

render: function() {
    //...
    <Modal.Body>
      //...
      <Notifier onShown={this.onModalShown} />
      //...
    </Modal.Body>
    //...
}

更新: 这就是Notifier组件的实现方式:

var Notifier = React.createClass({
    componentDidMount: function(){
        this.props.onShown();
    },
    render: function() {
        return null;
    }
});

答案 2 :(得分:0)

我也有同样的问题,这就是我试过的......

Class Modal extends React.Component {

        showModal(){
        let obj = this.refs.modal;
        //write the required code here
      }
render() {
    return (
      <div>
        <div className="standard-content">
              <p><a className="btn" onClick={this.showModal}  data-toggle="modal" data-target="#exampleModal">Modal popup</a></p>
        </div>
        <div ref="modal" className="modal" id="exampleModal" role="dialog" >
            <div className="modal-dialog">
              <div className="modal-content">
                <div className="modal-header">      
                    <h4 className="modal-title">Header area</h4> 
              </div>
              <div className="modal-body">
                   body goes here..               
              </div>
              </div>
            </div>
          </div>
      </div>

    );
  }
}

因为我正在使用ES6所以我采用了类方法。我为模态内容创建了一个容器div,并给出了一个ref属性供参考。然后点击标签我附上了一个处理程序,当模态出现时它将触发。在this.refs的帮助下,您可以访问模态。希望它可以帮到你。