当前位置: 首页 > news >正文

React 中实现拖拽功能-插件 react-beautiful-dnd

拖拽功能在平时开发中是很常见的,这篇文章主要使用react-beautiful-dnd插件实现此功能。
非常好用,附上GitHub地址:https://github.com/atlassian/react-beautiful-dnd


安装及引入

// 1.引入
# yarn
yarn add react-beautiful-dnd# npm
npm install react-beautiful-dnd --save

具体使用
 

import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd";// 样式相关 代码
const grid = 8;
// 垂直样式
// const getItemStyle = (isDragging, draggableStyle) => ({
//     // some basic styles to make the items look a bit nicer
//     userSelect: "none",
//     padding: grid * 2,
//     margin: `0 0 ${grid}px 0`,
//
//     // change background colour if dragging
//     background: isDragging ? "lightgreen" : "grey",
//
//     // styles we need to apply on draggables
//     ...draggableStyle
// });
// const getListStyle = isDraggingOver => ({
//     background: isDraggingOver ? "lightblue" : "lightgrey",
//     padding: grid,
//     width: 250,
// });// 水平样式
const getItemStyle = (isDragging, draggableStyle) => ({// some basic styles to make the items look a bit niceruserSelect: 'none',padding: grid * 2,margin: `0 ${grid}px 0 0`,// change background colour if draggingbackground: isDragging ? 'lightgreen' : 'grey',// styles we need to apply on draggables...draggableStyle,
});
const getListStyle = isDraggingOver => ({background: isDraggingOver ? 'lightblue' : 'lightgrey',display: 'flex',padding: grid,overflow: 'auto',
});class App extends React.Component {constructor(props) {super(props)this.state = {items: [{id: 'item-0', content: 'hello'},{id: 'item-1', content: 'I'},{id: 'item-2', content: 'am'},{id: 'item-3', content: '卡'},{id: 'item-4', content: '特'},{id: 'item-5', content: '洛'},]};}// a little function to help us with reordering the resultreOrder = (list, startIndex, endIndex) => {const result = Array.from(list);const [removed] = result.splice(startIndex, 1);result.splice(endIndex, 0, removed);return result;};onDragEnd = (result) => {// dropped outside the listif (!result.destination) {return;}const items = this.reOrder(this.state.items,result.source.index,result.destination.index);this.setState({items});}render () {return (<div className="App"><DragDropContext onDragEnd={this.onDragEnd}><Droppable droppableId="droppable" direction="horizontal">{(provided, snapshot) => (<divref={provided.innerRef}style={getListStyle(snapshot.isDraggingOver)}{...provided.droppableProps}>{this.state.items.map((item, index) => (<Draggable key={item.id} draggableId={item.id} index={index}>{(provided, snapshot) => (<divref={provided.innerRef}{...provided.draggableProps}{...provided.dragHandleProps}style={getItemStyle(snapshot.isDragging,provided.draggableProps.style)}>{item.content}</div>)}</Draggable>))}{provided.placeholder}</div>)}</Droppable></DragDropContext></div>);}
}export default App;

说明一下:<Droppable />中的 direction 属性可以控制是水平方向还是垂直方向,配合相关 getItemStyle 和 getListStyle 的代码,可做到。
效果展示

补充一下: 如果你是react-creat-app 创建的项目,则需要删除代码里自带的react 严格模式。否则拖拽效果出不来。

http://www.lryc.cn/news/295094.html

相关文章:

  • golang 引入swagger(iris、gin)
  • Java开发IntelliJ IDEA2023
  • LeetCode LCP 30.魔塔游戏:贪心(优先队列)
  • Oracle的权限
  • 20240206三次握手四次挥手
  • Navicat的使用教程,操作详解
  • Git―基本操作
  • 【PostgreSQL内核学习(二十六) —— (共享数据缓冲区)】
  • word调整论文格式的记录
  • android.MediaMuxer时间裁剪
  • 【蓝桥杯选拔赛真题91】Scratch筛选数据 第十五届蓝桥杯scratch图形化编程 少儿编程创意编程选拔赛真题解析
  • 英语学习——16组英语常用短语
  • unity 增加系统时间显示、FPS帧率、ms延迟
  • 【Python基础】文件详解(文件基础、csv文件、时间处理、目录处理、excel文件、jsonpicke、ini配置文件)
  • [UI5 常用控件] 05.FlexBox, VBox,HBox,HorizontalLayout,VerticalLayout
  • Unity类银河恶魔城学习记录1-14 AttackDirection源代码 P41
  • DataX详解和架构介绍
  • 02.05
  • 【C语言】贪吃蛇 详解
  • Mysql MGR搭建
  • 新火种AI|寒武纪跌落神坛!七年连亏50亿,AI芯片第一股不行了吗?
  • three.js CSS3DObject、CSS2DObject、CSS3DSprite、Sprite的作为标签的区别
  • 第7节、双电机直线运动【51单片机+L298N步进电机系列教程】
  • 【C语言 - 哈希表 - 力扣 - 相交链表】
  • C++参悟:内存管理-unique_ptr
  • 【征稿已开启】第五大数据、人工智能与软件工程国际研讨会(ICBASE 2024)
  • Vue3父子组件传参
  • SpringBoot整理-微服务
  • 服务器和CDN推荐
  • c#读取csv文件中的某一列的数据