react-dnd實(shí)現(xiàn)拖拽效果

最終效果

拖拽.gif

幾個(gè)概念

  • DragSource 用于包裝你需要拖動(dòng)的組件,使組件能夠被拖拽(make it draggable)
  • DropTarget 用于包裝接收拖拽元素的組件,使組件能夠放置(dropped on it)
  • DragDropContext 用于包裝拖拽根組件,DragSource 和 DropTarget 都需要包裹在DragDropContext內(nèi)

參數(shù)說(shuō)明

 DragSource(type, spec, collect)
 DropTarget (type, spec, collect)
  • DragSource和DropTarget各有三個(gè)參數(shù)

type: 拖拽類(lèi)型,必填。當(dāng) source組件的type 和 target組件的type 一致時(shí),target組件可以接受source組件。
spec: 拖拽事件的方法對(duì)象,必填。
collect: 把拖拽過(guò)程中需要信息注入組件的 props,接收兩個(gè)參數(shù) connect and monitor,必填。

具體實(shí)現(xiàn)步驟

  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

// 設(shè)置分析指標(biāo)彈層:拖拽源
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. 拖拽目標(biāo)組件

拖拽目標(biāo)組件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>

拖拽目標(biāo)組件js

// 拖拽到的目標(biāo)組件
const chooseListTarget = {
  drop(props, monitor) {
    props.onDrop(monitor.getItem());
  }
};

// 設(shè)置分析指標(biāo)彈層:拖拽x軸接受區(qū)域組件
@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軸選中內(nèi)容
  @autobind
  deleteXData() {
    this.props.delXData();
  }

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

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

使用DragDropContext包裹根組件
拖拽結(jié)束后交互處理

  • html
...
<AxisSourceItem type="bandsDragDrop" data="{this}"/>
...
<AxisXTargetArea accepts="bandsDragDrop" onDrop="{'bandChartX' | onDrop}" data="{dragBandChartX}" delXData="{delXData}"/>
...
  • js
@registerTmpl("BandDiagnostic")
@inject("store")
@DragDropContext(HTML5Backend) // 將根組件用拖拽組件包起來(lái)
@observer
class BandDiagnostic extends Component {
  ...
  //拖動(dòng)結(jié)束后的操作
  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,
          }
        });
      }
    };
  }
  ...
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 孤蟬獨(dú)鳴翹北松,沉香不在瘦花叢。 小樓燭火掩垂簾,獨(dú)上穹廬對(duì)月空。 風(fēng)華自飲不知憶,瑤軫千緒思不同。 芳箋幾許解花...
    清遠(yuǎn)塵閱讀 760評(píng)論 88 33
  • du?du?du
    五月清晨的文明少年閱讀 999評(píng)論 0 0

友情鏈接更多精彩內(nèi)容