26

我需要像这样打开/关闭模式

$(元素).modal('显示')

怎么做?

4

4 回答 4

35

您正在寻找的是自定义模态触发器,它使用OverlayMixin并允许您自己管理模态的状态。您可以控制是否显示模态,this.setState({isModalOpen: true})以实现与您在下面示例中的帖子中所要求的等效。以下代码来自 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>
    );
  }
});
于 2015-04-07T10:46:55.923 回答
12

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

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

onHide函数只是更改showModal状态属性。您的模态根据父母的状态显示/隐藏:

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

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

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

这是一个模拟此工作流程的小提琴。

于 2015-09-11T09:48:57.477 回答
4

你可以用 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;

于 2019-08-13T01:58:56.057 回答
4

从状态以编程/动态方式打开和关闭 react-bootstrap 模式:

这里我使用了 ES6 语法类组件语法。

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;
于 2016-12-05T10:59:19.817 回答