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. 数据库 【redis】 命令大全

    以下纯属搬砖,我用Python抓取的redis命令列表页内容 如果想看命令的具体使用可查去官网查看,以下整理为个人查找方便而已 地理位置GEOADD 将指定的地理空间位置(纬度.经度.名称)添加到指定 ...

  2. Vue番外篇 -- vue-router浅析原理

    近期被问到一个问题,在你们项目中使用的是Vue的SPA(单页面)还是Vue的多页面设计? 这篇文章主要围绕Vue的SPA单页面设计展开. 关于如何展开Vue多页面设计请点击查看. 官网vue-rout ...

  3. Ansible安装及简单使用备注

    1.安装epel源: rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm 2.安装: yum ...

  4. 一篇读懂HTTPS:加密原理、安全逻辑、数字证书等

    1.引言 HTTPS(全称: Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.本文,就来深入介绍下其 ...

  5. 学python走过的坑一 类的实例化

    运行结果:line 21, in <module>  s=speaker('ken',10,'aaa')TypeError: __init__() missing 1 required p ...

  6. 音频处理EQ的基本概念

     我们通常所说的人声,歌声以及乐声都是一个复合音,也就是由声音的基音和一系列的泛音所构成的.这些泛音都是基音频率的倍数,物理学中叫分音,电声学中叫谐波,音乐中则把它们称做泛音.可以说,泛音对音色的特性 ...

  7. Jenkins~配合Docker及dotnetCore进行生产和测试环境的灵活部署

    回到目录 首先要清楚本文是讲dotnetcore项目在生产和测试环境部署的,这在过去的frameworks项目里,我们可以通过设置web.config的环境变量,然后再发布时指定具体的变量,去实现生产 ...

  8. Mybatis+mysql批量插入性能分析测试

    前言 今天在网上看到一篇文章(后文中的文章指的就是它) https://www.jianshu.com/p/cce617be9f9e 发现了一种有关于mybatis批量插入的新方法,而且看了文章发现我 ...

  9. 运输问题的+Leapms模型

    运输问题的+Leapms模型 运输问题是本科教课书中的一个经典章节.运输问题的线性规划模型非常简单,而且求解难度极小. 问题 一个公司生产并销售一种产品.该公司有m个产地.n个销地.产地 i 的供给量 ...

  10. 补习系列(15)-springboot 分布式会话原理

    目录 一.背景 二.SpringBoot 分布式会话 三.样例程序 四.原理进阶 A. 序列化 B. 会话代理 C. 数据老化 小结 一.背景 在 补习系列(3)-springboot 几种scope ...