javascript - 有没有一种方法可以在不使用 ANTD 上的默认按钮的情况下关闭 Modal?

标签 javascript reactjs antd ant-design-pro

所以我是 ReactJS 的新手,我正在使用 ANT Design,目前正在使用他们的 Modal。我想知道我们是否可以在不使用 OKCancel 按钮的情况下关闭模态框。

所以我删除了这些按钮。并在配置中创建了一个按钮。我想使用该按钮关闭模式。任何帮助都会很棒!提前致谢!

这是我的代码。

const {  Modal, Button  } = antd;

const ReachableContext = React.createContext();
const UnreachableContext = React.createContext();

const handleButtonOnClick = () => {
  console.log('this button was clicked');
}

const config = {
  visible: false,
  title: 'Use Hook!', icon: null,
  okButtonProps: { style: { display: 'none' } },
  // cancelButtonProps: { style: { display: 'none' } },
  content: (
    <div>
      <ReachableContext.Consumer>
        {sample => (
          <Button
            type='primary'
            block
          >
            Click Me Button
            // IS THERE A FUNCTION THAT I CAN CLOSE THE MODAL USING THIS BUTTON?
          </Button>
         )}
      </ReachableContext.Consumer>
    </div>
  ),
};

const App = () => {
  const [modal, contextHolder] = Modal.useModal();
  return (
    <ReachableContext.Provider value={modal}>
      <Button
        onClick={() => {
          modal.confirm(config);
        }}
      >
        Confirm
      </Button>
      {contextHolder} 
    </ReachableContext.Provider>
  );
};

ReactDOM.render(<App />, mountNode); 

最佳答案

这就是我关闭/显示 Modal 的方式.我不使用 Okcancel 按钮。如果 showForm 属性为 true,则 Modal 将显示,否则不会显示。

import React, { Component } from "react";
import { connect } from "react-redux";
import * as actions from "../../actions";

import { Modal, Icon } from "antd";

class FormContainerModal extends Component {
  state = {};

  render() {
    const { showForm } = this.props;
    return (
      <>
        <Modal
          title={
            <div>
              Title
            </div>
          }
          destroyOnClose={true}
          footer={null}
          centered
          maskClosable={false}
          onCancel={this.props.closeModal}
          visible={showForm} //it will close the modal if showForm is false
          width="950px"
        >
            <div>
              My Content
            </div>
        </Modal>
      </>
    );
  }
}

const mapStateToProps = state => {
  return {
    showForm: state.form.showForm
  };
};

export default connect(mapStateToProps, actions)(FormContainerModal);

在您的情况下,您可以在单击按钮时更改 showForm 的 bool 值。

           <Button
            type='primary'
            block
           onClick={()=>this.setState({showForm: false})} //here make showForm to false to close the modal
          >
            Close the Modal
          </Button>

关于javascript - 有没有一种方法可以在不使用 ANTD 上的默认按钮的情况下关闭 Modal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60592716/

相关文章:

javascript - 如何在图片的两个区域悬停?

javascript - 如何居中引导行元素?

css - 如何在 React 中使用 Material-ui 将内联样式更改为 css?

javascript - 如何取消选中 Antd React.js 中的 Checkbox.Group

javascript - antd react 中有没有解决方法来检查组件中未保存的更改?

javascript - 如何重置 ant design 表选定的行?

javascript - 如何在 Angular 2 或 4 的 Jasmine 测试中模拟鼠标事件

javascript - FireBug 的 console.log() 和 console.debug() 有什么区别?

ReactJS - 新 useState React Hook 中的 prevState?

reactjs - 在使用 React hooks 渲染之前调度 redux 操作