-
Notifications
You must be signed in to change notification settings - Fork 312
/
useDragSort.ts
178 lines (167 loc) · 7.54 KB
/
useDragSort.ts
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
// 表格 行拖拽 + 列拖拽功能
import { MutableRefObject, useEffect, useMemo, useRef, useState } from 'react';
import Sortable, { SortableEvent, SortableOptions, MoveEvent } from 'sortablejs';
import get from 'lodash/get';
import { TableRowData, TdPrimaryTableProps, DragSortContext, PrimaryTableCol } from '../type';
import useClassName from './useClassName';
import { hasClass } from '../../_util/dom';
import log from '../../_common/js/log';
import swapDragArrayElement from '../../_common/js/utils/swapDragArrayElement';
import { BaseTableColumns } from '../interface';
export default function useDragSort(props: TdPrimaryTableProps, primaryTableRef: MutableRefObject<any>) {
const { sortOnRowDraggable, dragSort, data, onDragSort } = props;
const { tableDraggableClasses, tableBaseClass, tableFullRowClasses } = useClassName();
const [columns, setDragSortColumns] = useState<BaseTableColumns>(props.columns || []);
// 判断是否有拖拽列。此处重点测试树形结构的拖拽排序
const dragCol = useMemo(() => columns.find((item) => item.colKey === 'drag'), [columns]);
// 行拖拽判断条件
const isRowDraggable = useMemo(() => sortOnRowDraggable || dragSort === 'row', [dragSort, sortOnRowDraggable]);
// 行拖拽判断条件-手柄列
const isRowHandlerDraggable = useMemo(
() => ['row-handler', 'row-handler-col'].includes(dragSort) && !!dragCol,
[dragSort, dragCol],
);
// 列拖拽判断条件
const isColDraggable = useMemo(() => ['col', 'row-handler-col'].includes(dragSort), [dragSort]);
// 为实现受控,存储上一次的变化结果。React 在回调函数中无法获取最新的 state/props 值,因此使用 useRef
const lastRowList = useRef([]);
// React 在回调函数中无法获取最新的 state/props 值,因此使用 useRef
const tData = useRef<TableRowData[]>();
// 为实现受控,存储上一次的变化结果。React 在回调函数中无法获取最新的 state/props 值,因此使用 useRef
const lastColList = useRef([]);
// 为实现受控,存储上一次的变化结果。React 在回调函数中无法获取最新的 state/props 值,因此使用 useRef
const dragColumns = useRef([]);
// 为实现受控,存储上一次的变化结果。React 在回调函数中无法获取最新的 state/props 值,因此使用 useRef
const originalColumns = useRef([]);
if (props.sortOnRowDraggable) {
log.warn('Table', "`sortOnRowDraggable` is going to be deprecated, use dragSort='row' instead.");
}
useEffect(() => {
// 更新排列顺序
lastRowList.current = data.map((item) => String(get(item, props.rowKey)));
tData.current = data;
}, [data, props.rowKey]);
useEffect(() => {
lastColList.current = props.columns.map((t) => t.colKey);
dragColumns.current = props.columns;
originalColumns.current = props.columns;
}, [props.columns]);
const registerRowDragEvent = (element: HTMLElement) => {
if (!isRowHandlerDraggable && !isRowDraggable) return;
// 拖拽实例
let dragInstanceTmp: Sortable = null;
const dragContainer = element?.querySelector('tbody');
if (!dragContainer) {
log.error('Table', 'tbody does not exist.');
return null;
}
const baseOptions: SortableOptions = {
animation: 150,
...props.dragSortOptions,
ghostClass: tableDraggableClasses.ghost,
chosenClass: tableDraggableClasses.chosen,
dragClass: tableDraggableClasses.dragging,
filter: `.${tableFullRowClasses.base}`, // 过滤首行尾行固定
onMove: (evt: MoveEvent) => !hasClass(evt.related, tableFullRowClasses.base),
onEnd: (evt: SortableEvent) => {
// 处理受控:拖拽列表恢复原始排序,等待外部数据 data 变化,更新最终顺序
let { oldIndex: currentIndex, newIndex: targetIndex } = evt;
dragInstanceTmp?.sort([...lastRowList.current]);
if (props.firstFullRow) {
currentIndex -= 1;
targetIndex -= 1;
}
const params: DragSortContext<TableRowData> = {
currentIndex,
current: tData.current[currentIndex],
targetIndex,
target: tData.current[targetIndex],
data: tData.current,
newData: swapDragArrayElement([...tData.current], currentIndex, targetIndex),
e: evt,
sort: 'row',
};
// currentData is going to be deprecated.
params.currentData = params.newData;
onDragSort?.(params);
},
};
if (isRowDraggable) {
dragInstanceTmp = new Sortable(dragContainer, { ...baseOptions });
} else if (isRowHandlerDraggable) {
dragInstanceTmp = new Sortable(dragContainer, {
...baseOptions,
handle: `.${tableDraggableClasses.handle}`,
});
}
lastRowList.current = dragInstanceTmp?.toArray();
};
// TODO: 待和 Vue 保持相同逻辑
const registerColDragEvent = (tableElement: HTMLElement) => {
if (!isColDraggable || !tableElement) return;
// 拖拽实例
let dragInstanceTmp: Sortable = null;
const options: SortableOptions = {
animation: 150,
...props.dragSortOptions,
dataIdAttr: 'data-colkey',
direction: 'vertical',
ghostClass: tableDraggableClasses.ghost,
chosenClass: tableDraggableClasses.chosen,
dragClass: tableDraggableClasses.dragging,
handle: `.${tableBaseClass.thCellInner}`,
onEnd: (evt: SortableEvent) => {
// 处理受控:拖拽列表恢复原始排序,等待外部数据 data 变化,更新最终顺序
dragInstanceTmp?.sort([...lastColList.current]);
let { oldIndex: currentIndex, newIndex: targetIndex } = evt;
const current = dragColumns.current[currentIndex];
const target = dragColumns.current[targetIndex];
if (!current || !current.colKey) {
log.error('Table', `colKey is missing in ${JSON.stringify(current)}`);
}
if (!target || !target.colKey) {
log.error('Table', `colKey is missing in ${JSON.stringify(target)}`);
}
// 寻找外部数据 props.columns 中的真正下标
currentIndex = originalColumns.current.findIndex((t) => t.colKey === current.colKey);
targetIndex = originalColumns.current.findIndex((t) => t.colKey === target.colKey);
const params: DragSortContext<PrimaryTableCol> = {
data: dragColumns.current,
currentIndex,
current,
targetIndex,
target,
newData: swapDragArrayElement([...originalColumns.current], currentIndex, targetIndex),
e: evt,
sort: 'col',
};
// currentData is going to be deprecated.
params.currentData = params.newData;
onDragSort?.(params);
},
};
const container = tableElement.querySelector('thead > tr') as HTMLDivElement;
dragInstanceTmp = new Sortable(container, options);
lastColList.current = dragInstanceTmp?.toArray();
};
// 注册拖拽事件
useEffect(() => {
if (!primaryTableRef || !primaryTableRef.current) return;
registerRowDragEvent(primaryTableRef.current?.tableElement);
registerColDragEvent(primaryTableRef.current?.tableHtmlElement);
/** 待表头节点准备完成后 */
const timer = setTimeout(() => {
if (primaryTableRef.current?.affixHeaderElement) {
registerColDragEvent(primaryTableRef.current.affixHeaderElement);
}
clearTimeout(timer);
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [primaryTableRef, columns, dragSort]);
return {
isRowDraggable,
isRowHandlerDraggable,
isColDraggable,
setDragSortColumns,
};
}