简体   繁体   English

如何使用 ant-design 将 react-draggable 用于模态?

[英]How to use react-draggable for modal using ant-design?

I decided to use the react-draggable npm package for my modal window using ant-design, (for those who don't know react-draggable, this is a simple component for making elements draggable).我决定为我的模态 window 使用react-draggable npm package,(对于那些不知道使用 reactant-design 制作元素的简单可拖动组件的人)。 But I am having difficulty, I tried to wrap my modal window inside but I got an error.但是我遇到了困难,我试图将我的模态 window 包裹在里面,但我得到了一个错误。

Cannot read property 'className' of undefined无法读取未定义的属性“类名”

import React, { useState } from "react";
import "antd/dist/antd.css";
import Modal from "antd/es/modal/Modal";
import Draggable from "react-draggable";
import Button from "antd/es/button";

const App = (props) => {
  const [visible, setVisible] = useState(false);

  return (
    <Draggable>
      <Button onClick={() => setVisible(true)} type="primary">
        Themes
      </Button>
      <Modal
        title="Customize the theme to your liking"
        centered
        visible={visible}
        onOk={() => setVisible(false)}
        onCancel={() => setVisible(false)}
        width={700}
      >
        <div className={"SideBarModal_Wrapper"}>
          <div className={"SideBarModal_Appearance"}>
            <div className={"SideBarModal_Child_Appearance"}>
              <p>Appearance</p>
            </div>

            <div>{props.SideBarWallpaperList}</div>
          </div>

          <div className={"SideBarModal_Accept_Color"}>
            <div className={"SideBarModal_Child_Color"}>
              <p>Colors</p>
            </div>

            <div>{props.list}</div>
          </div>
        </div>
      </Modal>
    </Draggable>
  );
};

export default App;

You can see my code in codesandbox there I have already installed all the required packages, but I could not apply react-draggable.你可以在代码沙箱中看到我的代码,我已经安装了所有必需的包,但我无法应用 react-draggable。 Here is the link https://codesandbox.io/s/xenodochial-mendel-4bdun这是链接https://codesandbox.io/s/xenodochial-mendel-4bdun

I believe you need to have a single child element in Draggable .我相信您需要在Draggable中有一个子元素。

So either put only the Button in the Draggable or wrap your Button and Modal in a div .因此,要么只将Button放在Draggable中,要么将ButtonModal包装在div中。

updated: https://codesandbox.io/s/currying-river-olme0?file=/src/App.js更新: https://codesandbox.io/s/currying-river-olme0?file=/src/App.js

The documentation says it loud and clear, Only a single child is allowed or an Error will be thrown. 文档说得很清楚,只允许一个孩子,否则会抛出错误。 :) :)

Bind the child components as one single child.将子组件绑定为一个子组件。 (eg, using a div as I've done below) (例如,使用div如下所示)

 import React, { useState } from "react"; import "antd/dist/antd.css"; import Modal from "antd/es/modal/Modal"; import Draggable from "react-draggable"; import Button from "antd/es/button"; const App = (props) => { const [visible, setVisible] = useState(false); return ( <Draggable> <div> <Button onClick={() => setVisible(true)} type="primary"> Themes </Button> <Modal title="Customize the theme to your liking" centered visible={visible} onOk={() => setVisible(false)} onCancel={() => setVisible(false)} width={700} > <div className={"SideBarModal_Wrapper"}> <div className={"SideBarModal_Appearance"}> <div className={"SideBarModal_Child_Appearance"}> <p>Appearance</p> </div> <div>{props.SideBarWallpaperList}</div> </div> <div className={"SideBarModal_Accept_Color"}> <div className={"SideBarModal_Child_Color"}> <p>Colors</p> </div> <div>{props.list}</div> </div> </div> </Modal> </div> </Draggable> ); }; export default App;

The basic concept is actually like this:基本概念其实是这样的:

import Draggable from 'react-draggable';

<div className="modal fade show d-block" id="myModal">
  <div className="modal-dialog">

   <Draggable> 
     
    <div className="modal-content">
      <div className="modal-header">
        <h4 className="modal-title">Modal Heading</h4>
        <button type="button" className="close" data-dismiss="modal">&times;</button>
      </div>
      <div className="modal-body">
        <h4>This is body</h4>
      </div>
      <div className="modal-footer">
        <button type="button" className="btn btn-danger" data-dismiss="modal">Close</button>
      </div>
    </div>

    </Draggable> 

  </div>
</div>

Try to figure out how to change it if you use react-bootstrap如果您使用 react-bootstrap,请尝试弄清楚如何更改它

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

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