Skip to content

Commit 6c95fbe

Browse files
committed
chore(components/draggable): 完成了 copy 的逻辑
1 parent 218508d commit 6c95fbe

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

packages/components/src/draggable/hooks/useDraggableChildren.ts

+28-21
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,25 @@ export function useDraggableChildren({
6363
// 拖动目标元素事件
6464
function getDragSideEvents() {
6565
const left: DOMAttributes<HTMLElement> = {
66-
onDragStart: (e: DragEvent) => {
67-
const dataTransfer = e.dataTransfer as DataTransfer;
68-
// dataTransfer.setData('text', `draggable:${index}`);
69-
dataTransfer.effectAllowed = 'move';
70-
fromRef.current = { children: el, item, id };
71-
const target = e.currentTarget as HTMLElement;
72-
target.classList.add(cls.__.ghost);
73-
},
7466
onDragEnd: (e: DragEvent) => {
75-
console.log('onDragEnd');
76-
const el = e.currentTarget as HTMLElement;
77-
el.classList.remove(cls.__.ghost);
67+
const target = e.currentTarget as HTMLElement;
68+
target.classList.remove(cls.__.ghost);
69+
console.log('onDragEnd', item, target);
7870
toRef.current?.cancel?.(id);
7971
emit();
8072
fromRef.current = null;
8173
toRef.current = null;
8274
stateRef.current.index = -1;
8375
e.preventDefault();
8476
},
77+
onDragStart: (e: DragEvent) => {
78+
const dataTransfer = e.dataTransfer as DataTransfer;
79+
// dataTransfer.setData('text', `draggable:${index}`);
80+
dataTransfer.effectAllowed = 'move';
81+
fromRef.current = { children: el, item, id };
82+
const target = e.currentTarget as HTMLElement;
83+
target.classList.add(cls.__.ghost);
84+
},
8585
};
8686
return left;
8787
}
@@ -141,12 +141,12 @@ export function useDraggableChildren({
141141
else moveFromGroup(from.item, from.children, index);
142142
} else {
143143
console.log('uuuuuuuuuuup');
144-
const temp = childrenRef.current.slice();
145-
temp[fromIndex] = cloneAndAddGhostClassName(
146-
temp[fromIndex] as ReactElement,
144+
updateChildrenOf(
145+
cloneAndAddGhostClassName(
146+
childrenRef.current[fromIndex] as ReactElement,
147+
),
148+
fromIndex,
147149
);
148-
childrenRef.current = temp;
149-
forceUpdate();
150150
}
151151
// target.classList.remove(cls['--'].hidden);
152152
stateRef.current.index = index;
@@ -166,16 +166,16 @@ export function useDraggableChildren({
166166
}
167167
},
168168
cancel: (_id2): void => {
169-
console.log('oncancel');
169+
console.log('oncancel', id, _id2, prever.id, target);
170170
stateRef.current.index = -1;
171-
if (prever.id !== id) {
172-
console.log('hhhhhhhhhh');
173-
}
174171
if (!list.includes(from.item)) {
175172
removeItem(from.item);
176173
preverRef.current = null;
177174
} else {
178-
target.classList.remove(cls.__.ghost);
175+
console.log('ccccccccccccc');
176+
const index = listRef.current.indexOf(from.item);
177+
// 还原成未加工状态
178+
updateChildrenOf(from.children, index);
179179
}
180180
},
181181
drop: (_id2): void => {
@@ -218,6 +218,13 @@ export function useDraggableChildren({
218218
insertToArray(item, to, listRef.current);
219219
forceUpdate();
220220
}
221+
function updateChildrenOf(children: ReactElement, index: number): void {
222+
const temp = childrenRef.current.slice();
223+
// 还原成未加工状态
224+
temp[index] = children;
225+
childrenRef.current = temp;
226+
forceUpdate();
227+
}
221228
function cloneAndAddGhostClassName(element: ReactElement): ReactElement {
222229
return cloneElement(element, {
223230
className: getClassNames(element.props.className, cls.__.ghost),

0 commit comments

Comments
 (0)