Open SunXinFei opened 6 years ago
按照react-dnd的issue中封装一个context类,调用该类即可
import {DragDropContext} from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
export default DragDropContext(HTML5Backend);
如果自定义了dragPreview,取消掉默认的浏览器preview就需要添加getEmptyImage
import { getEmptyImage } from 'react-dnd-html5-backend';
componentDidMount() {
// Use empty image as a drag preview so browsers don't draw it
// and we can draw whatever we want on the custom drag layer instead.
this.props.connectDragPreview(getEmptyImage(), {
// IE fallback: specify that we'd rather screenshot the node
// when it already knows it's being dragged so we can hide it with CSS.
captureDraggingState: true
});
}
React-dnd更新版本迭代时隔一年已经升级了N个版本,迭代升级速度让我非常吃惊,于是最新项目,我们使用v9.3.9版本去开发,看看跟之前的代码变化。
DragSource
DragSource(type, spec, collect)的参数解读
type :Required. Either a string, an ES6 symbol, or a function that returns either given the component's props. Only the drop targets registered for the same type will react to the items produced by this drag source. Read the overview to learn more about the items and types.
必须,设置好跟dropTarget相同的type字符串,才能够放置。
spec: Required. A plain JavaScript object with a few allowed methods on it. It describes how the drag source reacts to the drag and drop events. See the drag source specification described in detail in the next section.
必须,一个js对象,里面有一些拖拽相关的方法。
关于spec中的方法:
collect: Required. The collecting function. It should return a plain object of the props to inject into your component. It receives two parameters: connect and monitor. Read the overview for an introduction to the monitors, the connectors, and the collecting function. See the collecting function described in detail after the next section.
必须,collect方法有两个参数connect和monitor
options: Optional. A plain object. If some of the props to your component are not scalar (that is, are not primitive values or functions), specifying a custom arePropsEqual(props, otherProps) function inside the options object can improve the performance. Unless you have performance problems, don't worry about it.
可选的参数,如果有性能问题才需要关心。
Monitor一些公共方法的表示的值
表示鼠标指针的在拖拽操作开始时的xy坐标,浏览器可视范围内的,不考虑滚动条,浏览器可视范围的左上角为(0,0)。
表示拖拽的当面DOM在拖动操作开始时的xy坐标,在浏览器可视范围内的,不考虑滚动条,浏览器可视范围的左上角为(0,0)。
表示拖拽中鼠标指针的xy坐标,在浏览器可视范围内的, 不考虑滚动条,浏览器可视范围的左上角为(0,0)。
拖拽的DOM相对于自身之前所在位置的偏移。完全重合为(0,0)。
拖拽的DOM在拖动操作中的xy坐标,放置到可视范围内的左上角为(0,0),不考虑滚动条