强大的拖拽组件:React DnD 的使用

10.6k 次阅读  ·  读完需要 25 分钟

17

文章首发我的个人blog : 原文链接

学习 React DnD 的最初原因是阅读《如何写一个拖拽日历组件》附的源码时,看不懂拖拽组件 React DnD 的相关代码,于是行动力极强地学习了React DnD这个组件。

本文会通过 在根组件(Contaier.jsx)展示将垃圾(Box.jsx)扔进垃圾桶(Dustbin.jsx)的例子,解释如何使用React DnD最基本的拖拽用法。

预览 垃圾桶效果

查看 垃圾桶源码

核心API

想要灵活使用,就先知道几个核心API

  • DragSource 用于包装你需要拖动的组件,使组件能够被拖拽(make it draggable)
  • DropTarget 用于包装接收拖拽元素的组件,使组件能够放置(dropped on it)
  • DragDropContex 用于包装拖拽根组件,DragSource 和 DropTarget 都需要包裹在DragDropContex
  • DragDropContextProvider 与 DragDropContex 类似,用 DragDropContextProvider 元素包裹拖拽根组件。

大致理解这几个API的概念后,垃圾(Box.jsx)扔进垃圾桶(Dustbin.jsx)的代码将会是:

// Box.jsx
import { DragSource } from 'react-dnd'; @DragSource(type, spec, collect)
export default class Box {
/* ... */
} // Dustbin.jsx
import { DropTarget } from 'react-dnd'; @DropTarget(types, spec, collect)
export default class Contaier {
/* ... */
} // Contaier.jsx (DragDropContex)
import { DragDropContext } from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
import Box from './Box';
import Dustbin from './Dustbin'; @DragDropContext(HTML5Backend)
export default class Contaier extends Component {
render() {
return (
<div>
<Dustbin/>
<Box/>
</div>
);
}
} // 也可以写成 Contaier.jsx (DragDropContextProvider)
import { DragDropContextProvider } from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
import Box from './Box';
import Dustbin from './Dustbin'; export default class DustbinContaier extends Component {
render() {
return (
<DragDropContextProvider backend = { HTML5Backend }>
<div>
<Dustbin/>
<Box/>
</div>
</DragDropContextProvider>
);
}
}

API参数介绍

上面的代码

@DragSource(type, spec, collect)
@DropTarget(types, spec, collect)

可以看到 DragSourceDropTarget 分别有三个参数:

  • type: 拖拽类型,必填
  • spec: 拖拽事件的方法对象,必填。
  • collect: 把拖拽过程中需要信息注入组件的 props,接收两个参数 connect and monitor,必填。

下面约定 source组件 为DragSource包装的组件(本示例为Box.jsx),target组件 为DropTarget包装的组件(本示例为Dustbin.jsx)。

type

当 source组件的type 和 target组件的type 一致时,target组件可以接受source组件

type的类型可以是 string,symbol,也可以是用一个函数来返回该组件的其他 props。

翻译为代码:

// ItemTypes.js 定义类型
export default {
BOX: 'box',
} // Box.jsx
import ItemTypes from './ItemTypes'
@DragSource(ItemTypes.BOX, spec, collect) // Dustbin.jsx
import ItemTypes from './ItemTypes'
@DropTarget(ItemTypes.BOX, spec, collect)

spec

spec定义特定方法的对象,如 source组件的spec 可以定义 拖动 相关的事件,target组件的spec 可以定义 放置 相关的事件,具体列表:

DragSource specObj

  • beginDrag(props, monitor, component): 拖动开始时触发的事件,必须。返回跟props相关的对象。
  • endDrag(props, monitor, component): 拖动结束时触发的事件,可选。
  • canDrag(props, monitor): 当前是否可以拖拽的事件,可选。
  • isDragging(props, monitor): 拖拽时触发的事件,可选。

翻译为代码:

  // Box.jsx
const sourceSpec = {
beginDrag(props, monitor, component){
// 返回需要注入的属性
return {
id: props.id
}
},
endDrag(props, monitor, component){
// ..
},
canDrag(props, monitor){
// ..
},
isDragging(props, monitor){
// ..
}
}
@DragSource(ItemTypes.BOX, sourceSpec, collect)

DropTarget specObj

  • drop(props, monitor, component) 组件放下时触发的事件,可选。
  • hover(props, monitor, component) 组件在DropTarget上方时响应的事件,可选。
  • canDrop(props, monitor) 组件可以被放置时触发的事件,可选。

翻译为代码:

// Dustbin.jsx
const targetSpec = {
drop(props, monitor, component){
// ..
},
hover(props, monitor, component){
// ..
},
canDrop(props, monitor){
// ..
}
}
@DropTarget(ItemTypes.BOX, targetSpec, collect)

specObj 对象方法相关参数

  • props: 组件当前的props
  • monitor:查询当前的拖拽状态,比如当前拖拽的item和它的type,当前拖拽的offsets,当前是否dropped。具体获取方法,参看collect 参数 monitor 部分

  • component:当前组件实例

collect

collect 是一个函数,默认有两个参数:connect 和 monitor。collect函数将返回一个对象,这个对象会注入到组件的 props 中,也就是说,我们可以通过 this.props 获取collect返回的所有属性。

参数 connect

  • source组件 collect 中 connect是 DragSourceConnector的实例,它内置了两个方法:dragSource() 和 dragPreview()dragSource()返回一个方法,将source组件传入这个方法,可以将 source DOM 和 React DnD backend 连接起来;dragPreview() 返回一个方法,你可以传入节点,作为拖拽预览时的角色。
  • target组件 collect 中 connect是 DropTargetConnector的实例,内置的方法 dropTarget() 对应 dragSource(),返回可以将 drop target 和 React DnD backend 连接起来的方法。

翻译为代码:

// Box.jsx
@DragSource(ItemTypes.BOX, sourceSpec,(connect)=>({
connectDragSource: connect.dragSource(),
connectDragPreview: connect.dragPreview(),
}))
export default class Box {
render() {
const { connectDragSource } = this.props
return connectDragSource(
<div>
{
/* ... */
}
</div>,
)
}
} // Dustbin.jsx
@DropTarget(ItemTypes.BOX, targetSpec, (connect)=>{
connectDropTarget: connect.dropTarget(),
})
export default class Dustbin {
render() {
const { connectDropTarget } = this.props
return connectDropTarget(
<div>
{
/* ... */
}
</div>,
)
}
}

参数 monitor

monitor 用于查询当前的拖拽状态,其对应实例内置了很多方法。

内置方法列表:

// DragSourceMonitor
monitor.canDrag() // 是否能被拖拽
monitor.isDragging() // 是否正在拖拽
monitor.getItemType() // 拖拽组件type
monitor.getItem() // 当前拖拽的item
monitor.getDropResult() // 查询drop结果
monitor.didDrop() // source是否已经drop在target
monitor.getInitialClientOffset() // 拖拽组件初始拖拽时offset
monitor.getInitialSourceClientOffset()
monitor.getClientOffset() // 拖拽组件当前offset
monitor.getDifferenceFromInitialOffset() // 当前拖拽offset和初始拖拽offset的差别
monitor.getSourceClientOffset() // DropTargetMonitor
monitor.canDrop() // 是否可被放置
monitor.isOver(options) // source是否在target上方
monitor.getItemType() // 拖拽组件type
monitor.getItem() // 当前拖拽的item
monitor.getDropResult() // 查询drop结果
monitor.didDrop() // source是否已经drop在target
monitor.getInitialClientOffset() // 拖拽组件初始拖拽时offset
monitor.getInitialSourceClientOffset()
monitor.getClientOffset() // 拖拽组件当前offset
monitor.getDifferenceFromInitialOffset() // 当前拖拽offset和初始拖拽offset的差别
monitor.getSourceClientOffset()

具体例子

先草草丢下官方例子和源码:

转自:https://segmentfault.com/a/1190000014723549

强大的拖拽组件:React DnD 的使用的更多相关文章

  1. React拖拽组件Dragact V0.1.7:教你优化React组件性能与手感

    仓库地址:Dragact手感丝滑的拖拽布局组件 预览地址:支持手机端噢- 上回我们说到,Dragact组件已经进行了一系列的性能优化,然而面对大量数据的时候,依旧比较吃力,让我们来看看,优化之前的Dr ...

  2. Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示

    本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...

  3. Vue-Grid-Layout分享一款好用的可拖拽组件

    在使用Grafana的过程中,发现Grafana关于视图页面中每一个面板都可拖拽,可随意放大放小,体验非常棒,F12看了Grafana的代码,看打包后的代码很像react,进一步css,看到有grid ...

  4. Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题

    Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...

  5. vue拖拽组件开发

    vue拖拽组件开发 创建临时vue项目 先查看node和npm版本,怎么安装就不多多bb了 再安装vue-cli npm install vue-cli -g //全局安装 vue-cli 检测是否安 ...

  6. vue-slicksort拖拽组件

    vue-slicksort拖拽组件 安装 通过npm安装 $ npm install vue-slicksort --save 通过yarn安装 $ yarn add vue-slicksort 插件 ...

  7. Vue拖拽组件

    vue开发公众号项目,***产品需要添加一个新的功能.拖拽功能.一听简单.百度上轮子挺多,直接拉一个过来用着就行.然鹅...兴奋之余,却失望至极.东西很多,没有一个能使得.你让我失望,那我就让你绝望. ...

  8. Vue拖拽组件列表实现动态页面配置

    需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...

  9. mp-vue拖拽组件的实现

    作为一个效率还不错的小前端,自己的任务做完之后真的好闲啊,千盼万盼终于盼来了业务的新需求,他要我多加一个排序题,然后用户通过拖拽来排序,项目经理看我是个实习生,说有点复杂做不出来就算了,我这么闲的一个 ...

随机推荐

  1. 【酷】JS+CSS打造沿Y轴纵深运动的3D球体

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  2. Linux Time_wait网络状态 调优

    Time_wait状态 表示收到了对方的FIN报文,并发送出了ACK报文,就等2MSL后即可回到CLOSED可用状态了. 如果FIN_WAIT_1状态下,收到了对方同时带FIN标志和ACK标志的报文时 ...

  3. Loj 6036 「雅礼集训 2017 Day4」编码 - 2-sat

    题目传送门 唯一的传送门 题目大意 给定$n$个串,每个串只包含 ' .问是否可能任意两个不同的串不满足一个是另一个的前缀. 2-sat的是显然的. 枚举每个通配符填0还是1,然后插入Trie树. 对 ...

  4. jquery easyui的应用-2

    有两个版本: freeware edition, commercial edition easyui的 datagrid 实际上是一个table, 其数据来源 通过 url属性来从后台的php页面 获 ...

  5. CMS GC启动参数优化配置

    简介: java启动参数共分为三类: 其一是标准参数(-),所有的JVM实现都必须实现这些参数的功能,而且向后兼容: 其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现 ...

  6. POJ 1390 Blocks(DP + 思维)题解

    题意:有一排颜色的球,每次选择一个球消去,那么这个球所在的同颜色的整段都消去(和消消乐同理),若消去k个,那么得分k*k,问你消完所有球最大得分 思路:显然这里我们直接用二位数组设区间DP行不通,我们 ...

  7. 论文笔记:Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships

    Structure Inference Net: Object Detection Using Scene-Level Context and Instance-Level Relationships ...

  8. A4988和CNC SHIELD使用方法 步进电机

    接线视频 点这看视频 来源 https://www.basemu.com/a4988_pinout_and_how_to_use.html 注意要点 A4988既要12V外部供电,也要5V逻辑供电 我 ...

  9. 初始Vue

    渐进式 JavaScript 框架 通过对框架的了解与运用程度,来决定其在整个项目中的应用范围,最终可以独立以框架方式完成整个web前端项目 走进Vue what -- 什么是Vue 可以独立完成前后 ...

  10. openssl 交叉编译

    建立build文件夹 mkdir build 在build文件夹中建立run.sh文件 cd build touch run.sh chmod 755 run.sh run.sh文件内容如下: #!/ ...