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

news/2024/7/15 18:09:39 标签: react.js, javascript, 前端

写在前边,二维数组可以拖拽,但是不可以编辑+拖拽,如果想要实现编辑+拖拽,还是需要转换成一维数组。原因是因为插件的官方规定,在拖拽过程中不可以编辑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 nicer
    userSelect: 'none',
    padding: grid * 2,
    margin: `0 ${grid}px 0 0`,

    // change background colour if dragging
    background: 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) => (
                  <div
                    ref={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 nicer
    userSelect: 'none',
    padding: grid * 2,
    margin: `0 ${grid}px 0 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',
    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) => (
                    <div
                      ref={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: '', // 类型id
        unitName: '', // 单位名称
        unitValue: '', // 单位id
        value: '', // 值
      };
      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 nicer
    width: '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) => (
                  <div
                    ref={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>
                    <div
                      style={{
                        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' }}>
                        <Input
                          placeholder="请输入内容"
                          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.niftyadmin.cn/n/5443559.html

相关文章

【晴问算法】入门篇—贪心算法—最优装箱

题目描述 有n 个箱子需要装上一艘轮船&#xff0c;已知第i 个箱子的重量为wi&#xff0c;轮船的载重为W。 问在不超过轮船载重的前提下&#xff0c;最多能把多少个箱子装上轮船。 输入描述 输出描述 输出两个整数&#xff0c;分别表示能装上轮船的箱子个数和总重量&#xff0c;…

申请Github Education获取免费Copilot权限(2024.3.18实测成功)

起因&#xff1a;旧帐户Copilot权限被封 我已经离开Github Copilot就无法独自耐着性子写代码了&#xff08;懒惰AI成瘾性&#xff09;&#xff0c;这两天Github Copilot不知道为什么在大规模封号&#xff0c;我不幸也被封号了&#xff08;禁用掉了Github Copilot权限&#xff…

网站建设中的HTTP 请求方法

快速入门 — Flask 0.10.1 文档 (jinkan.org) HTTP 请求方法 | 菜鸟教程 (runoob.com) HTTP &#xff08;与 Web 应用会话的协议&#xff09;有许多不同的访问 URL 方法。默认情况下&#xff0c;路由只回应 GET 请求&#xff0c;但是通过 route() 装饰器传递 methods 参数可以…

数据结构奇妙旅程之线性表

线性表&#xff08;Linear List&#xff09;是数据结构中的一种基本类型&#xff0c;它代表了具有相同类型数据元素的一个有限序列。线性表中的数据元素之间是一对一的关系&#xff0c;即除了第一个元素外&#xff0c;每个元素有且仅有一个前驱元素&#xff1b;除了最后一个元素…

通过git bash 或命令行ssh访问服务器 sftp上传下载文件

上传下载文件 sftp -P 端口 appywIP 示例&#xff1a;sftp -P 10022 appyw25.222.133.222 然后输入密码即可 ls 查看文件 lls 查看本地文件 cd 跳转 lcd 本地跳转 get ... 下载文件 put 本地文件名 远程文件夹 //上传文件 put -r 本地文件夹 远程文件夹 //上传文件夹服务器…

uniApp中使用小程序XR-Frame创建3D场景(1)环境搭建

1.XR-Frame简介 XR-Frame作为微信小程序官方推出的3D框架&#xff0c;是目前所有小程序平台中3D效果最好的一个&#xff0c;由于其本身针对微信小程序做了优化&#xff0c;在性能方面比其他第三方库都要高很多。 2.与Three.js的区别 做3D小程序的同学们对Three.js一定不陌生…

面试十二、装饰器模式

装饰器&#xff08;Decorator&#xff09;模式的定义&#xff1a;指在不改变现有对象结构的情况下&#xff0c;动态地给该对象增加一些职责&#xff08;即增加其额外功能&#xff09;的模式&#xff0c;它属于对象结构型模式。 装饰器模式的主要优点有&#xff1a; 装饰器是继…

搭建ELK+minio及配置

什么是ELK ELK是一种基于开源工具的日志管理和数据分析解决方案&#xff0c;它由三个核心组件组成&#xff1a; Elasticsearch&#xff1a;用于存储、搜索和分析大规模数据的分布式搜索引擎。Logstash&#xff1a;用于收集、过滤、转换和发送日志数据的数据处理管道。Kibana&…