简体   繁体   English

反应创建模态窗口的最佳实践

[英]React best practice to create modal window

How to create a modal window using React? 如何使用React创建模态窗口? By modal window I mean a frame which overlays with the content of a page. 通过模态窗口,我的意思是一个覆盖页面内容的框架。 I want it to open up using a button on the webpage, be able to close it by clicking outside area and it will contain a form which user will fill and submit. 我希望它能够使用网页上的按钮打开,能够通过单击外部区域来关闭它,它将包含一个用户将填写并提交的表单。

Does React have some production ready library for this task, or should I implement it using css? React是否为此任务提供了一些生产就绪库,还是应该使用css实现它? Also in the case of container/representational component pattern, should I nest the modal component inside the container component of modal opening component or inside the modal opening component itself? 同样在容器/代表性组件模式的情况下,我应该将模态组件嵌套在模态开口组件的容器组件内还是模态开口组件本身内?

React Modal是一个很棒的库。

1 - If you want to write a generic modal component in React, the only thing that your react modal component should do is to give a consistent view such as containing an overlay, setting positioning values and all common behaviour etc. The interaction decisions depends on how you proceed along the application. 1 - 如果你想在React中编写一个通用模态组件,你的反应模态组件应该做的唯一事情就是给出一致的视图,例如包含叠加,设置定位值和所有常见行为等。交互决策取决于你如何继续申请。 For example, in this kind of approach I mention as (1), fits better with using Flux. 例如,我提到的这种方法(1),更适合使用Flux。 Basically you have a store, and collect all component state related data in it, and the ui states are managed by props passed to your component instead of calling setState in order to change the state of your component. 基本上你有一个商店,并收集其中的所有组件状态相关数据,并且ui状态由传递给你的组件的props管理,而不是调用setState来改变组件的状态。 In addition, this gives you the ability to change the component's state outside of the component. 此外,这使您能够在组件外部更改组件的状态。 Let me clear myself with an example: 让我用一个例子清楚自己:

import "./modal.scss";

class Modal extends React.Component {
  constructor(props) {
    super(props);
  }

  shouldComponentUpdate(nextProps) {
    return !I.is(this.props.data, nextProps.data);
  }

  render() {
    let isOpen = this.props.data.get("isOpen");
    return (
      <div className={`flex middle center modal ${isOpen ? "open" : ""}`}>
      {isOpen ?
        (<div className={`row modal-content ${this.props.size || ""}`}>
          <div className="col-12 middle modal-title">{this.props.title}</div>
          <div className="col-12 modal-body">
            {this.props.body}
          </div>
          <div className="col-12 middle modal-footer">{this.props.footer}</div>
        </div>) : null
      }
      </div>);
  }
}

export default Modal;

And a style file like : 和样式文件如:

.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10000;
  padding: 2rem;
  background-color: rgba(0, 0, 0, 0.55);

  .modal-content {
    background-color: white;
    max-height: 80%;
    height: 80%;

    .modal-title {
      padding: 0 2.25rem;
      height: 5rem;
      max-height: 5rem;
      text-transform: uppercase;
      font-size: 1.8rem;
    }

    .modal-body {
      height: calc(100% - 16rem);
      overflow-y: scroll;
      padding: 0 2rem;
    }

    .modal-footer {
      height: 5rem;
      max-height: 5rem;
      padding: 0 2.25rem;
    }
  }
}

And you can use it as : 你可以用它作为:

<Modal isOpen={/* depends on your parameter */} 
       title="Hello Title" 
       body="Hello Body"
       footer="Hello Footer"
/>

This modal design only gives the proper view and basic controls of a modal, and configuration is all up to your usages. 此模态设计仅提供模态的正确视图和基本控件,配置完全取决于您的用法。 If your application logic pretty much similar in other usages of modals, then you can add them into the modal component yourself. 如果您的应用程序逻辑在其他模态用法中非常相似,那么您可以自己将它们添加到模态组件中。

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

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