DragTable.jsx 2.76 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1
import React, { useState, useEffect, useRef } from 'react';
2 3 4 5 6 7 8 9
import { Table } from 'antd';

import { HTML5Backend } from 'react-dnd-html5-backend';
import { DndProvider } from 'react-dnd';

import DraggableBodyRow from './DraggableBodyRow';

const DragTable = props => {
皮倩雯's avatar
皮倩雯 committed
10
  const { columns, dataSource, dragCallBack, ItemTypes, editEventType, setSelectColor } = props;
11
  const [data, setData] = useState(null); // 分组后的数组
皮倩雯's avatar
皮倩雯 committed
12
  const moving = useRef(false);
13 14 15 16 17 18
  const components = {
    body: {
      row: DraggableBodyRow,
    },
  };
  useEffect(() => {
皮倩雯's avatar
皮倩雯 committed
19
    moving.current = false;
20 21 22 23
    if (dataSource.length > 0) {
      setData(dataSource);
    }
  }, [dataSource]);
24 25
  // 每次拖拽后返回
  useEffect(() => {
皮倩雯's avatar
皮倩雯 committed
26 27 28 29 30 31 32
    if (ItemTypes === 'maintenance') {
      let moveState = moving.current;
      let val = { data, moveState };
      dragCallBack(val);
    } else {
      dragCallBack(data);
    }
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  }, [data]);
  // 移动数组元素到指定位置
  const moveInArray = (arr, from, to) => {
    // 确保是有效数组
    if (Object.prototype.toString.call(arr) !== '[object Array]') {
      throw new Error('Please provide a valid array');
    }
    // 删除当前的位置
    let item = arr.splice(from, 1);
    // 确保还剩有元素移动
    if (!item.length) {
      throw new Error(`There is no item in the array at index ${from}`);
    }
    // 移动元素到指定位置
    arr.splice(to, 0, item[0]);
    return arr;
  };
  // 拖拽表格
  const moveRow = (dragIndex, hoverIndex) => {
皮倩雯's avatar
皮倩雯 committed
52
    moving.current = true;
53 54 55 56 57 58 59 60 61
    setData(val => {
      let newData = JSON.parse(JSON.stringify(val));
      newData = moveInArray(newData, dragIndex, hoverIndex);
      return newData;
    });
  };
  return (
    <div>
      <DndProvider backend={HTML5Backend}>
皮倩雯's avatar
皮倩雯 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        {ItemTypes === 'maintenance' ? (
          <Table
            bordered
            columns={columns}
            dataSource={data}
            components={components}
            onRow={(record, index) => ({
              index,
              ItemTypes,
              moveRow,
              onClick: () => props.onClick && props.onClick(record),
              onDoubleClick: event => {
                event.stopPropagation();
                setSelectColor(record.id);
                editEventType(record);
              }, // 双击
            })}
            {...props}
          />
        ) : (
          <Table
            bordered
            columns={columns}
            dataSource={data}
            components={components}
            onRow={(record, index) => ({
              index,
              ItemTypes,
              moveRow,
              onClick: () => props.onClick && props.onClick(record),
            })}
            {...props}
          />
        )}
96 97 98 99 100 101
      </DndProvider>
    </div>
  );
};

export default DragTable;