如何以编程方式 open/close react-bootstrap 模态?

How to open/close react-bootstrap modal programmatically?

我需要open/close模态

$(element).modal('show')

怎么做?

您正在寻找的是自定义模态触发器,它使用 OverlayMixin 并允许您自己管理模态的状态。您可以使用 this.setState({isModalOpen: true}) 控制是否显示模态,以实现与您在下面示例中的 post 中要求的等效。以下代码来自react-bootstrap网站(http://react-bootstrap.github.io/components.html#modals):

const CustomModalTrigger = React.createClass({
  mixins: [OverlayMixin],

  getInitialState() {
    return {
      isModalOpen: false
    };
  },

  handleToggle() {
    this.setState({
      isModalOpen: !this.state.isModalOpen
    });
  },

  render() {
    return (
      <Button onClick={this.handleToggle} bsStyle='primary'>Launch</Button>
    );
  },

  // This is called by the `OverlayMixin` when this component
  // is mounted or updated and the return value is appended to the body.
  renderOverlay() {
    if (!this.state.isModalOpen) {
      return <span/>;
    }

    return (
      <Modal bsStyle='primary' title='Modal heading' onRequestHide={this.handleToggle}>
        <div className='modal-body'>
          This modal is controlled by our custom trigger component.
        </div>
        <div className='modal-footer'>
          <Button onClick={this.handleToggle}>Close</Button>
        </div>
      </Modal>
    );
  }
});

React.render(<CustomModalTrigger />, mountNode);

更新(2015 年 8 月 4 日)

在最新版本的 React-Bootstrap 中,是否显示模态框由传递给模态框的 show prop 控制。不再需要 OverlayMixin 来控制模态。控制模态的状态仍然是通过 setState 完成的,在这个例子中是通过 this.setState({ showModal: true })。以下内容基于 React-Bootstrap 网站上的示例:

const ControlledModalExample = React.createClass({

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

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

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

  render() {
    return (
      <div>
        <Button onClick={this.open}>
          Launch modal
        </Button>

        <Modal show={this.state.showModal} onHide={this.close}>
          <Modal.Header closeButton>
            <Modal.Title>Modal heading</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            <div>Modal content here </div>
          </Modal.Body>
          <Modal.Footer>
            <Button onClick={this.close}>Close</Button>
          </Modal.Footer>
        </Modal>
      </div>
    );
  }
});

您的模式将有一个 show 属性和一个 onHide 属性来确定它何时显示。例如:

<Modal show={this.state.showModal} onHide={this.close}>

onHide 函数只是改变 showModal 状态 属性。你的模式是 shown/hidden 基于 parent 的状态:

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

如果您想从模态本身中关闭模态,您可以通过 props 触发 parent 上定义的 onHide 函数。例如,这是模式内部某处的按钮关闭它:

<button type="button" className="close" onClick={this.props.onHide}>
  <span>&times;</span>
</button>

Here is a fiddle 模拟此工作流。

从状态打开和关闭 react-bootstrap 模态 programmatically/dynamically:

这里我使用了ES6语法class组件语法

import React, { Component, PropTypes } from 'react';
import { Modal, Button } from 'react-bootstrap';
import './GenericModal.scss';

class GenericModal extends Component {
  constructor(props, context) {
  super(props, context);

  this.state = {
    showModal: false
  };

  this.open = this.open.bind(this);
  this.close = this.close.bind(this);
}


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

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

render() {
  return(
    <div>
      <div>I am a Bootstrap Modal</div>
      <Button onClick={this.open}>Show Modal</Button>
      <div>
        <Modal className="modal-container" 
          show={this.state.showModal} 
          onHide={this.close}
          animation={true} 
          bsSize="small">

          <Modal.Header closeButton>
            <Modal.Title>Modal title</Modal.Title>
          </Modal.Header>

          <Modal.Body>
            One of fine body.........
          </Modal.Body>

          <Modal.Footer>
            <Button onClick={this.close}>Close</Button>
            <Button bsStyle="primary">Save changes</Button>
          </Modal.Footer>         
        </Modal> 
      </div>
    </div>
  );
 }
}

export default GenericModal;

您可以使用 React hooks 来完成。 (包含在 React 16.8 中)

import React, { useState } from "react";
import { Modal, Button } from "react-bootstrap";

const ComponentWithModal = props => {
  const [isModalOpen, setModalStatus] = useState(false);

  return (
    <div>
      <div>I am a Bootstrap Modal</div>
      <Button onClick={() => setModalStatus(true)}>Show Modal</Button>
      <div>
        <Modal
          className="modal-container"
          show={isModalOpen}
          onHide={() => setModalStatus(false)}
        >
          <Modal.Header closeButton>
            <Modal.Title>Modal title</Modal.Title>
          </Modal.Header>

          <Modal.Body>Modal content here</Modal.Body>

          <Modal.Footer>
            <Button onClick={() => setModalStatus(false)}>Close</Button>
          </Modal.Footer>
        </Modal>
      </div>
    </div>
  );
};

export default ComponentWithModal;