All files / src/components/groupedGrandChild GroupedGrandChildGroupDetailItem.tsx

2.63% Statements 1/38
0% Branches 0/18
0% Functions 0/8
2.7% Lines 1/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                                                                      66x                                                                                                                                                                                                                                                                                                                                  
import type { Identifier, XYCoord } from 'dnd-core';
import { FormInstance, List } from 'antd';
import React, { ReactElement, useRef, useState } from 'react';
import { DragSourceMonitor, useDrag, useDrop } from 'react-dnd';
import GroupedGrandChildGroupExtra from './GroupedGrandChildGroupExtra';
import GroupedGrandChildGroupTitle from './GroupedGrandChildGroupTitle';
import {
  CellComponentDisplayPage,
  RecordProps,
  TableMetaProps
} from '@props/RecordProps';
import { DndItemTypes } from "@config/base";
import { DragItem } from "./GroupedGrandChildDetailGroup";
import { useTranslation } from 'react-i18next';
 
export interface GroupedGrandChildGroupDetailItemProps {
  key: number;
  data: Array<RecordProps>;
  record: RecordProps;
  columns: Array<TableMetaProps>;
  summaryGroupTitleColumns: Array<TableMetaProps>;
  summaryGroupDescriptionColumns: Array<TableMetaProps>;
  summaryGroupExtraColumns: Array<TableMetaProps>;
  reload: () => void;
  parentDomainId: number;
  parentDomainName: string;
  domainName: string;
  page: CellComponentDisplayPage,
  zIndex: number;
  canDnd: boolean;
  index: number;
  moveCard: (dragIndex: number, hoverIndex: number) => void
  onDrop: (item: DragItem,) => void
}
 
const GroupedGrandChildGroupDetailItem = (props: GroupedGrandChildGroupDetailItemProps): ReactElement => {
  const {
    data, record, columns, summaryGroupTitleColumns, parentDomainId, domainName, parentDomainName, reload,
    //summaryGroupDescriptionColumns,
    page, zIndex, canDnd, moveCard, onDrop, summaryGroupExtraColumns, index
  } = props;
 
  const [showOperations, setShowOperations] = useState<boolean>(false);
 
  const { t } = useTranslation();
 
  const ref = useRef<HTMLDivElement>(null);
  const [{ handlerId }, drop] = useDrop<
    DragItem,
    void,
    { handlerId: Identifier | null }
  >({
    accept: DndItemTypes.List,
    collect(monitor) {
      return {
        handlerId: monitor.getHandlerId(),
      };
    },
    hover(item: DragItem, monitor) {
      //dragIndex: 移动的节点的index
      //hoverIndex: 移动之后,最后一次经过的节点的index
      //如果移动过程中,经过了多个节点,那么每经过一个节点,本方法会被调用一次
      if (!ref.current) {
        return;
      }
      if (!canDnd) {
        return;
      }
      const dragIndex = item.index;
      const hoverIndex = index;
 
      // Don't replace items with themselves
      if (dragIndex === hoverIndex) {
        return;
      }
 
      // not exist in this data group, ignored
      if (!data.find((d) => d.id === item.id)) {
        return;
      }
 
      // Determine rectangle on screen
      const hoverBoundingRect = ref.current?.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 as XYCoord).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
      // moveCard(dragIndex, hoverIndex);
      // 如何判断是否是在同一个组内拖拽
      // 如果在同一个组内,拖拽,直接更新该组内的顺序
      // 如果不在同一个组内的拖拽,直接从原来的组中删除,然后添加到新的组中,添加的时候 displaySequence 比其前面的大 5, 比其后面的小 5
      // Time to actually perform the action
      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.
      item.index = hoverIndex;
      // 要把结果回写到父组件,然后回写到后端
      // console.log(item.index, ' <--- item.index value', hoverIndex, dragIndex);
    },
  });
 
  const [{ isDragging }, drag] = useDrag<
    DragItem,
    void,
    { isDragging: boolean | null }
  >({
    type: DndItemTypes.List,
    item: () => {
      return { id: record.id, index, type: DndItemTypes.List };
    },
    end: onDrop,
    collect: (monitor: DragSourceMonitor) => ({
      isDragging: monitor.isDragging(),
    }),
  });
 
  const opacity = isDragging ? 0 : 1;
  drag(drop(ref));
 
  const title = <GroupedGrandChildGroupTitle
    record={record}
    columns={columns}
    displayColumns={summaryGroupTitleColumns}
    domainName={domainName}
    page={page} zIndex={zIndex}
    form={{} as FormInstance}
    canDnd={canDnd}
  />;
  const extra = <GroupedGrandChildGroupExtra
    record={record}
    reload={reload}
    operationsProps={{
      ownerId: parentDomainId,
      ownerClass: parentDomainName,
    }}
    columns={columns}
    displayColumns={summaryGroupExtraColumns}
    domainName={domainName}
    page={page}
    zIndex={zIndex}
    form={{} as FormInstance}
    showOperations={showOperations}
  />;
  // const description = <GroupedGrandChildGroupTitle
  //   record={record}
  //   columns={columns}
  //   displayColumns={summaryGroupDescriptionColumns}
  //   domainName={domainName}
  //   page={page}
  //   zIndex={zIndex}
  //   form={{} as FormInstance}
  // />;
  return (<List.Item
    extra={extra}
    key={record.id}
    ref={ref}
    data-handler-id={handlerId}
    style={{ opacity }}
    title={ t('Click and drag to sort') }
    onMouseEnter={() => setShowOperations(true)}
    onMouseLeave={() => setShowOperations(false)}
  >
    <List.Item.Meta
      title={title}
      //FIXME add description back
      description={undefined}
    />
  </List.Item>);
};
 
export default GroupedGrandChildGroupDetailItem;