This repository has been archived by the owner on Mar 8, 2021. It is now read-only.
generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDragDropList.jsx
135 lines (122 loc) · 4.24 KB
/
DragDropList.jsx
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
import React from 'react';
import { isEmpty } from 'lodash';
import { DragDropContext, Draggable, Droppable } from 'react-beautiful-dnd';
import { v4 as uuid } from 'uuid';
const DragDropList = (props) => {
const { childList, children, onMoveItem, as = 'div' } = props; //renderChild
const [placeholderProps, setPlaceholderProps] = React.useState({});
const [uid] = React.useState(uuid());
const handleDragStart = React.useCallback((event) => {
const queryAttr = 'data-rbd-draggable-id';
const domQuery = `[${queryAttr}='${event.draggableId}']`;
const draggedDOM = document.querySelector(domQuery);
if (!draggedDOM) {
return;
}
const { clientHeight, clientWidth } = draggedDOM;
const sourceIndex = event.source.index;
var clientY =
parseFloat(window.getComputedStyle(draggedDOM.parentNode).paddingTop) +
[...draggedDOM.parentNode.children]
.slice(0, sourceIndex)
.reduce((total, curr) => {
const style = curr.currentStyle || window.getComputedStyle(curr);
const marginBottom = parseFloat(style.marginBottom);
return total + curr.clientHeight + marginBottom;
}, 0);
setPlaceholderProps({
clientHeight,
clientWidth,
clientY,
clientX: parseFloat(
window.getComputedStyle(draggedDOM.parentNode).paddingLeft,
),
});
}, []);
const onDragUpdate = React.useCallback((update) => {
if (!update.destination) {
return;
}
const draggableId = update.draggableId;
const destinationIndex = update.destination.index;
const queryAttr = 'data-rbd-draggable-id';
const domQuery = `[${queryAttr}='${draggableId}']`;
const draggedDOM = document.querySelector(domQuery);
if (!draggedDOM) {
return;
}
const { clientHeight, clientWidth } = draggedDOM;
const sourceIndex = update.source.index;
const childrenArray = [...draggedDOM.parentNode.children];
const movedItem = childrenArray[sourceIndex];
childrenArray.splice(sourceIndex, 1);
const updatedArray = [
...childrenArray.slice(0, destinationIndex),
movedItem,
...childrenArray.slice(destinationIndex + 1),
];
var clientY =
parseFloat(window.getComputedStyle(draggedDOM.parentNode).paddingTop) +
updatedArray.slice(0, destinationIndex).reduce((total, curr) => {
if (!curr) return total;
const style = curr.currentStyle || window.getComputedStyle(curr);
const marginBottom = parseFloat(style.marginBottom);
return total + curr.clientHeight + marginBottom;
}, 0);
setPlaceholderProps({
clientHeight,
clientWidth,
clientY,
clientX: parseFloat(
window.getComputedStyle(draggedDOM.parentNode).paddingLeft,
),
});
}, []);
const AsDomComponent = as;
return (
<DragDropContext
onDragEnd={(result) => {
const isMoved = onMoveItem(result);
if (isMoved) setPlaceholderProps({});
}}
onDragStart={handleDragStart}
onDragUpdate={onDragUpdate}
>
<Droppable droppableId={uid}>
{(provided, snapshot) => (
<AsDomComponent
ref={provided.innerRef}
{...provided.droppableProps}
style={{ position: 'relative' }}
>
{childList
.filter(([id, child]) => id && child) // beware numbers!
.map(([childId, child], index) => (
<Draggable
draggableId={childId.toString()}
index={index}
key={childId}
>
{(draginfo) => children({ child, childId, index, draginfo })}
</Draggable>
))}
{provided.placeholder}
{!isEmpty(placeholderProps) && (
<div
style={{
position: 'absolute',
top: `${placeholderProps.clientY}px`,
height: `${placeholderProps.clientHeight + 18}px`,
background: '#eee',
width: `${placeholderProps.clientWidth}px`,
borderRadius: '3px',
}}
/>
)}
</AsDomComponent>
)}
</Droppable>
</DragDropContext>
);
};
export default DragDropList;