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. 如何创建应用程序包(C ++)

    备注 如果您要创建UWP应用程序包,请参阅使用MakeAppx.exe工具创建应用程序包. 了解如何使用打包API为Windows应用商店应用创建应用包. 如果要手动创建桌面应用程序包,还可以使用使用 ...

  2. 我是如何拿到蚂蚁金服 offer 的 ?

    阅读本文大概需要 5.6 分钟. 作者:翟洪毅 一.梦想和被拒 二.积累 三.结语   首先介绍一下投稿作者 翟洪毅,16年华理计算机本科毕业.在年前拿到了蚂蚁金服Java开发的offer,P6. 工 ...

  3. ios的跨站脚本限制

    概述 项目中碰到一个问题,就是在ios机上,用iframe内嵌的网页有时需要登录,有时候又不需要登录.查找了半天,终于发现是ios的跨站脚本限制导致的.这里就来介绍下跨站脚本限制,供以后开发时参考,相 ...

  4. 用github展示自己的网页要做哪些准备(总结)

    以前,如果想建立一个自己的网站,需要买域名,买存储空间,对个人来说维护成本比较高. 并且很多人只是想有一个网页展示自己的作品或者展示个人的简历. 在github越来越成熟的现在,直接使用github托 ...

  5. 引入外部 CDN失效时--怎么加载本地资源文件(本文以jquery为例)

    相信大家都使用过CDN静态资源库,比如下面 CDN官方静态资源库:https://cdnjs.com/ 七牛前端公开库:http://staticfile.org   (vue,react,nl都有) ...

  6. 机器学习基石笔记:01 The Learning Problem

    原文地址:https://www.jianshu.com/p/bd7cb6c78e5e 什么时候适合用机器学习算法? 存在某种规则/模式,能够使性能提升,比如准确率: 这种规则难以程序化定义,人难以给 ...

  7. Python3+Flask+uwsgi部署

    python3 按照常规的方式安装即可: wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tgz tar zxvf Python-3 ...

  8. RabbitMQ第一次不能正常读取第二次正常的问题

    1.利用rabbitmq导数据包,第一不能正常导入,第二次正常,第三次又出现问题,第四次又恢复正常的坑爹问题. 2.可访问rabbitmq注意消费者个数是否正常 有两个消费者,导致第一次消息被第一个消 ...

  9. 小白都会超详细--ELK日志管理平台搭建教程

    目录 一.介绍 二.安装JDK 三.安装Elasticsearch 四.安装Logstash 五.安装Kibana 六.Kibana简单使用 系统环境:CentOS Linux release 7.4 ...

  10. .NetCore 使用Cookie

    1.首先我们在Startup下面的ConfigureServices中注册授权认证服务以及AddCookie services.AddAuthentication(CookieAuthenticati ...