Comments (13)
确实会有闭包问题,把 Draggable 封装到另一个组件里,不要在 confirm 的 function 里传入 onStart 这些 handler
from ant-design.
这样是可以的
import ReactDOM from "react-dom";
import React, { useRef, useState } from "react";
import { Modal } from "antd";
import Draggable from "react-draggable";
import "antd/dist/reset.css";
import "./index.css";
const Comp = ({ modal }: any) => {
const draggleRef = useRef(null);
const [bounds, setBounds] = useState({
left: 0,
right: 0,
top: 0,
bottom: 0,
});
const onStart = (_event: any, uiData: any) => {
const { clientWidth, clientHeight } = window.document.documentElement;
const targetRect = draggleRef.current?.getBoundingClientRect();
if (!targetRect) {
return;
}
console.log({
left: -targetRect.left + uiData.x,
right: clientWidth - (targetRect.right - uiData.x),
top: -targetRect.top + uiData.y,
bottom: clientHeight - (targetRect.bottom - uiData.y),
});
setBounds({
left: -targetRect.left + uiData.x,
right: clientWidth - (targetRect.right - uiData.x),
top: -targetRect.top + uiData.y,
bottom: clientHeight - (targetRect.bottom - uiData.y),
});
};
return (
<Draggable
bounds={bounds}
nodeRef={draggleRef}
onStart={(event, uiData) => onStart(event, uiData)}
>
<div ref={draggleRef}>{modal}</div>
</Draggable>
);
};
const useMergeModal = () => {
const { confirm } = Modal;
const showMergeModal = () => {
const current = confirm({
onOk: () => {
current.destroy();
},
modalRender: (modal) => <Comp modal={modal} />,
});
};
return {
showMergeModal,
};
};
const App = () => {
const { showMergeModal } = useMergeModal();
return (
<div>
<button onClick={showMergeModal}>api modal</button>
</div>
);
};
ReactDOM.render(<App />, document.getElementById("root"));
from ant-design.
from ant-design.
Hello @lgz5689. Please provide a online reproduction by forking codesandbox of [email protected] or [email protected], or provide a minimal GitHub repository. Issues labeled by Need Reproduce
will be closed if no activities in 3 days.
你好 @lgz5689,我们需要你提供一个在线的重现实例以便于我们帮你排查问题。你可以通过点击这里创建一个 [email protected] 或 [email protected] 的 codesandbox,或者提供一个最小化的 GitHub 仓库。3 天内未跟进此 issue 将会被自动关闭。
from ant-design.
https://codesandbox.io/p/sandbox/antd-reproduction-template-forked-q44yy4?file=%2Findex.js
from ant-design.
应该是拖拽的逻辑有问题哈
from ant-design.
https://ant-design.antgroup.com/components/modal-cn#components-modal-demo-modal-render
我是参考官网的写法 拖拽相关的逻辑是一样 只是使用了api来进行打开Modal
from ant-design.
这样修改后,可以发现每次执行onStart,都只会打印上一次的uiData,这里是否产生了闭包陷阱?
from ant-design.
可以仔细看下 https://github.com/react-grid-layout/react-draggable 的文档,onStart 的触发时机就是这样的
from ant-design.
https://codesandbox.io/p/sandbox/antd-reproduction-template-forked-q44yy4?file=%2Findex.js%3A12%2C41
我最终发现 打开modal之后 使用的一直是打开modal时的state 这个我该如何解决
from ant-design.
这个问题跟 modal 没关系,你应该去看看拖拽相关的文章
from ant-design.
这样是可以的
import ReactDOM from "react-dom"; import React, { useRef, useState } from "react"; import { Modal } from "antd"; import Draggable from "react-draggable"; import "antd/dist/reset.css"; import "./index.css"; const Comp = ({ modal }: any) => { const draggleRef = useRef(null); const [bounds, setBounds] = useState({ left: 0, right: 0, top: 0, bottom: 0, }); const onStart = (_event: any, uiData: any) => { const { clientWidth, clientHeight } = window.document.documentElement; const targetRect = draggleRef.current?.getBoundingClientRect(); if (!targetRect) { return; } console.log({ left: -targetRect.left + uiData.x, right: clientWidth - (targetRect.right - uiData.x), top: -targetRect.top + uiData.y, bottom: clientHeight - (targetRect.bottom - uiData.y), }); setBounds({ left: -targetRect.left + uiData.x, right: clientWidth - (targetRect.right - uiData.x), top: -targetRect.top + uiData.y, bottom: clientHeight - (targetRect.bottom - uiData.y), }); }; return ( <Draggable bounds={bounds} nodeRef={draggleRef} onStart={(event, uiData) => onStart(event, uiData)} > <div ref={draggleRef}>{modal}</div> </Draggable> ); }; const useMergeModal = () => { const { confirm } = Modal; const showMergeModal = () => { const current = confirm({ onOk: () => { current.destroy(); }, modalRender: (modal) => <Comp modal={modal} />, }); }; return { showMergeModal, }; }; const App = () => { const { showMergeModal } = useMergeModal(); return ( <div> <button onClick={showMergeModal}>api modal</button> </div> ); }; ReactDOM.render(<App />, document.getElementById("root"));
请问用这样的示例代码,如何做到 disable 的状态,想做成类似官方示例中点击标题拖拽,不点击标题按照浏览器正常事件处理
from ant-design.
i see...
Draggable 有个入参数是 handler ,会比antd官方的用起来好一点
like
<Draggable handle='.handler' bounds={bounds} onStart={(event, uiData) => onStart(event, uiData)}>
<div ref={draggleRef}>{content}</div>
</Draggable>
在content 的div中确保 classname 存在 handler
from ant-design.
Related Issues (20)
- Table 编辑行 ellipsis 失效 HOT 4
- 【bug】表格排序错误的排序出参 HOT 2
- Select组件通过 dropdownRender 自定义虚拟列表 Table, 提示React does not recognize the offsetX prop on a DOM element HOT 5
- 无法使用antd包 HOT 4
- [Wrong statement]Why is component defaultValue not working when inside Form.Item? HOT 4
- Badge 徽标数 设置了color时 count为0时 本该消失,但仍显示小红点 HOT 4
- Colorpicker dragging too many times will be banned HOT 11
- Modal组件的destroyOnClose属性无法销毁其中Form组件具有form属性时的状态 HOT 1
- 受控的Select组件,onChange中setValue为undefiend后,展示的label与实际的值不一致 HOT 3
- Dropdown 菜单支持展示 Checkbox HOT 5
- 父层级设置了scale后,Select下拉列表展开错误 HOT 1
- Table: Horizontal scrollbar is between table and fixed summary HOT 1
- DatePicker crashes when input is custimized via components props HOT 4
- datepicker 能否支持修改表头星期样式 HOT 4
- > Picker will set `locale` to dayjs object before format to ensure it get correct format with locale. Which means it's not enough to set timezone only and should provide fully local info. Pls ref official doc of ConfigProvider with correct locale handle. HOT 2
- 当Badge的status为processing且使用tailwindcss时,闪烁效果异常 HOT 2
- Tabs 以及页签禁用tabs键选中
- 升级V5后,原来的Less文件中使用的变量,在新版中怎么用
- After upgrading from version 5.5 to version 5.16 I get Node memory limit error HOT 6
- Table组件在dataSource为空数组时,且columns为动态列较多时,scroll设置为出现滚动条时,初次渲染会闪动,而且非常明显,感觉开始没有加载宽度,列越多闪动截止明显。 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ant-design.