简体   繁体   English

在React Bootstrap中将类分配给模态

[英]Assign classes to modals in React Bootstrap

I am trying to style the React Bootstrap modal by assigning it classes via the docs here 我正在尝试通过此处的文档分配类来设置React Bootstrap模式的样式

prop: dialogClassName   
type: string
description: A css class to apply to the Modal dialog DOM node.

However, it is not showing up as a valid class. 但是,它没有显示为有效的类。

Flexbox, background-color, etc. do not seem to apply to any children of the Modal parent, which makes me think that either Modal disables any styling or I'm doing something incorrectly Flexbox,背景色等似乎不适用于Modal父级的任何子级,这使我认为Modal禁用了任何样式或我做错了什么

Thank you! 谢谢!

<ExampleModal 
     dialogClassName="planModal" <--Tried here
     modalState={this.props.modalState}
     openModal={this.props.actions.openModal}
     mealPlanArray={this.props.mealPlanArray}/>

render of ExampleModal looks like this ExampleModal的渲染看起来像这样

render() {                
    if (!this.props.modalState.openDisplay) { return ( <span/>) }
      return (
       <Modal className="TriedHere" show={this.props.modalState.openDisplay} onHide={this.props.openModal}>        
        <Modal.Header>            
          <h2>Bot Activated</h2>
        </Modal.Header>
        <Modal.Body>            
          {this.renderResults()}
        </Modal.Body>
      </Modal>    
    );
  } 
}

Can anyone think of a way to style the contents of React Modal? 谁能想到一种样式化React Modal内容的方法?

Just to elaborate on the comments a bit, 只是为了详细说明评论,

In your current implementation you are setting dialogClassName on the component responsible for rendering the <Modal /> component, rather than, as the docs say , on the <Modal /> component itself. 在当前的实现中,您要在负责呈现<Modal />组件的组件上设置dialogClassName ,而不是按照文档所说的<Modal />组件本身上设置。

This means that you have access to this.props.dialogClassName inside <ExampleModal /> rather than <Modal /> which is where react-bootstrap expects it. 这意味着您可以访问<ExampleModal /> this.props.dialogClassName而不是react-bootstrap期望它的<Modal />

There's nothing wrong with passing down the className from a higher level component, in fact it's a pretty likely use case that you would want to be conditionally rendering some styles to your <Modal /> component. 从更高级别的组件中传递className没什么错,实际上,您很可能会希望将一些样式有条件地呈现给<Modal />组件,这是一个很可能的用例。 Just be sure that it gets the prop dialogClassName . 只要确保它获得dialogClassName

In your case you just need to pass it one more level down: 在您的情况下,您只需要再向下传递一次即可:

<ExampleModal 
 dialogClassName="planModal"  <-- This is a custom prop that you have created, 
 ...                              it's been given the same name, but the name 
 ...                              could be anything really.
 ... 
/>

Then inside of the render method of <ExampleModal /> : 然后在<ExampleModal />的render方法内部:

<Modal 
  dialogClassName={this.props.dialogClassName}  <-- This is the prop that react-boostrap expects 
  show={this.props.modalState.openDisplay} 
  onHide={this.props.openModal}>  

On a side note: the code samples in the react-bootstrap docs are actually editable in-browser. 附带说明一下:react-bootstrap文档中的代码示例实际上是可在浏览器中编辑的。 Which can definitely be pretty useful whenever you're looking to test out things like the optional props for the different components they offer. 每当您想要测试诸如它们提供的不同组件的可选道具之类的东西时,这绝对是非常有用的。

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

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