React-Bootstrap 多模态

Mur*_*rat 6 twitter-bootstrap bootstrap-modal react-bootstrap

我在我的项目中使用 React-bootstrap。我需要打开多个对话框。有没有办法实现这一目标?

注:有一个引导的答案在这里,但它不工作的反应,引导。谢谢。

Pha*_*uri 7

如果您使用 scss 作为 css 预处理器,那么您可以使用循环来定义正确的z-index's 以使所有内容都显示为一个顶部。

此循环最多可处理 5 个级别,您可以根据需要增加数量

@for $i from 1 through 6 {
  $zIndexBackdrop:  #{1000 + (5 * $i)};
  $zIndexContent:  #{1000 + (5 * $i) + 2};
  .modal-backdrop.show:nth-of-type(#{$i}) {
    z-index: $zIndexBackdrop;
  }
  div[role="dialog"][aria-modal="true"]:nth-of-type(#{$i}) {
    z-index: $zIndexContent;
  }
}
Run Code Online (Sandbox Code Playgroud)

这里的1 through 6循环循环了 5 次,如果需要,可以通过增加数量来增加模态的深度。

我使用了react-bootstrap模态使用的通用类名,请交叉检查背景和主要模态是如何创建的。

  • 谢谢。这很有魅力。 (3认同)

Arm*_*ino 7

“nth-of-type”解决方案对我不起作用。相反,我用“nth-last-child”这样的方法解决了这个问题。

div[role="dialog"][aria-modal="true"]:nth-last-child(1) {
  z-index: 1125;
}
.modal-backdrop.show:nth-last-child(2){
  z-index: 1100;
}
div[role="dialog"][aria-modal="true"]:nth-last-child(3) {
  z-index: 1075;
}
.modal-backdrop.show:nth-last-child(4){
  z-index: 1050;
}
div[role="dialog"][aria-modal="true"]:nth-last-child(5) {
  z-index: 1025;
}
.modal-backdrop.show:nth-last-child(6){
  z-index: 1000;
}
Run Code Online (Sandbox Code Playgroud)

这个想法是,每次显示模态时,模态元素和阴影元素将被附加到主体(库已经发生了这种情况)。因此,从最后一个子元素开始,(n + 1) 元素将是模态元素,而 (n + 2) 元素将是模态阴影元素。

在示例中,我将嵌套模态设置为最多 3 个级别,但您可以根据需要添加其中两个样式。


Sam*_*eph 5

我们只是通过在不同的 id 中每次传递然后将“显示”状态设置为来处理多个模态:

https://github.com/AgileVentures/agile-ventures-website-react-front-end/blob/4_homepage_changes_mob/src/App.js#L26

class App extends Component {

  constructor(props, context) {
    super(props, context);

    this.handleShow = this.handleShow.bind(this);
    this.handleClose = this.handleClose.bind(this);

    this.state = {
      show: null
    };
  }

  handleClose() {
    this.setState({show: id});
  }

  handleShow(id) {
    this.setState({show: id});
  }

  render() {
    return (
      <div className="App">

        <Grid>
          <Row>
            <Col xsOffset={1} sm={5}>
              <Button bsStyle="primary" bsSize="large" onClick={() => this.handleShow('here')}>
                <h1>You are here!</h1>
              </Button>
              <Modal
                show={this.state.show == 'here'} onHide={this.handleClose}
              >
                <Modal.Header closeButton closeLabel="close window">
                </Modal.Header>
                <Modal.Body>
                  <p className='landing-page-markers you-are-here'>Tired of toy projects, tutorials and online courses?
                      <img src={logo} className="App-logo" alt="logo" />
                  </p>
                </Modal.Body>
              </Modal>
            </Col>
          </Row>
          ... more modals passing in different ids ...
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您使用的是 React 16.8+,您可以使用 React Hook useState(和功能组件),这样您就可以根据状态指定您希望它显示的模式:

export const MyModals = () => {
const [modalState, setModalState] = useState< "modal-one" | "modal-two" | "close" >("close")

const handleShowModalOne = () => {
 setModalState("modal-one")
}

const handleShowModalTwo = () => {
 setModalState("modal-two")
}

const handleClose = () => {
 setModalState("close")
}

return (
 <div>
   <Button onClick={handleShowModalOne}>Show Modal One</Button>

   <Modal show={modalState === "modal-one"}>
     <Modal.Body>This is Modal One</Modal.Body>
     <Modal.Footer>
       <Button onClick={handleShowModalTwo}>Show Modal Two</Button>
     </Modal.Footer>
   </Modal>

   <Modal show={modalState === "modal-two"}>
     <Modal.Body>This is Modal Two</Modal.Body>
     <Modal.Footer>
       <Button onClick={handleClose}>Close</Button>
     </Modal.Footer>
   </Modal>
 </div>
)
}

Run Code Online (Sandbox Code Playgroud)