美文网首页
react-dnd实现拖拽效果

react-dnd实现拖拽效果

作者: 亲亲美人鱼1 | 来源:发表于2018-06-20 12:36 被阅读0次

最终效果

拖拽.gif

几个概念

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

参数说明

 DragSource(type, spec, collect)
 DropTarget (type, spec, collect)
  • DragSource和DropTarget各有三个参数

type: 拖拽类型,必填。当 source组件的type 和 target组件的type 一致时,target组件可以接受source组件。
spec: 拖拽事件的方法对象,必填。
collect: 把拖拽过程中需要信息注入组件的 props,接收两个参数 connect and monitor,必填。

具体实现步骤

  1. 引入文件
import HTML5Backend from 'react-dnd-html5-backend';
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
  1. 拖拽源组件

拖拽源组件html

<template name="AxisSourceItem">
  <div class="{styles.btnWrap}"><ant-Button type="primary" size="small" disabled="{disabled}">{data.name}<ant-Icon type="plus" /></ant-Button></div>
</template>

拖拽源组件js

// 设置分析指标弹层:拖拽源
const axisListSource = {
  beginDrag(props) {
    return {
      data: props.data,
    };
  }
};

@registerTmpl('AxisSourceItem')
@DragSource(props => props.type, axisListSource, (connect, monitor) => ({
  connectDragSource: connect.dragSource(),
  isDragging: monitor.isDragging(),
}))
@inject('store')
@observer
class AxisSourceItem extends Component {
  render() {
    const { connectDragSource } = this.props;

    return connectDragSource(tmpls.AxisSourceItem(this.props, {
      styles,
      disabled: this.props.data.status === 1 ? false : true,
    }));
  }
}
  1. 拖拽目标组件

拖拽目标组件html

<template name="AxisXTargetArea">
  <span class="{styles.targetArea}">
    <#if {data.name}>
      <ant-Button type="primary" size="small">{data.name}</ant-Button><ant-Icon type="minus" onClick="{deleteXData}"/>
    </#if>
  </span>
</template>

拖拽目标组件js

// 拖拽到的目标组件
const chooseListTarget = {
  drop(props, monitor) {
    props.onDrop(monitor.getItem());
  }
};

// 设置分析指标弹层:拖拽x轴接受区域组件
@registerTmpl('AxisXTargetArea')
@DropTarget(props => props.accepts, chooseListTarget, (connect, monitor) => ({
  connectDropTarget: connect.dropTarget(),
  isOver: monitor.isOver(),
  canDrop: monitor.canDrop(),
}))
@inject('store')
@observer
class AxisXTargetArea extends Component {
  constructor() {
    super();
  }

  // 删除x轴选中内容
  @autobind
  deleteXData() {
    this.props.delXData();
  }

  render() {
    const { connectDropTarget } = this.props;

    return connectDropTarget(tmpls.AxisXTargetArea(this.state, this.props, this, {
      styles,
    }));
  }
}
  1. 拖拽组件在根组件中的使用

使用DragDropContext包裹根组件
拖拽结束后交互处理

  • html
...
<AxisSourceItem type="bandsDragDrop" data="{this}"/>
...
<AxisXTargetArea accepts="bandsDragDrop" onDrop="{'bandChartX' | onDrop}" data="{dragBandChartX}" delXData="{delXData}"/>
...
  • js
@registerTmpl("BandDiagnostic")
@inject("store")
@DragDropContext(HTML5Backend) // 将根组件用拖拽组件包起来
@observer
class BandDiagnostic extends Component {
  ...
  //拖动结束后的操作
  onDrop(index) {
    return (item) => {
      if (index === 'bandChartX') {
        this.setState({
          dragBandChartX: {
            id: item.data.code,
            name: item.data.name,
          }
        });
      } else if (index === 'bandChartY') {
        this.setState({
          dragBandChartY: {
            id: item.data.code,
            name: item.data.name,
          }
        });
      }
    };
  }
  ...
}

相关文章

  • react-dnd实现拖拽效果

    最终效果 几个概念 DragSource 用于包装你需要拖动的组件,使组件能够被拖拽(make it dragga...

  • react项目中使用react-dnd实现列表的拖拽排序

    现在有一个新需求就是需要对一个列表,实现拖拽排序的功能,要实现的效果如下图:​ 可以通过 react-dnd 或者...

  • 使用 immutability-helper 更好的更新复杂数据

    最近在用 React-Dnd 做拖拽效果的时候,顺带学习到了 immutability-helper 这个库。我觉...

  • react.js 拖拽

    react.js拖拽排序功能的实现 1.使用 react-dnd npm install--save react-...

  • 拖拽API

    实现拖拽效果 目前实现拖拽效果 HTML5拖拽 源元素事件例子 目标元素事件 从本地拖放图片到页面中 实现拖拽

  • HTML5实现拖拽

    实现拖拽效果源元素 - 要拖拽的文件目标元素 - 要拖拽到哪里去 目前实现拖拽效果使用原生DOM就能实现 - 最麻...

  • HTML5拖拽drag

    通过拖拽实现页面元素的位置改变 实现拖拽效果 源元素 - 要拖拽的文件 目标元素 - 要拖拽到哪里去 目前实现拖拽...

  • 使用 react-beautiful-dnd 快速实现可拖拽看板

    当我还在使用 react-dnd 设计拖拽逻辑和交互、当我还在为计算拖拽元素和 hover 元素的位置坐标而烦躁不...

  • 原生拖拽,拖放事件(drag and drop)

    拖拽,拖放事件可以通过拖拽实现数据传递,达到良好的交互效果,如:从操作系统拖拽文件实现文件选择,拖拽实现元素布局的...

  • Flutter 仿QQ消息拖拽(贝塞尔曲线实现)

    Flutter 仿qq消息拖拽效果通过自定义view实现,效果如下 如果要实现最终的效果,首先绘制拖拽连接处的线(...

网友评论

      本文标题:react-dnd实现拖拽效果

      本文链接:https://www.haomeiwen.com/subject/csntyftx.html