简体   繁体   English

模态窗口从未激活

[英]Modal Window never activated

I am working with javascript React (I am new to it). 我正在使用JavaScript React(我是新手)。 From my main page, I want to click on a button to open a dialog window to allow the user to input some values. 我想在我的主页上单击一个按钮以打开一个对话框窗口,以允许用户输入一些值。

I found out about modal windows, so I tried generating one after analysing some examples, but for now, nothing happens... 我发现了模态窗口,因此我在分析了一些示例后尝试生成一个模态窗口,但目前没有任何反应...

I tried this code : 我尝试了这段代码:

My main page, Environment.js : 我的主页Environment.js:

import React, { Component } from 'react'
import { ReactLoader } from 'Loader'
import Modal from './Modal';

class EnvironmentExtension extends ModelExtension {

constructor (viewer, options) {

 super (viewer, options)
 this.state = { isModalOpen: false };

 this.openModal = this.openModal.bind(this)
 this.closeModal = this.closeModal.bind(this)
 this.react = options.react }

openModal = () => {
 this.react.setState({isModalOpen: true});
 console.log("openModal()");
 console.log('state modal');
 console.log(this.state.isModalOpen)}

closeModal = () => {
 this.react.setState({isModalOpen: false});
 console.log("closeModal()");
 console.log('state modal');
 console.log(this.state.isModalOpen)
}

render () {
 return (
  <button onClick={() => this.openModal()}  
    title="Show map dialog :O">
    <span className="fa fa-hand-o-left"/>
  </button>

  <Modal isOpen={this.state.isModalOpen} onClose={()=>this.closeModal()}>
    content for modal
    <button onClick={()=> this.closeModal()}>
     Close
    </button>
   </Modal> )}

And for the modal file, modal.js : 对于模态文件modal.js:

import React from 'react';
import PropTypes from 'prop-types';

class Modal extends React.Component {
 render() {

 if(!this.props.isOpen) {
  return null;
}

 // The gray background
 const backdropStyle = {
   position: 'fixed',
   top: 0,
   bottom: 0,
   left: 0,
   right: 0,
   backgroundColor: 'rgba(0,0,0,0.3)',
   padding: 50};

 // The modal "window"
 const modalStyle = {
   backgroundColor: '#fff',
   borderRadius: 5,
   maxWidth: 500,
   minHeight: 300,
   margin: '0 auto',
   padding: 30};

return (
  <div className="backdrop">
    <div className="modal">
      {this.props.children}

      <div className="footer">
        <button onClick={this.props.onClose}>
          Close
        </button>
      </div>
    </div>
  </div>
);
}
}

Modal.propTypes = {
  onClose: PropTypes.func.isRequired,
  onOpen: PropTypes.bool,
  children: PropTypes.node
};

export default Modal;

When I run the code, I get this error : 运行代码时,出现此错误:

Warning: Unknown props isOpen , onClose on tag. 警告:未知isOpen ,标记上的onClose Remove these props from the element 从元素中移除这些道具

And when I click the button supposed to open the dialog box, I have the text "content for modal" showing up in the middle of the page, as well as the button Cancel. 当我单击应该打开对话框的按钮时,页面中间会显示文本“模式内容”,以及“取消”按钮。 Moreover, in the console, the boolean isModalOpen is always false... No dialog box showing up at all... 而且,在控制台中,布尔值isModalOpen始终为false ...根本没有对话框出现...

Two things. 两件事情。

Firstly, I'm not sure where your warning is coming from, but take a look at this: https://reactjs.org/warnings/unknown-prop.html . 首先,我不确定您的警告来自哪里,但请看一下: https : //reactjs.org/warnings/unknown-prop.html Perhaps you have a user-defined component that is not capitalised (it might be missing from the code you've posted). 也许您有一个未大写的用户定义组件(您发布的代码中可能缺少该组件)。

Secondly, it sounds like your modal is opening and closing correctly, and that you're actually encountering a CSS issue. 其次,听起来您的模态正确地打开和关闭,并且您实际上遇到了CSS问题。 On modalStyle , add the following: modalStyle ,添加以下内容:

position:absolute
top:50%;
left:50%;
transform:translate(-50%, -50%);
width:400px;
height:300px;

This should give you a starting point. 这应该给您一个起点。

On a related note try using a UI kit, like Semantic ( https://react.semantic-ui.com ) when learning about styling up React components. 在相关说明中,在了解有关对React组件进行样式设置时,请尝试使用UI套件,例如Semantic( https://react.semantic-ui.com )。

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

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