简体   繁体   English

我希望Popup关闭,我该怎么做?

[英]I want Popup to close, how do I do it?

When I press the AddAction button from the Addaction component, I want the popup to close. 当我从Addaction组件按下AddAction按钮时,我想要关闭弹出窗口。 ?

in fact, if I reach the onCloseAddActionModal method in my component which is popup from AddAction component, my problem will be solved. 事实上,如果我在我的组件中到达onCloseAddActionModal组件弹出的onCloseAddActionModal方法,我的问题将得到解决。

AddAction Component: AddAction组件:

   class AddAction extends React.Component { 

    constructor() {
        super();
        this.state = {
            items: [{id:null, actiontype: null}],
            error: null,
            isLoaded: false,
            selectId: null,
        }
        this.handleCheckChieldElement = 
        this.handleCheckChieldElement.bind(this); // set this,  because you need get methods from CheckBox 
      }

    componentDidMount = () => {
        ....
    }

    fetchAdd = (carid, offboardingactiontypeid) => {
        ...
    }

    handleCheckChieldElement = (id, e) => {
        this.setState({selectId: id})
    }

    render() {
      const items = this.state.items;
      return (

        <div>
                <ul className="popupAddAction">
                    {
                        items.map((item) => {
                            return (
                                <li className="list" key={item.id}>
                                    <input  key={item.id} onClick= 
                    {(e) 
                                 => 

                    this.handleCheckChieldElement(item.id, 
                                e)} type="checkbox" /> 
                                 {item.actiontype}
                               </li>
                            )
                        })
                    } 
                </ul>
                <div className="popupAddAction--btn">
                    <button
                                    onClick=
                                    { () => 
                 this.fetchAdd(this.props.id, this.state.selectId)}
                                    className="btn btn-primary 
                                   popupAddAction--btn" 
                                >
                                    Add Action
                    </button>
                </div>
              </div>
               );
              }
             } 

           export default AddAction;

Popup Component:

         class OffBoarding extends Component {
          this.state = {
          openAddAction: false
          };

          onOpenAddActionModal = () => {
            this.setState({ openAddAction: true });
          };    

          onCloseAddActionModal = () => {
            this.setState({ openAddAction: false });
          };
          render(){
          return{
          <div>
          <Button className="btn btn-danger commentPlus" onClick= 
           {this.onOpenAddActionModal}> <FontAwesomeIcon icon= 
           {faPlus}/></Button>
          </div>
          {this.state.openAddAction ? 
              <div style={styles}>
               <Modal open= 
              {this.state.openAddAction} onClose= 
              {this.onCloseAddActionModal} center>
                                      <AddAction id= 
          {this.state.carid} 
                                   close= 
          {this.state.openAddAction}/>
                </Modal>
               </div> : null
             }}
          }}

You can simply pass the onCloseAddActionModal method as prop while rendering AddAction component from OffBoarding component. 您可以简单地将onCloseAddActionModal方法作为prop传递,同时从OffBoarding组件渲染AddAction组件。 And then, you can call that passed function as prop on "Add Action" button click ie 然后,您可以将“传递函数”作为“添加操作”按钮单击即调用

So in you popup component, change this: 所以在你的弹出组件中,改变这个:

 <AddAction id= 
      {this.state.carid} 
      close={this.state.openAddAction}/>

to this (passing function as prop): 对此(传递函数作为道具):

<AddAction id= 
      {this.state.carid} 
      close={this.state.openAddAction}
      closeDialog={this.onCloseAddActionModal}/>

And then in your AddAction component, change this: 然后在您的AddAction组件中,更改此:

  <button
   onClick={() => 
   this.fetchAdd(this.props.id, this.state.selectId)}
   className="btn btn-primary popupAddAction--btn">
     Add Action
   </button>

to this (calling function passed as prop in previous step): 对此(调用函数在上一步中作为prop传递):

<button
  onClick=
  {() =>{ 
  this.fetchAdd(this.props.id, this.state.selectId);
  this.props.closeDialog();
  }}
  className="btn btn-primary popupAddAction--btn">
  Add Action
 </button>

If openAddAction flag is true then only addaction component will display right. 如果openAddAction标志为true,则只有addaction组件才会显示正确。 Instead of open and close modal add below code to modal and in add action modal in fetch method set openAddAction to false. 而不是打开和关闭模态添加下面的代码到模态和添加动作模式在fetch方法中将openAddAction设置为false。 in your code you have communication from child to parent but you are trying to close modal based on child but modal exist in parent so make a communication to parent to child for that pass function through component 在您的代码中,您有从子级到父级的通信,但您尝试关闭基于子级的模式,但是父级中存在模态,因此通过组件与父级子级进行通信

 <Modal isOpen = {this.state.openAddAction} center>
        <AddAction id= {this.state.carid}  
          closeModa={this.onCloseAddActionModal} />
 </Modal>

In addAction modal you have to add like this 在addAction模式中,你必须像这样添加

fetchAdd = (carid, offboardingactiontypeid) => {
    this.setState({openAddAction:false});
    this.props.onCloseAddActionModal();
}

Call this closeModal method in fetchAdd method 在fetchAdd方法中调用此closeModal方法

暂无
暂无

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

相关问题 我发布到Facebook后如何关闭弹出窗口? - How do I close the popup after I post to facebook? 如何在弹出窗口中插入关闭按钮 - How do I insert a close button inside my popup 如何使我的弹出窗口永久关闭? - How do I get my popup form to close permanently? 如何创建一个包含表格并具有关闭按钮的弹出窗口? - how do I create a popup that contains table and has a close button? 如何关闭表单提交中的模式弹出窗口? - How do I close Modal Popup on form submit? 我有一些简单的 JavaScript 代码,我想把它变成一个弹出扩展。 我该怎么做呢? - I have some simple JavaScript code, and I want to turn it into a popup extension. How do I do this? 如何在按下按钮时关闭弹出窗口,并在打开弹出窗口的窗口上发起回发? - How do I close a popup window on a press of a button and initiate postback on the window opening the popup? 我想在angularjs的选项卡关闭时显示弹出窗口吗? - I want to Display Popup on tab close in angularjs? 如何在未收到“是否要关闭此窗口”提示的情况下关闭浏览器窗口? - How can I close a browser window without receiving the “Do you want to close this window” prompt? 当我单击该弹出窗口内的编辑按钮时,我想使用输入字段调用相同的弹出窗口,我该怎么做(如果可能)? - I want to call the same popup with input fields when I click on edit button which is inside that popup, how can I do this (if possible)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM