@patternfly/react-drag-drop 6.0.0-alpha.71 → 6.0.0-alpha.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/cjs.tsbuildinfo +1 -1
- package/dist/esm/components/DragDrop/DragDropContainer.d.ts +41 -0
- package/dist/esm/components/DragDrop/DragDropContainer.d.ts.map +1 -0
- package/dist/esm/components/DragDrop/DragDropContainer.js +147 -0
- package/dist/esm/components/DragDrop/DragDropContainer.js.map +1 -0
- package/dist/esm/components/DragDrop/DragDropSort.d.ts +5 -11
- package/dist/esm/components/DragDrop/DragDropSort.d.ts.map +1 -1
- package/dist/esm/components/DragDrop/DragDropSort.js +11 -59
- package/dist/esm/components/DragDrop/DragDropSort.js.map +1 -1
- package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js +2 -2
- package/dist/esm/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
- package/dist/esm/components/DragDrop/Droppable.d.ts +15 -9
- package/dist/esm/components/DragDrop/Droppable.d.ts.map +1 -1
- package/dist/esm/components/DragDrop/Droppable.js +21 -4
- package/dist/esm/components/DragDrop/Droppable.js.map +1 -1
- package/dist/esm/components/DragDrop/index.d.ts +2 -0
- package/dist/esm/components/DragDrop/index.d.ts.map +1 -1
- package/dist/esm/components/DragDrop/index.js +2 -0
- package/dist/esm/components/DragDrop/index.js.map +1 -1
- package/dist/esm.tsbuildinfo +1 -1
- package/dist/js/components/DragDrop/DragDropContainer.d.ts +41 -0
- package/dist/js/components/DragDrop/DragDropContainer.d.ts.map +1 -0
- package/dist/js/components/DragDrop/DragDropContainer.js +151 -0
- package/dist/js/components/DragDrop/DragDropContainer.js.map +1 -0
- package/dist/js/components/DragDrop/DragDropSort.d.ts +5 -11
- package/dist/js/components/DragDrop/DragDropSort.d.ts.map +1 -1
- package/dist/js/components/DragDrop/DragDropSort.js +11 -59
- package/dist/js/components/DragDrop/DragDropSort.js.map +1 -1
- package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js +2 -2
- package/dist/js/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
- package/dist/js/components/DragDrop/Droppable.d.ts +15 -9
- package/dist/js/components/DragDrop/Droppable.d.ts.map +1 -1
- package/dist/js/components/DragDrop/Droppable.js +21 -4
- package/dist/js/components/DragDrop/Droppable.js.map +1 -1
- package/dist/js/components/DragDrop/index.d.ts +2 -0
- package/dist/js/components/DragDrop/index.d.ts.map +1 -1
- package/dist/js/components/DragDrop/index.js +2 -0
- package/dist/js/components/DragDrop/index.js.map +1 -1
- package/package.json +3 -3
- package/src/components/DragDrop/DragDropContainer.tsx +305 -0
- package/src/components/DragDrop/DragDropSort.tsx +22 -137
- package/src/components/DragDrop/DraggableDualListSelectorListItem.tsx +2 -2
- package/src/components/DragDrop/Droppable.tsx +64 -15
- package/src/components/DragDrop/__tests__/DragDrop.test.tsx +5 -0
- package/src/components/DragDrop/examples/DataListDraggable.tsx +1 -0
- package/src/components/DragDrop/examples/DragDrop.md +8 -2
- package/src/components/DragDrop/examples/DragDropContainerBasic.tsx +45 -0
- package/src/components/DragDrop/examples/DragDropContainerDataList.tsx +75 -0
- package/src/components/DragDrop/examples/DragDropContainerDualListSelector.tsx +191 -0
- package/src/components/DragDrop/examples/DragDropDemos.md +25 -1
- package/src/components/DragDrop/index.ts +2 -0
- package/dist/esm/components/DragDrop/DroppableContext.d.ts +0 -6
- package/dist/esm/components/DragDrop/DroppableContext.d.ts.map +0 -1
- package/dist/esm/components/DragDrop/DroppableContext.js +0 -6
- package/dist/esm/components/DragDrop/DroppableContext.js.map +0 -1
- package/dist/esm/next/components/DragDrop/DragButton.d.ts +0 -11
- package/dist/esm/next/components/DragDrop/DragButton.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/DragButton.js +0 -14
- package/dist/esm/next/components/DragDrop/DragButton.js.map +0 -1
- package/dist/esm/next/components/DragDrop/DragDropSort.d.ts +0 -35
- package/dist/esm/next/components/DragDrop/DragDropSort.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/DragDropSort.js +0 -73
- package/dist/esm/next/components/DragDrop/DragDropSort.js.map +0 -1
- package/dist/esm/next/components/DragDrop/Draggable.d.ts +0 -13
- package/dist/esm/next/components/DragDrop/Draggable.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/Draggable.js +0 -22
- package/dist/esm/next/components/DragDrop/Draggable.js.map +0 -1
- package/dist/esm/next/components/DragDrop/DraggableDataListItem.d.ts +0 -15
- package/dist/esm/next/components/DragDrop/DraggableDataListItem.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/DraggableDataListItem.js +0 -26
- package/dist/esm/next/components/DragDrop/DraggableDataListItem.js.map +0 -1
- package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts +0 -23
- package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.js +0 -37
- package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +0 -1
- package/dist/esm/next/components/DragDrop/Droppable.d.ts +0 -16
- package/dist/esm/next/components/DragDrop/Droppable.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/Droppable.js +0 -11
- package/dist/esm/next/components/DragDrop/Droppable.js.map +0 -1
- package/dist/esm/next/components/DragDrop/DroppableContext.d.ts +0 -6
- package/dist/esm/next/components/DragDrop/DroppableContext.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/DroppableContext.js +0 -6
- package/dist/esm/next/components/DragDrop/DroppableContext.js.map +0 -1
- package/dist/esm/next/components/DragDrop/index.d.ts +0 -2
- package/dist/esm/next/components/DragDrop/index.d.ts.map +0 -1
- package/dist/esm/next/components/DragDrop/index.js +0 -2
- package/dist/esm/next/components/DragDrop/index.js.map +0 -1
- package/dist/esm/next/components/index.d.ts +0 -2
- package/dist/esm/next/components/index.d.ts.map +0 -1
- package/dist/esm/next/components/index.js +0 -2
- package/dist/esm/next/components/index.js.map +0 -1
- package/dist/esm/next/index.d.ts +0 -2
- package/dist/esm/next/index.d.ts.map +0 -1
- package/dist/esm/next/index.js +0 -2
- package/dist/esm/next/index.js.map +0 -1
- package/dist/js/components/DragDrop/DroppableContext.d.ts +0 -6
- package/dist/js/components/DragDrop/DroppableContext.d.ts.map +0 -1
- package/dist/js/components/DragDrop/DroppableContext.js +0 -10
- package/dist/js/components/DragDrop/DroppableContext.js.map +0 -1
- package/dist/js/next/components/DragDrop/DragButton.d.ts +0 -11
- package/dist/js/next/components/DragDrop/DragButton.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/DragButton.js +0 -18
- package/dist/js/next/components/DragDrop/DragButton.js.map +0 -1
- package/dist/js/next/components/DragDrop/DragDropSort.d.ts +0 -35
- package/dist/js/next/components/DragDrop/DragDropSort.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/DragDropSort.js +0 -77
- package/dist/js/next/components/DragDrop/DragDropSort.js.map +0 -1
- package/dist/js/next/components/DragDrop/Draggable.d.ts +0 -13
- package/dist/js/next/components/DragDrop/Draggable.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/Draggable.js +0 -26
- package/dist/js/next/components/DragDrop/Draggable.js.map +0 -1
- package/dist/js/next/components/DragDrop/DraggableDataListItem.d.ts +0 -15
- package/dist/js/next/components/DragDrop/DraggableDataListItem.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/DraggableDataListItem.js +0 -30
- package/dist/js/next/components/DragDrop/DraggableDataListItem.js.map +0 -1
- package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts +0 -23
- package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.js +0 -41
- package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +0 -1
- package/dist/js/next/components/DragDrop/Droppable.d.ts +0 -16
- package/dist/js/next/components/DragDrop/Droppable.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/Droppable.js +0 -15
- package/dist/js/next/components/DragDrop/Droppable.js.map +0 -1
- package/dist/js/next/components/DragDrop/DroppableContext.d.ts +0 -6
- package/dist/js/next/components/DragDrop/DroppableContext.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/DroppableContext.js +0 -10
- package/dist/js/next/components/DragDrop/DroppableContext.js.map +0 -1
- package/dist/js/next/components/DragDrop/index.d.ts +0 -2
- package/dist/js/next/components/DragDrop/index.d.ts.map +0 -1
- package/dist/js/next/components/DragDrop/index.js +0 -5
- package/dist/js/next/components/DragDrop/index.js.map +0 -1
- package/dist/js/next/components/index.d.ts +0 -2
- package/dist/js/next/components/index.d.ts.map +0 -1
- package/dist/js/next/components/index.js +0 -5
- package/dist/js/next/components/index.js.map +0 -1
- package/dist/js/next/index.d.ts +0 -2
- package/dist/js/next/index.d.ts.map +0 -1
- package/dist/js/next/index.js +0 -5
- package/dist/js/next/index.js.map +0 -1
- package/src/components/DragDrop/DroppableContext.ts +0 -6
- package/src/components/DragDrop/examples/BasicSortingWithDragButton.tsx +0 -19
- package/src/next/components/DragDrop/DragButton.tsx +0 -27
- package/src/next/components/DragDrop/DragDropSort.tsx +0 -186
- package/src/next/components/DragDrop/Draggable.tsx +0 -58
- package/src/next/components/DragDrop/DraggableDataListItem.tsx +0 -61
- package/src/next/components/DragDrop/DraggableDualListSelectorListItem.tsx +0 -93
- package/src/next/components/DragDrop/Droppable.tsx +0 -27
- package/src/next/components/DragDrop/DroppableContext.ts +0 -6
- package/src/next/components/DragDrop/__tests__/DragDrop.test.tsx +0 -24
- package/src/next/components/DragDrop/__tests__/__snapshots__/DragDrop.test.tsx.snap +0 -59
- package/src/next/components/DragDrop/examples/BasicSortingWithDragButton.tsx +0 -20
- package/src/next/components/DragDrop/examples/DataListDraggable.tsx +0 -38
- package/src/next/components/DragDrop/examples/DragDrop.md +0 -32
- package/src/next/components/DragDrop/examples/DragDropDemos.md +0 -37
- package/src/next/components/DragDrop/examples/DualListSelectorDraggable.tsx +0 -150
- package/src/next/components/DragDrop/index.ts +0 -1
- package/src/next/components/index.ts +0 -1
- package/src/next/index.ts +0 -1
- /package/src/{next/components → components}/DragDrop/examples/BasicSorting.tsx +0 -0
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import * as ReactDOM from 'react-dom';
|
|
3
|
+
import { css } from '@patternfly/react-styles';
|
|
4
|
+
import {
|
|
5
|
+
DndContext,
|
|
6
|
+
closestCenter,
|
|
7
|
+
DragOverlay,
|
|
8
|
+
DndContextProps,
|
|
9
|
+
KeyboardSensor,
|
|
10
|
+
PointerSensor,
|
|
11
|
+
useSensor,
|
|
12
|
+
useSensors,
|
|
13
|
+
DragEndEvent,
|
|
14
|
+
DragStartEvent,
|
|
15
|
+
UniqueIdentifier,
|
|
16
|
+
DragOverEvent,
|
|
17
|
+
CollisionDetection,
|
|
18
|
+
pointerWithin,
|
|
19
|
+
rectIntersection,
|
|
20
|
+
getFirstCollision,
|
|
21
|
+
DragCancelEvent
|
|
22
|
+
} from '@dnd-kit/core';
|
|
23
|
+
import { arrayMove, sortableKeyboardCoordinates } from '@dnd-kit/sortable';
|
|
24
|
+
import { Draggable } from './Draggable';
|
|
25
|
+
import { DraggableDataListItem } from './DraggableDataListItem';
|
|
26
|
+
import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
|
|
27
|
+
import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
|
|
28
|
+
import { DataList, canUseDOM } from '@patternfly/react-core';
|
|
29
|
+
|
|
30
|
+
export type DragDropContainerDragStartEvent = DragStartEvent;
|
|
31
|
+
export type DragDropContainerDragOverEvent = DragOverEvent;
|
|
32
|
+
export type DragDropContainerDragEndEvent = DragEndEvent;
|
|
33
|
+
export type DragDropContainerDragCancelEvent = DragCancelEvent;
|
|
34
|
+
|
|
35
|
+
export interface DraggableObject {
|
|
36
|
+
/** Unique id of the draggable object */
|
|
37
|
+
id: string | number;
|
|
38
|
+
/** Content rendered in the draggable object */
|
|
39
|
+
content: React.ReactNode;
|
|
40
|
+
/** Props spread to the rendered wrapper of the draggable object */
|
|
41
|
+
props?: any;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* DragDropSortProps extends dnd-kit's props which may be viewed at https://docs.dndkit.com/api-documentation/context-provider#props.
|
|
46
|
+
*/
|
|
47
|
+
export interface DragDropContainerProps extends DndContextProps {
|
|
48
|
+
/** Content containing one or more Droppable zones. */
|
|
49
|
+
children?: React.ReactNode;
|
|
50
|
+
/** Set of records of all child droppables - their zone IDs and their draggable items. */
|
|
51
|
+
items: Record<string, DraggableObject[]>;
|
|
52
|
+
/** Callback when use begins dragging a draggable object */
|
|
53
|
+
onDrag?: (event: DragDropContainerDragStartEvent) => void;
|
|
54
|
+
/** Callback when an item is dragged to another container */
|
|
55
|
+
onContainerMove?: (event: DragDropContainerDragOverEvent, items: Record<string, DraggableObject[]>) => void;
|
|
56
|
+
/** Callback when user drops a draggable object */
|
|
57
|
+
onDrop: (event: DragDropContainerDragEndEvent, items: Record<string, DraggableObject[]>) => void;
|
|
58
|
+
/** Callback when drag is cancelled */
|
|
59
|
+
onCancel?: (event: DragDropContainerDragCancelEvent, items: Record<string, DraggableObject[]>) => void;
|
|
60
|
+
/** The variant determines which component wraps the draggable object.
|
|
61
|
+
* Default variant wraps the draggable object in a div.
|
|
62
|
+
* DataList variant wraps the draggable object in a DataListItem
|
|
63
|
+
* DualListSelectorList variant wraps the draggable objects in a DualListSelectorListItem and a div.pf-c-dual-list-selector__item-text element
|
|
64
|
+
* */
|
|
65
|
+
variant?: 'default' | 'DataList' | 'DualListSelectorList';
|
|
66
|
+
/** Additional classes to apply to the drag overlay */
|
|
67
|
+
overlayProps?: any;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export const DragDropContainer: React.FunctionComponent<DragDropContainerProps> = ({
|
|
71
|
+
children,
|
|
72
|
+
items,
|
|
73
|
+
onDrag = () => {},
|
|
74
|
+
onContainerMove = () => {},
|
|
75
|
+
onDrop = () => {},
|
|
76
|
+
onCancel = () => {},
|
|
77
|
+
variant = 'default',
|
|
78
|
+
overlayProps,
|
|
79
|
+
...props
|
|
80
|
+
}: DragDropContainerProps) => {
|
|
81
|
+
const itemsCopy = React.useRef<Record<string, DraggableObject[]> | null>(null);
|
|
82
|
+
const hasRecentlyMovedContainer = React.useRef(false);
|
|
83
|
+
const [activeId, setActiveId] = React.useState<UniqueIdentifier>(null);
|
|
84
|
+
const lastOverId = React.useRef<UniqueIdentifier | null>(null);
|
|
85
|
+
|
|
86
|
+
const findItem = React.useCallback(
|
|
87
|
+
(id: UniqueIdentifier, containerId: UniqueIdentifier) => items[containerId].find((item) => item.id === id),
|
|
88
|
+
[items]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const findContainer = React.useCallback(
|
|
92
|
+
(id: UniqueIdentifier) => {
|
|
93
|
+
if (id in items) {
|
|
94
|
+
return id;
|
|
95
|
+
}
|
|
96
|
+
return Object.keys(items).find((key) => items[key].find((obj) => obj.id === id));
|
|
97
|
+
},
|
|
98
|
+
[items]
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const sensors = useSensors(
|
|
102
|
+
useSensor(PointerSensor),
|
|
103
|
+
useSensor(KeyboardSensor, {
|
|
104
|
+
coordinateGetter: sortableKeyboardCoordinates
|
|
105
|
+
})
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const collisionDetectionStrategy: CollisionDetection = React.useCallback(
|
|
109
|
+
(args) => {
|
|
110
|
+
if (activeId && activeId in items) {
|
|
111
|
+
return closestCenter({
|
|
112
|
+
...args,
|
|
113
|
+
droppableContainers: args.droppableContainers.filter((container) => container.id in items)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const pointerIntersections = pointerWithin(args);
|
|
118
|
+
const intersections = pointerIntersections.length > 0 ? pointerIntersections : rectIntersection(args);
|
|
119
|
+
let overId = getFirstCollision(intersections, 'id');
|
|
120
|
+
|
|
121
|
+
if (overId != null) {
|
|
122
|
+
if (overId in items) {
|
|
123
|
+
const containerItems = items[overId];
|
|
124
|
+
|
|
125
|
+
if (containerItems.length > 0) {
|
|
126
|
+
overId = closestCenter({
|
|
127
|
+
...args,
|
|
128
|
+
droppableContainers: args.droppableContainers.filter(
|
|
129
|
+
(container) => container.id !== overId && containerItems.find((obj) => obj.id === container.id)
|
|
130
|
+
)
|
|
131
|
+
})[0]?.id;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
lastOverId.current = overId;
|
|
136
|
+
|
|
137
|
+
return [{ id: overId }];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (hasRecentlyMovedContainer.current) {
|
|
141
|
+
lastOverId.current = activeId;
|
|
142
|
+
}
|
|
143
|
+
return lastOverId.current ? [{ id: lastOverId.current }] : [];
|
|
144
|
+
},
|
|
145
|
+
[activeId, items]
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
React.useEffect(() => {
|
|
149
|
+
requestAnimationFrame(() => {
|
|
150
|
+
hasRecentlyMovedContainer.current = false;
|
|
151
|
+
});
|
|
152
|
+
}, [items]);
|
|
153
|
+
|
|
154
|
+
const handleDragStart = (event: DragStartEvent) => {
|
|
155
|
+
const { active } = event;
|
|
156
|
+
itemsCopy.current = { ...items };
|
|
157
|
+
setActiveId(active.id);
|
|
158
|
+
|
|
159
|
+
onDrag(event);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const handleDragOver = (event: DragOverEvent) => {
|
|
163
|
+
const { active, over } = event;
|
|
164
|
+
const { id: activeId } = active;
|
|
165
|
+
const { id: overId } = over;
|
|
166
|
+
|
|
167
|
+
if (!overId || activeId in items) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const activeContainer = findContainer(activeId);
|
|
172
|
+
const overContainer = findContainer(overId);
|
|
173
|
+
|
|
174
|
+
if (!overContainer || !activeContainer) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (activeContainer !== overContainer) {
|
|
179
|
+
const activeItems = items[activeContainer];
|
|
180
|
+
const overItems = items[overContainer];
|
|
181
|
+
const overIndex = overItems.findIndex((draggableItem) => draggableItem.id === overId);
|
|
182
|
+
const activeIndex = activeItems.findIndex((draggableItem) => draggableItem.id === activeId);
|
|
183
|
+
|
|
184
|
+
const isBelowOverItem =
|
|
185
|
+
over && active.rect.current.translated && active.rect.current.translated.top > over.rect.top + over.rect.height;
|
|
186
|
+
|
|
187
|
+
const modifier = isBelowOverItem ? 1 : 0;
|
|
188
|
+
const newIndex = overIndex >= 0 ? overIndex + modifier : overItems.length + 1;
|
|
189
|
+
|
|
190
|
+
const newItems = {
|
|
191
|
+
...items,
|
|
192
|
+
[activeContainer]: items[activeContainer].filter((item) => item.id !== active.id),
|
|
193
|
+
[overContainer]: [
|
|
194
|
+
...items[overContainer].slice(0, newIndex),
|
|
195
|
+
items[activeContainer][activeIndex],
|
|
196
|
+
...items[overContainer].slice(newIndex, items[overContainer].length)
|
|
197
|
+
]
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
hasRecentlyMovedContainer.current = true;
|
|
201
|
+
onContainerMove(event, newItems);
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const handleDragEnd = (event: DragEndEvent) => {
|
|
206
|
+
const { active, over } = event;
|
|
207
|
+
const { id: activeId } = active;
|
|
208
|
+
const { id: overId } = over;
|
|
209
|
+
|
|
210
|
+
const activeContainer = findContainer(activeId);
|
|
211
|
+
if (!over || !activeContainer) {
|
|
212
|
+
setActiveId(null);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const overContainer = findContainer(overId);
|
|
217
|
+
if (!overContainer) {
|
|
218
|
+
setActiveId(null);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const activeIndex = items[activeContainer].findIndex((draggableItem) => draggableItem.id === activeId);
|
|
223
|
+
const overIndex = items[overContainer].findIndex((draggableItem) => draggableItem.id === overId);
|
|
224
|
+
|
|
225
|
+
if (activeIndex !== overIndex) {
|
|
226
|
+
const newItems = { ...items, [overContainer]: arrayMove(items[overContainer], activeIndex, overIndex) };
|
|
227
|
+
onDrop(event, newItems);
|
|
228
|
+
}
|
|
229
|
+
setActiveId(null);
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const handleDragCancel = (event: DragCancelEvent) => {
|
|
233
|
+
onCancel(event, itemsCopy.current);
|
|
234
|
+
itemsCopy.current = null;
|
|
235
|
+
setActiveId(null);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const getDragOverlay = () => {
|
|
239
|
+
if (!activeId) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
const item = findItem(activeId, findContainer(activeId));
|
|
243
|
+
|
|
244
|
+
let content;
|
|
245
|
+
switch (variant) {
|
|
246
|
+
case 'DualListSelectorList':
|
|
247
|
+
content = (
|
|
248
|
+
<DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
|
|
249
|
+
{item.content}
|
|
250
|
+
</DraggableDualListSelectorListItem>
|
|
251
|
+
);
|
|
252
|
+
break;
|
|
253
|
+
case 'DataList':
|
|
254
|
+
content = (
|
|
255
|
+
<DraggableDataListItem key={item.id} id={item.id} {...item.props}>
|
|
256
|
+
{item.content}
|
|
257
|
+
</DraggableDataListItem>
|
|
258
|
+
);
|
|
259
|
+
break;
|
|
260
|
+
default:
|
|
261
|
+
content = (
|
|
262
|
+
<Draggable useDragButton={variant === 'default'} key={item.id} id={item.id} {...item.props}>
|
|
263
|
+
{item.content}
|
|
264
|
+
</Draggable>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return (
|
|
269
|
+
<div
|
|
270
|
+
className={css(styles.draggable, styles.modifiers.dragging)}
|
|
271
|
+
style={
|
|
272
|
+
{
|
|
273
|
+
'--pf-v6-c-draggable--m-dragging--BackgroundColor':
|
|
274
|
+
'var(--pf-t--global--background--color--primary--default)'
|
|
275
|
+
} as React.CSSProperties
|
|
276
|
+
}
|
|
277
|
+
>
|
|
278
|
+
{variant === 'DualListSelectorList' && <ul className="pf-v6-c-dual-list-selector">{content}</ul>}
|
|
279
|
+
{variant === 'DataList' && (
|
|
280
|
+
<DataList aria-label="draggable overlay" {...overlayProps}>
|
|
281
|
+
{content}
|
|
282
|
+
</DataList>
|
|
283
|
+
)}
|
|
284
|
+
{variant !== 'DualListSelectorList' && variant !== 'DataList' && content}
|
|
285
|
+
</div>
|
|
286
|
+
);
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
const dragOverlay = <DragOverlay>{activeId && getDragOverlay()}</DragOverlay>;
|
|
290
|
+
return (
|
|
291
|
+
<DndContext
|
|
292
|
+
sensors={sensors}
|
|
293
|
+
collisionDetection={collisionDetectionStrategy}
|
|
294
|
+
onDragEnd={handleDragEnd}
|
|
295
|
+
onDragOver={handleDragOver}
|
|
296
|
+
onDragStart={handleDragStart}
|
|
297
|
+
onDragCancel={handleDragCancel}
|
|
298
|
+
{...props}
|
|
299
|
+
>
|
|
300
|
+
{children}
|
|
301
|
+
{canUseDOM ? ReactDOM.createPortal(dragOverlay, document.getElementById('root')) : dragOverlay}
|
|
302
|
+
</DndContext>
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
DragDropContainer.displayName = 'DragDropContainer';
|
|
@@ -1,40 +1,11 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
closestCenter,
|
|
6
|
-
DragOverlay,
|
|
7
|
-
DndContextProps,
|
|
8
|
-
KeyboardSensor,
|
|
9
|
-
PointerSensor,
|
|
10
|
-
useSensor,
|
|
11
|
-
useSensors,
|
|
12
|
-
DragEndEvent,
|
|
13
|
-
DragStartEvent
|
|
14
|
-
} from '@dnd-kit/core';
|
|
15
|
-
import {
|
|
16
|
-
arrayMove,
|
|
17
|
-
SortableContext,
|
|
18
|
-
sortableKeyboardCoordinates,
|
|
19
|
-
verticalListSortingStrategy
|
|
20
|
-
} from '@dnd-kit/sortable';
|
|
21
|
-
import { Draggable } from './Draggable';
|
|
22
|
-
import { DraggableDataListItem } from './DraggableDataListItem';
|
|
23
|
-
import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
|
|
24
|
-
import styles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
|
|
2
|
+
import { DndContextProps, DragEndEvent, DragStartEvent } from '@dnd-kit/core';
|
|
3
|
+
import { Droppable } from './Droppable';
|
|
4
|
+
import { DragDropContainer, DraggableObject } from './DragDropContainer';
|
|
25
5
|
|
|
26
6
|
export type DragDropSortDragEndEvent = DragEndEvent;
|
|
27
7
|
export type DragDropSortDragStartEvent = DragStartEvent;
|
|
28
8
|
|
|
29
|
-
export interface DraggableObject {
|
|
30
|
-
/** Unique id of the draggable object */
|
|
31
|
-
id: string;
|
|
32
|
-
/** Content rendered in the draggable object */
|
|
33
|
-
content: React.ReactNode;
|
|
34
|
-
/** Props spread to the rendered wrapper of the draggable object */
|
|
35
|
-
props?: any;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
9
|
/**
|
|
39
10
|
* DragDropSortProps extends dnd-kit's props which may be viewed at https://docs.dndkit.com/api-documentation/context-provider#props.
|
|
40
11
|
*/
|
|
@@ -50,11 +21,12 @@ export interface DragDropSortProps extends DndContextProps {
|
|
|
50
21
|
onDrag?: (event: DragDropSortDragStartEvent, oldIndex: number) => void;
|
|
51
22
|
/** The variant determines which component wraps the draggable object.
|
|
52
23
|
* Default variant wraps the draggable object in a div.
|
|
53
|
-
* DataList
|
|
24
|
+
* DataList variant wraps the draggable object in a DataListItem
|
|
54
25
|
* DualListSelectorList variant wraps the draggable objects in a DualListSelectorListItem and a div.pf-c-dual-list-selector__item-text element
|
|
55
|
-
* TableComposable variant wraps the draggable objects in TODO
|
|
56
26
|
* */
|
|
57
|
-
variant?: 'default' | 'DataList' | 'DualListSelectorList'
|
|
27
|
+
variant?: 'default' | 'DataList' | 'DualListSelectorList';
|
|
28
|
+
/** Additional classes to apply to the drag overlay */
|
|
29
|
+
overlayProps?: any;
|
|
58
30
|
}
|
|
59
31
|
|
|
60
32
|
export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
|
|
@@ -63,120 +35,33 @@ export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
|
|
|
63
35
|
onDrag = () => {},
|
|
64
36
|
variant = 'default',
|
|
65
37
|
children,
|
|
38
|
+
overlayProps,
|
|
66
39
|
...props
|
|
67
40
|
}: DragDropSortProps) => {
|
|
68
|
-
const [activeId, setActiveId] = React.useState<string>(null);
|
|
69
41
|
const itemIds = React.useMemo(() => (items ? Array.from(items, (item) => item.id as string) : []), [items]);
|
|
70
42
|
|
|
71
|
-
const getItemById = (id: string): DraggableObject => items.find((item) => item.id === id);
|
|
72
|
-
|
|
73
|
-
const sensors = useSensors(
|
|
74
|
-
useSensor(PointerSensor),
|
|
75
|
-
useSensor(KeyboardSensor, {
|
|
76
|
-
coordinateGetter: sortableKeyboardCoordinates
|
|
77
|
-
})
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
const handleDragEnd = (event: DragEndEvent) => {
|
|
81
|
-
const { active, over } = event;
|
|
82
|
-
const oldIndex = itemIds.indexOf(active.id as string);
|
|
83
|
-
const newIndex = itemIds.indexOf(over.id as string);
|
|
84
|
-
const newItems = arrayMove(items, oldIndex, newIndex);
|
|
85
|
-
onDrop(event, newItems, oldIndex, newIndex);
|
|
86
|
-
setActiveId(null);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
43
|
const handleDragStart = (event: DragStartEvent) => {
|
|
90
|
-
setActiveId(event.active.id as string);
|
|
91
44
|
onDrag(event, itemIds.indexOf(event.active.id as string));
|
|
92
45
|
};
|
|
93
46
|
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
let content;
|
|
101
|
-
switch (variant) {
|
|
102
|
-
case 'DualListSelectorList':
|
|
103
|
-
content = (
|
|
104
|
-
<DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
|
|
105
|
-
{item.content}
|
|
106
|
-
</DraggableDualListSelectorListItem>
|
|
107
|
-
);
|
|
108
|
-
break;
|
|
109
|
-
case 'DataList':
|
|
110
|
-
content = (
|
|
111
|
-
<DraggableDataListItem key={item.id} id={item.id} {...item.props}>
|
|
112
|
-
{item.content}
|
|
113
|
-
</DraggableDataListItem>
|
|
114
|
-
);
|
|
115
|
-
break;
|
|
116
|
-
default:
|
|
117
|
-
content = (
|
|
118
|
-
<Draggable useDragButton={variant === 'default'} key={item.id} id={item.id} {...item.props}>
|
|
119
|
-
{item.content}
|
|
120
|
-
</Draggable>
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return (
|
|
125
|
-
<div
|
|
126
|
-
className={css(styles.draggable, styles.modifiers.dragging)}
|
|
127
|
-
style={
|
|
128
|
-
{
|
|
129
|
-
'--pf-v6-c-draggable--m-dragging--BackgroundColor': 'var(--pf-v6-global--BackgroundColor--100)'
|
|
130
|
-
} as React.CSSProperties
|
|
131
|
-
}
|
|
132
|
-
>
|
|
133
|
-
{content}
|
|
134
|
-
</div>
|
|
135
|
-
);
|
|
47
|
+
const handleDragEnd = (event: DragEndEvent, newItems: Record<string, DraggableObject[]>) => {
|
|
48
|
+
const { active, over } = event;
|
|
49
|
+
const oldIndex = itemIds.indexOf(active.id as string);
|
|
50
|
+
const newIndex = itemIds.indexOf(over.id as string);
|
|
51
|
+
onDrop(event, newItems[dropZoneId], oldIndex, newIndex);
|
|
136
52
|
};
|
|
137
53
|
|
|
138
|
-
const
|
|
139
|
-
<SortableContext items={itemIds} strategy={verticalListSortingStrategy} id="droppable">
|
|
140
|
-
{items.map((item: DraggableObject) => {
|
|
141
|
-
switch (variant) {
|
|
142
|
-
case 'DualListSelectorList':
|
|
143
|
-
return (
|
|
144
|
-
<DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
|
|
145
|
-
{item.content}
|
|
146
|
-
</DraggableDualListSelectorListItem>
|
|
147
|
-
);
|
|
148
|
-
case 'DataList':
|
|
149
|
-
return (
|
|
150
|
-
<DraggableDataListItem key={item.id} id={item.id} {...item.props}>
|
|
151
|
-
{item.content}
|
|
152
|
-
</DraggableDataListItem>
|
|
153
|
-
);
|
|
154
|
-
default:
|
|
155
|
-
return (
|
|
156
|
-
<Draggable useDragButton={variant === 'default'} key={item.id} id={item.id} {...item.props}>
|
|
157
|
-
{item.content}
|
|
158
|
-
</Draggable>
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
})}
|
|
162
|
-
<DragOverlay>{activeId && getDragOverlay()}</DragOverlay>
|
|
163
|
-
</SortableContext>
|
|
164
|
-
);
|
|
165
|
-
|
|
54
|
+
const dropZoneId = props.id ? props.id : 'droppable';
|
|
166
55
|
return (
|
|
167
|
-
<
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
{
|
|
56
|
+
<DragDropContainer
|
|
57
|
+
variant={variant}
|
|
58
|
+
items={{ [dropZoneId]: items }}
|
|
59
|
+
onDrag={handleDragStart}
|
|
60
|
+
onDrop={handleDragEnd}
|
|
61
|
+
overlayProps={overlayProps}
|
|
173
62
|
>
|
|
174
|
-
{children &&
|
|
175
|
-
|
|
176
|
-
children: renderedChildren
|
|
177
|
-
})}
|
|
178
|
-
{!children && <div>{renderedChildren}</div>}
|
|
179
|
-
</DndContext>
|
|
63
|
+
<Droppable items={items} id={dropZoneId} variant={variant} {...(children && { wrapper: children })} />
|
|
64
|
+
</DragDropContainer>
|
|
180
65
|
);
|
|
181
66
|
};
|
|
182
67
|
DragDropSort.displayName = 'DragDropSort';
|
|
@@ -5,7 +5,7 @@ import { css } from '@patternfly/react-styles';
|
|
|
5
5
|
import styles from '@patternfly/react-styles/css/components/DualListSelector/dual-list-selector';
|
|
6
6
|
import dragStyles from '@patternfly/react-styles/css/components/DragDrop/drag-drop';
|
|
7
7
|
import { DragButton } from './DragButton';
|
|
8
|
-
import { DualListSelectorListContext
|
|
8
|
+
import { DualListSelectorListContext } from '@patternfly/react-core/dist/esm/components/DualListSelector/DualListSelectorContext';
|
|
9
9
|
|
|
10
10
|
export interface DraggableDualListSelectorListItemProps extends React.HTMLProps<HTMLLIElement> {
|
|
11
11
|
/** Content rendered inside DragDrop */
|
|
@@ -42,7 +42,7 @@ export const DraggableDualListSelectorListItem: React.FunctionComponent<Draggabl
|
|
|
42
42
|
animateLayoutChanges: () => false
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
-
const { setFocusedOption } = React.useContext(
|
|
45
|
+
const { setFocusedOption } = React.useContext(DualListSelectorListContext);
|
|
46
46
|
|
|
47
47
|
const style = {
|
|
48
48
|
transform: CSS.Transform.toString(transform),
|
|
@@ -1,27 +1,76 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { useDroppable } from '@dnd-kit/core';
|
|
3
|
+
import { DraggableObject } from './DragDropContainer';
|
|
4
|
+
import { SortableContext, verticalListSortingStrategy } from '@dnd-kit/sortable';
|
|
5
|
+
import { DraggableDualListSelectorListItem } from './DraggableDualListSelectorListItem';
|
|
6
|
+
import { DraggableDataListItem } from './DraggableDataListItem';
|
|
7
|
+
import { Draggable } from './Draggable';
|
|
3
8
|
|
|
4
9
|
interface DroppableProps extends React.HTMLProps<HTMLDivElement> {
|
|
5
|
-
/**
|
|
6
|
-
|
|
7
|
-
/**
|
|
10
|
+
/** ID of the drop zone */
|
|
11
|
+
id?: string;
|
|
12
|
+
/** Additional classes added to the div, or cloned element if wrapper is used */
|
|
8
13
|
className?: string;
|
|
9
|
-
/**
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
|
|
14
|
+
/** Array of draggable objects */
|
|
15
|
+
items: DraggableObject[];
|
|
16
|
+
/** Alternative to wrapping drop zone in a div, will override any ref and style set on the element. */
|
|
17
|
+
wrapper?: React.ReactElement;
|
|
18
|
+
/** The variant determines which component wraps the draggable object.
|
|
19
|
+
* Default variant wraps the draggable object in a div.
|
|
20
|
+
* DataList vairant wraps the draggable object in a DataListItem
|
|
21
|
+
* DualListSelectorList variant wraps the draggable objects in a DualListSelectorListItem and a div.pf-c-dual-list-selector__item-text element
|
|
22
|
+
* TableComposable variant wraps the draggable objects in TODO
|
|
23
|
+
* */
|
|
24
|
+
variant?: 'default' | 'DataList' | 'DualListSelectorList' | 'TableComposable';
|
|
15
25
|
}
|
|
16
26
|
|
|
17
|
-
export const Droppable: React.FunctionComponent<DroppableProps> = ({
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
export const Droppable: React.FunctionComponent<DroppableProps> = ({
|
|
28
|
+
items,
|
|
29
|
+
id = 'droppable',
|
|
30
|
+
variant = 'default',
|
|
31
|
+
wrapper,
|
|
32
|
+
...props
|
|
33
|
+
}: DroppableProps) => {
|
|
34
|
+
const itemIds = React.useMemo(() => (items ? Array.from(items, (item) => item.id as string) : []), [items]);
|
|
35
|
+
const { setNodeRef } = useDroppable({ id: id ? id : 'droppable' });
|
|
36
|
+
|
|
37
|
+
const content = items.map((item: DraggableObject) => {
|
|
38
|
+
switch (variant) {
|
|
39
|
+
case 'DualListSelectorList':
|
|
40
|
+
return (
|
|
41
|
+
<DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
|
|
42
|
+
{item.content}
|
|
43
|
+
</DraggableDualListSelectorListItem>
|
|
44
|
+
);
|
|
45
|
+
case 'DataList':
|
|
46
|
+
return (
|
|
47
|
+
<DraggableDataListItem key={item.id} id={item.id} {...item.props}>
|
|
48
|
+
{item.content}
|
|
49
|
+
</DraggableDataListItem>
|
|
50
|
+
);
|
|
51
|
+
default:
|
|
52
|
+
return (
|
|
53
|
+
<Draggable useDragButton key={item.id} id={item.id} {...item.props}>
|
|
54
|
+
{item.content}
|
|
55
|
+
</Draggable>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
20
59
|
|
|
21
60
|
return (
|
|
22
|
-
<
|
|
23
|
-
{
|
|
24
|
-
|
|
61
|
+
<SortableContext items={itemIds} strategy={verticalListSortingStrategy} id={id}>
|
|
62
|
+
{wrapper &&
|
|
63
|
+
React.cloneElement(wrapper, {
|
|
64
|
+
children: content,
|
|
65
|
+
ref: setNodeRef,
|
|
66
|
+
...props
|
|
67
|
+
})}
|
|
68
|
+
{!wrapper && (
|
|
69
|
+
<div ref={setNodeRef} {...props}>
|
|
70
|
+
{content}
|
|
71
|
+
</div>
|
|
72
|
+
)}
|
|
73
|
+
</SortableContext>
|
|
25
74
|
);
|
|
26
75
|
};
|
|
27
76
|
Droppable.displayName = 'Droppable';
|
|
@@ -2,6 +2,11 @@ import React from 'react';
|
|
|
2
2
|
import { render } from '@testing-library/react';
|
|
3
3
|
import { DragDropSort } from '../';
|
|
4
4
|
|
|
5
|
+
jest.mock('react-dom', () => ({
|
|
6
|
+
...jest.requireActual('react-dom'),
|
|
7
|
+
createPortal: jest.fn((node) => node)
|
|
8
|
+
}));
|
|
9
|
+
|
|
5
10
|
test('renders some divs', () => {
|
|
6
11
|
const { asFragment } = render(
|
|
7
12
|
<div className="pf-c-droppable pf-m-dragging">
|
|
@@ -15,12 +15,18 @@ import AngleDoubleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-d
|
|
|
15
15
|
import AngleRightIcon from '@patternfly/react-icons/dist/esm/icons/angle-right-icon';
|
|
16
16
|
import PficonSortCommonAscIcon from '@patternfly/react-icons/dist/esm/icons/pficon-sort-common-asc-icon';
|
|
17
17
|
|
|
18
|
-
import { DragDropSort } from '@patternfly/react-drag-drop';
|
|
18
|
+
import { DragDropSort, DragDropContainer, Droppable as NewDroppable } from '@patternfly/react-drag-drop';
|
|
19
19
|
|
|
20
20
|
## Sorting examples
|
|
21
21
|
|
|
22
22
|
### Basic drag and drop sorting
|
|
23
23
|
|
|
24
|
-
```ts file="./
|
|
24
|
+
```ts file="./BasicSorting.tsx"
|
|
25
|
+
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Multiple drop zones
|
|
29
|
+
|
|
30
|
+
```ts file="./DragDropContainerBasic.tsx"
|
|
25
31
|
|
|
26
32
|
```
|