import React, { Component } from 'react';
import './App.css';
import Card from './Card';
import HTML5Backend from 'react-dnd-html5-backend'
import { DragDropContext } from 'react-dnd'
// const update = require('immutability-helper');
import update from 'react-addons-update'; class App extends Component {
state = {
cards: [
{
id: 1,
text: 'Write a cool JS library',
},
{
id: 2,
text: 'Make it generic enough',
},
{
id: 3,
text: 'Write README',
},
{
id: 4,
text: 'Create some examples',
},
{
id: 5,
text:
'Spam in Twitter and IRC to promote it (note that this element is taller than the others)',
},
{
id: 6,
text: '???',
},
{
id: 7,
text: 'PROFIT',
},
],
} deleteItem = id => {
this.setState(prevState => {
return {
items: prevState.items.filter(item => item.id !== id)
}
})
} moveCard = (dragIndex, hoverIndex) => {
const { cards } = this.state
const dragCard = cards[dragIndex] this.setState(
update(this.state, {
cards: {
$splice: [[dragIndex, 1], [hoverIndex, 0, dragCard]],
},
}),
() => {
console.log(this.state.cards);
})
} render() {
return (
<div className="App">
<header className="App-header">
{/* <img src={logo} className="App-logo" alt="logo" /> */}
<h1 className="App-title">Welcome to React</h1>
</header>
<div className="App-intro">
<div className="app-container">
<div className="item-container">
{/* {this.state.items.map((item, index) => (
<Item key={item.id} item={item} handleDrop={(id) => this.deleteItem(id)} />
))} */}
</div> {/* <Target /> */}
</div>
<div className="card-container">
{this.state.cards.map((card, i) => (
<Card
key={card.id}
index={i}
id={card.id}
text={card.text}
moveCard={this.moveCard}
/>
))}
</div>
</div>
</div>
);
}
} export default DragDropContext(HTML5Backend)(App);

card

import React from 'react';
import PropTypes from 'prop-types';
import { findDOMNode } from 'react-dom';
import {
DragSource,
DropTarget,
ConnectDropTarget,
ConnectDragSource,
DropTargetMonitor,
DropTargetConnector,
DragSourceConnector,
DragSourceMonitor,
} from 'react-dnd';
import { XYCoord } from 'dnd-core';
import flow from 'lodash/flow'; const style = {
border: '1px dashed gray',
padding: '0.5rem 1rem',
marginBottom: '.5rem',
backgroundColor: 'white',
cursor: 'move',
}; const cardSource = {
beginDrag(props) {
return {
id: props.id,
index: props.index,
}
},
}; const cardTarget = {
hover(props, monitor, component) {
const dragIndex = monitor.getItem().index
const hoverIndex = props.index // Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
} // Determine rectangle on screen
const hoverBoundingRect = (findDOMNode(
component,
)).getBoundingClientRect(); // Get vertical middle
const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2; // Determine mouse position
const clientOffset = monitor.getClientOffset(); // Get pixels to the top
const hoverClientY = (clientOffset).y - hoverBoundingRect.top; // Only perform the move when the mouse has crossed half of the items height
// When dragging downwards, only move when the cursor is below 50%
// When dragging upwards, only move when the cursor is above 50%
// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
return;
} // Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
return;
} // Time to actually perform the action
props.moveCard(dragIndex, hoverIndex); // Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;
},
} class Card extends React.Component {
static propTypes = {
connectDragSource: PropTypes.func.isRequired,
connectDropTarget: PropTypes.func.isRequired,
index: PropTypes.number.isRequired,
isDragging: PropTypes.bool.isRequired,
id: PropTypes.any.isRequired,
text: PropTypes.string.isRequired,
moveCard: PropTypes.func.isRequired,
} render() {
const {
text,
isDragging,
connectDragSource,
connectDropTarget,
} = this.props;
const opacity = isDragging ? 0 : 1; return (
connectDragSource &&
connectDropTarget &&
connectDragSource(
connectDropTarget(<div style={{ ...style, opacity }}>{text}</div>),
)
);
}
} export default flow(
DragSource(
'card',
cardSource,
(connect, monitor) => ({
connectDragSource: connect.dragSource(),
isDragging: monitor.isDragging(),
}),
),
DropTarget('card', cardTarget, (connect) => ({
connectDropTarget: connect.dropTarget(),
}))
)(Card);

react dnd demo2的更多相关文章

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

    强大的拖拽组件:React DnD 的使用 react.js 10.6k 次阅读  ·  读完需要 25 分钟 17 文章首发我的个人blog : 原文链接 学习 React DnD 的最初原因是阅读 ...

  2. react dnd demo

    target import React ,{ Component } from 'react'; import { DropTarget } from 'react-dnd'; import Item ...

  3. 六、React 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定

    接:https://www.cnblogs.com/chenxi188/p/11782349.html 事件对象 .键盘事件. 表单事件 .ref获取dom节点.React实现类似vue双向数据绑定 ...

  4. HTML5 drag & drop & H5 DnD

    HTML5 drag & drop H5 DnD https://html5demos.com/ demos https://html5demos.com/dnd-upload https:/ ...

  5. 13个精选的React JS框架

    如果你正在使用 React.js 或 React Native 创建用户界面,可以试一试本文推荐的这些框架. React.js 和 React Native 是流行的用户界面(UI)开发平台,且都是开 ...

  6. React的生命周期函数

    概述 在React中,生命周期函数指的是组件在某一个时刻会自动执行的函数 constructor 在类或组件创建的时候被自动执行,我们可以说它是生命周期函数,但它并不是React所特有的,所有的Es6 ...

  7. react-dnd使用介绍

    核心API 想要灵活使用,就先知道几个核心API DragSource 用于包装你需要拖动的组件,使组件能够被拖拽(make it draggable) DropTarget 用于包装接收拖拽元素的组 ...

  8. react-dnd 拖拽

    介绍 React DnD 是一组 React 高阶组件,可以用来帮你构建复杂的拖拽接口,同时解耦你的组件.React DnD 非常适合像 Trello 和 Storify 这样的应用,在不同地方通过拖 ...

  9. 学习 React(jsx语法) + es2015 + babel + webpack

    视频学习地址: http://www.jtthink.com/course/play/575 官方地址 https://facebook.github.io/react/ 神坑: 1.每次this.s ...

随机推荐

  1. Objective-C 学习 (一):Objective-C 概述

    1. OC在C的基础上新增了一些面向对象的语法,将C的复杂的.繁琐的语法封装的更为简单.且OC完全兼容C语言. 2. OC程序的源文件的后缀名是.m, m 代表message,代表OC中最重要的一个机 ...

  2. JAVA之enum类详解

    目录    一.简介    二.默认枚举类    三.多值枚举类    四.属性和方法    五.构造函数    六.重要方法    七.引用参考 一.简介    1.枚举类代表一组常量:    2. ...

  3. 查找第三方银行官方app下载链接探索过程

    需求:最近有个需求,点击按钮,弹出一个所需银行选项的非全屏弹出层,再点击某银行选项,随即跳转到该银行的app下载界面,如下图所示           注:这里只是引用相关银行的链接,不需要做什么逻辑处 ...

  4. 【机器学习】--FP-groupth算法从初始到应用

    一.前述 二.构建FP_groupth数流程 1.扫描事务数据库D 一次.收集频繁项的集合F 和它们的支持度.对F 按支持度降序排序,结果为频繁项表L. 2.创建FP 树的根节点,以“null”标记它 ...

  5. Java设计模式系列-抽象工厂模式

    原创文章,转载请标注出处:https://www.cnblogs.com/V1haoge/p/10755412.html 一.概述 抽象工厂模式是对工厂方法模式的再升级,但是二者面对的场景稍显差别. ...

  6. SpringBoot实用小技巧之动态设置SpringBoot日志级别

    有时线上问题我们用打日志的方式来观察错误或埋点参数,但由于这些日志如果都打出来会占用大量存储空间而且覆盖了一些有效信息,所以线上级别一般设置INFO,调试级别用作特殊情况下.此时如果线上想查看调试级别 ...

  7. Asp.Net Core微服务再体验

    ASP.Net Core的基本配置 .在VS中调试的时候有很多修改Web应用运行端口的方法.但是在开发.调试微服务应用的时候可能需要同时在不同端口上开启多个服务器的实例,因此下面主要看看如何通过命令行 ...

  8. .Net Framework项目引用.NetStandard标准库出现版本冲突解决办法

    今天在工作中出现一个引用问题,害我找问题找了很久.起因是在一个Winform项目下需要引用一个.NetStandard标准库,标准库引用了System.ComponentModel.Annotatio ...

  9. select标签 禁止选择但又能通过序列化form表单传值到后台

    前言 项目开发中,我们可能会碰到这样的需求:select标签,禁止选择但又能通过序列化form表单传值到后台,但是当我们使用disabled="disabled"时发现,无法序列化 ...

  10. C#面向对象(1)

    一.面向对象(OOP) 面向过程 面向过程就是分析出解决问题的所需要的步骤,然后每个步骤使用函数实现,使用时将函数依次调用即可 C语言 面向对象 对象:生活中真实存在的事物(电脑.手机.草.树.... ...