react拖拽react-beautiful-dnd,一维数组,二维数组

写在前边,二维数组可以拖拽,但是不可以编辑+拖拽,如果想要实现编辑+拖拽,还是需要转换成一维数组。原因是因为插件的官方规定,在拖拽过程中不可以编辑Droppable层的Props。

 

相关地址:

中文文档地址

react-beautiful-dnd - 《react-beautiful-dnd 中文文档帮助手册教程》 - 极客文档 (geekdaxue.co)

git源码

GitHub - chinanf-boy/react-beautiful-dnd-zh: 🇨🇳翻译: react-beautiful-dnd 文档 ❤️ 更新 ✅

使用

安装

# yarn
yarn add react-beautiful-dnd

# npm
npm install react-beautiful-dnd --save

引入

import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';

引用

<DraggableList data={listDemo}></DraggableList>

一维数组使用

传参data是一维数组

import React, { useEffect, useState } from 'react';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';interface Props {data: any[];
}
const DraggableList: React.FC<Props> = ({ data }) => {const [sortList, setSortList] = useState([]);const getItems = () => {const arr = Array.from({ length: 10 }, (v, k) => k).map((k) => ({id: `item-${k}`,content: `item ${k}`,}));setSortList(arr);};useEffect(() => {getItems();}, []);const grid = 8;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: any) => ({background: isDraggingOver ? 'lightblue' : 'lightgrey',display: 'flex',padding: grid,overflow: 'auto',});const onDragEnd = (result) => {if (!result.destination) {return;}console.log('end', sortList, result);const res = sortList.filter((item) => item); // 更改引用地址console.log('移动前res', res);const [removed] = res.splice(result.source.index, 1);console.log('删除???', removed);res.splice(result.destination.index, 0, removed);console.log('添加后', res);setSortList(res);};console.log('data', data);/*** Draggable组件可以拖动并拖放到其Droppables上. 一个Draggable必须始终包含在一个Droppable.* 它是 可能重新排序Draggable在其Droppable家中或移动到另一个Droppable.* 一个Draggable必须包含在一个Droppable.* */return (<DragDropContext onDragEnd={onDragEnd}><Droppable droppableId="droppable" direction="horizontal">{(provided, snapshot) => (<div ref={provided.innerRef} style={getListStyle(snapshot.isDraggingOver)} {...provided.droppableProps}>{sortList.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>);
};
export default DraggableList;

二维数组的使用

import React, { useEffect, useState } from 'react';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';interface Props {data: any[];
}
const DraggableList: React.FC<Props> = ({ data = [] }) => {const [sortList, setSortList] = useState(data);const grid = 8;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',});const onDragEnd = (result) => {if (!result.destination) {return;}console.log('end', sortList, result);const res = sortList.filter((item) => item); //修改引用地址console.log('res', res);const [removed] = res.splice(result.source.index, 1);console.log('删除???', removed);res.splice(result.destination.index, 0, removed);console.log('添加后', res);setSortList(res);};useEffect(() => {setSortList(data);}, [data]);console.log('data', data);return (<DragDropContext onDragEnd={onDragEnd}>{sortList.map((item, index) => {return (<Droppable droppableId={'droppable' + index} key={index} direction="vertical">{(provided, snapshot) => (<div ref={provided.innerRef} style={getListStyle(snapshot.isDraggingOver)} {...provided.droppableProps}>{/*{data.map((item, index) => (*/}<Draggable key={item[0].value} draggableId={item[0].value} index={index}>{(provided, snapshot) => (<divref={provided.innerRef}{...provided.draggableProps}{...provided.dragHandleProps}style={getItemStyle(snapshot.isDragging, provided.draggableProps.style)}>{66666 + item[0].label}</div>)}</Draggable>{/*))}*/}{provided.placeholder}</div>)}</Droppable>);})}</DragDropContext>);
};
export default DraggableList;

组件传值的数组内容 

  const [options, setOptions] = useState([{label: '延时时间',value: 'delayTime',children: [{label: '时',value: 'hour',disabled: false,},{label: '分',value: 'minute',disabled: false,},{label: '秒',value: 'second',disabled: false,},],},{label: '限制类型',value: 'limitType',children: [{label: '前置点位1',value: '1',disabled: false,},{label: '前置点位2',value: '2',disabled: false,},{label: '前置点位3',value: '3',disabled: false,},],},{label: '温度',value: 'templete',},]);

案列

案例是通过级联的组件选择条件,新增条件时,前端重新定义数据格式,将二维的结构改成一维数组的结构。遍历填充内容时,是在Droppable的下一级,所以可以修改内容。

  const onDispatchValue = (res: any) => {dispatch({type: `${MODEL_NAME}/save`,payload: {proTypeList: res,},});}; // 新增、删除前置条件const [inputFlag, setInputFlag] = useState(false);const [listDemo, setListDemo] = useState([]);const changeCondition = (ids, option) => {let arr2 = [];// 第三层关系选中两个时的判断if (ids && ids.length > 1) {// 二维数组结构成一维数组,方便去重arr2 = ids.reduce((a, b) => {return a.concat(b);});const arr3 = Array.from(new Set(arr2));if (arr2.length !== arr3.length) {setRepeatFlag(true);return message.warning('前置条件重复,请删除!');} else {setRepeatFlag(false);}}// 没有子级或者全选的判断ids.map((item, index) => {if (item.length === 1 && option[index][0].value === item[0] && option[index][0]?.children?.length > 0) {setRepeatFlag(true);return message.warning('前置条件重复,请删除!');} else {setRepeatFlag(false);}});const arr = option.map((item) => {let obj = {typeName: '', // 类型名称typeValue: '', // 类型idunitName: '', // 单位名称unitValue: '', // 单位idvalue: '', // 值};item.map((i, index) => {if (item.length === 1) {obj.typeName = i.label;obj.typeValue = i.value;}if (item.length === 2) {if (index === 1) {obj.unitName = i.label;obj.unitValue = i.value;} else {obj.typeName = i.label;obj.typeValue = i.value;}}});return obj;});setListDemo(arr);// 保存定义好的数据,用于组件之间传值onDispatchValue(arr);};
// 父组件引用
<DraggableList data={proTypeList}></DraggableList>
// 子组件
import { ConnectState } from '@/typing/connect';
import { connect } from '@@/exports';
import { Input } from 'antd';
import React, { useEffect, useState } from 'react';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import { Dispatch } from 'umi';
interface Props {data: any[];dispatch: Dispatch;
}const MODEL_NAME = 'mainConfig';
const DraggableList: React.FC<Props> = ({ data = [], dispatch }) => {const [sortList, setSortList] = useState(data);// 拖拽时的样式const getListStyle = () => ({overflow: 'auto',width: '100%',});// 拖拽后的样式const getItemStyle = (isDragging, draggableStyle) => ({// some basic styles to make the items look a bit nicerwidth: '100%',userSelect: 'none',...draggableStyle,});const onDragEnd = (result) => {if (!result.destination) {return;}const res = sortList.filter((item) => item); //修改引用地址const [removed] = res.splice(result.source.index, 1);res.splice(result.destination.index, 0, removed);// console.log('添加后', res);setSortList(res);dispatch({type: `${MODEL_NAME}/save`,payload: {proTypeList: res,},});console.log('拖拽后', res);};// 校验输入框内容const regInputValue = (e: any, index: number) => {// 输入框聚焦时const arr = data.filter((item) => item);arr[index].value = e.target.value;console.log('arr', arr);setSortList(arr);dispatch({type: `${MODEL_NAME}/save`,payload: {proTypeList: arr,},});};useEffect(() => {setSortList(data);}, [data]);// console.log('弹窗起落data', data);/*** Draggable组件可以拖动并拖放到其Droppables上. 一个Draggable必须始终包含在一个Droppable.* 它是 可能重新排序Draggable在其Droppable家中或移动到另一个Droppable.* 一个Draggable必须包含在一个Droppable.* */return (<DragDropContext onDragEnd={onDragEnd}><Droppable droppableId="droppable" direction="horizontal">{(provided, snapshot) => (<div ref={provided.innerRef} style={getListStyle(snapshot.isDraggingOver)} {...provided.droppableProps}>{data.map((item, index) => (<Draggable key={item.typeValue} draggableId={item.typeValue} index={index}>{(provided, snapshot) => (<divref={provided.innerRef}{...provided.draggableProps}{...provided.dragHandleProps}style={getItemStyle(snapshot.isDragging, provided.draggableProps.style)}><div style={{ width: '100%', display: 'flex', justifyContent: 'flex-start', textAlign: 'center' }}><div style={{ width: '33%', backgroundColor: '#f2f2f2', padding: '8px 0' }}>条件名称</div><div style={{ width: '33%', backgroundColor: '#f2f2f2', padding: '8px 0' }}>条件值</div><div style={{ width: '33%', backgroundColor: '#f2f2f2', padding: '8px 0' }}>单位名称</div></div><divstyle={{width: '100%',display: 'flex',justifyContent: 'flex-start',padding: '6px',textAlign: 'center',marginBottom: 16,}}><div style={{ width: '33%', padding: '8px 0' }}>{item.typeName}</div><div style={{ width: '33%', padding: '8px 0' }}><Inputplaceholder="请输入内容"onChange={(e) => {regInputValue(e, index);}}/></div><div style={{ width: '33%', padding: '8px 0' }}>{item.unitName}</div></div></div>)}</Draggable>))}{provided.placeholder}</div>)}</Droppable></DragDropContext>);
};
export default connect(({ mainConfig }: ConnectState) => ({mainConfig ,
}))(DraggableList);

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/285041.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

surface go 2简单的配置

1.基本的配置信息 cpu 4425Y 感觉还是比较的弱 但是处理基本的网页浏览或收发电子邮件还是很不错的 2. C:\Users\win>systeminfo 主机名: DESKTOP-F5TT6HJ OS 名称: Microsoft Windows 10 专业版 OS 版本: 10.0.19045 暂缺 Build 19045 …

WordCount案例实操

文章目录 需求分析代码环境准备编写Mapper类编写Reducer类编写Driver驱动类本地运行注意事项运行结果 提交到集群测试 需求分析 在给定的文本文件中统计输出每一个单词出现的总次数 期望输出数据&#xff1a; atxiaoyu 2 banzhang 1 cls 2 hadoop 1 jiao 1 ss 2 xue 1 实现过…

智慧物联-能源分析平台

物联能源分析平台是为了满足企业对能源管理和节能减排的需求而开发的一套在线平台。随着能源问题日益凸显&#xff0c;企业对能源的使用和管理面临着越来越大的挑战。因此&#xff0c;开发一个能够帮助企业实时监测、分析和优化能源消耗的平台变得尤为重要。 随着工业化和城市…

nodejs+vue高校奖助学金系统python-flask-django-php

高校奖助学金系统的目的是让使用者可以更方便的将人、设备和场景更立体的连接在一起。能让用户以更科幻的方式使用产品&#xff0c;体验高科技时代带给人们的方便&#xff0c;同时也能让用户体会到与以往常规产品不同的体验风格。 与安卓&#xff0c;iOS相比较起来&#xff0c;…

基于spring boot的个人博客系统的设计与实现(带源码)

随着国内市场经济这几十年来的蓬勃发展&#xff0c;突然遇到了从国外传入国内的互联网技术&#xff0c;互联网产业从开始的群众不信任&#xff0c;到现在的离不开&#xff0c;中间经历了很多挫折。本次开发的个人博客系统&#xff0c;有管理员&#xff0c;用户&#xff0c;博主…

人工智能之Tensorflow批标准化

批标准化&#xff08;Batch Normalization,BN&#xff09;是为了克服神经网络层数加深导致难以训练而诞生的。 随着神经网络的深度加深&#xff0c;训练会越来越困难&#xff0c;收敛速度会很慢&#xff0c;常常会导致梯度消失问题。梯度消失问题是在神经网络中&#xff0c;当前…

STM32学习笔记(5_2)- EXTI外部中断代码

无人问津也好&#xff0c;技不如人也罢&#xff0c;都应静下心来&#xff0c;去做该做的事。 最近在学STM32&#xff0c;所以也开贴记录一下主要内容&#xff0c;省的过目即忘。视频教程为江科大&#xff08;改名江协科技&#xff09;&#xff0c;网站jiangxiekeji.com 本期介…

STM32学习笔记(6_2)- TIM定时器中断和定时器内外时钟源选择代码

无人问津也好&#xff0c;技不如人也罢&#xff0c;都应静下心来&#xff0c;去做该做的事。 最近在学STM32&#xff0c;所以也开贴记录一下主要内容&#xff0c;省的过目即忘。视频教程为江科大&#xff08;改名江协科技&#xff09;&#xff0c;网站jiangxiekeji.com 现在开…

网络核心知识点 - 网络通信技术 XHR(XMLHttpRequest) 和 Fetch

一、关于 AJAX&#xff08;一种思想和方法&#xff09; 浏览器本身就具备网络通信的能力&#xff0c;但在早期&#xff0c;浏览器并没有把这个能力开放给JS。最早是微软在IE浏览器中把这一能力向JS开放&#xff0c;让JS可以在代码中实现发送请求&#xff0c;并不会刷新页面。Aj…

道路与航线

一道类似缩点的好题&#xff0c;先按道路缩点 然后将缩点以后的图按照航线做DAG 在DAG上先跑topsort 在每一个团内部跑dijkstra&#xff0c;同时更新top点 很有意思的一道题目 #include<bits/stdc.h> using namespace std; using ll long long; const int N 3e510; co…

python—接口编写部分

最近准备整理一下之前学过的前端小程序知识笔记&#xff0c;形成合集。顺便准备学一学接口部分&#xff0c;希望自己能成为一个全栈嘿嘿。建议关注收藏&#xff0c;持续更新技术文档。 目录 前端知识技能树http请求浏览器缓存 后端知识技能树python_api&#xff1a;flaskflask…

【Node.js】zlib

gzip 和 deflate 的基本使用 const zlib require("zlib"); const fs require(fs)// 压缩 1. createGzip .gz 2. createDeflate .deflate // const readStream fs.createReadStream(index.txt) // const writeStream fs.createWriteStream(index.txt.gz) // rea…

IDEA, Pycharm, Goland控制台乱码

IDEA, Pycharm, Goland控制台乱码 问题描述: 控制台出现&#xfffd;&#xfffd;&#xfffd;&#xfffd;等乱码 复现频率: 总是 解决方案: 以IDEA为例 添加 -Dfile.encodingUTF-8位置 idea64.exe.vmoptions 在安装idea的bin目录idea.vmoptions idea客户端 示意图

Open CASCADE学习|将圆转换为NURBS曲线

NURBS曲线&#xff0c;全称非均匀有理B样条曲线&#xff08;Non-Uniform Rational B-Splines&#xff09;&#xff0c;是计算机图形学中用于表示几何形状的数学表示方法。它结合了非均匀B样条&#xff08;B-Splines&#xff09;和有理基函数&#xff08;Rational Basis Functio…

[leetcode] 240. 搜索二维矩阵 II

编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target 。该矩阵具有以下特性&#xff1a; 每行的元素从左到右升序排列。每列的元素从上到下升序排列。 示例 1&#xff1a; 输入&#xff1a;matrix [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,…

在 Windows 中安装配置并启动运行 Jenkins【图文详细教程】

安装 Jenkins 的系统要求&#xff1a; 最少 256MB 可用内存最少 1GB 可用磁盘空间JDK 8 / 11 /17&#xff08;Jenkins 是用 Java 写的&#xff0c;打包成 war 包&#xff09; 查看 JDK 的版本 Java JDK 在 Windows 中安装可以参考&#xff1a;https://www.yuque.com/u27599042/…

SQL的事务及其ACID属性

目录 SQL中的事务简介事务和ACID属性SQL事务中的关键命令示例SQL事务的隔离层级1. 未提交读取2. 提交后读取3. 可重复读取4. 可序列化脏读、不可重复读或虚读脏读取不可重复读取(未提交读取)虚读取推荐超级课程: Docker快速入门到精通Kubernetes入门到大师通关课AWS云服务快速…

HTML5和CSS3新特性

Html新增属性 1.新增语义化标签 <header>&#xff1a;头部标签 <nav>&#xff1a;导航标签 <article>&#xff1a;内容标签 <section>&#xff1a;定义文档某个区域 <aside>&#xff1a;侧边栏标签 <footer>&#xff1a;尾部标签 2.…

c#绘制图形

窗体工具控件 如果选纹理 ,需要在ImageList中选择图像(点击添加选择图片路径) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.…

QT----基于QT的人脸考勤系统ubuntu系统运行,编译开发板

目录 1 Ubantu编译opencv和seetaface库1.1 Ubantu编译opencv1.2 Ubuntu编译seetaface1.3 安装qt 2 更改代码2.1 直接运行报错/usr/bin/ld: cannot find -lGL: No such file or directory2.2 遇到报错摄像头打不开2.3 修改部分代码2.4 解决中文语音输出问题 3 尝试交叉编译rk358…