@patternfly/react-drag-drop 5.4.0-prerelease.3 → 5.4.0-prerelease.31

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.
Files changed (62) hide show
  1. package/CHANGELOG.md +123 -0
  2. package/dist/cjs.tsbuildinfo +1 -1
  3. package/dist/esm/next/components/DragDrop/DragDropContainer.d.ts +42 -0
  4. package/dist/esm/next/components/DragDrop/DragDropContainer.d.ts.map +1 -0
  5. package/dist/esm/next/components/DragDrop/DragDropContainer.js +147 -0
  6. package/dist/esm/next/components/DragDrop/DragDropContainer.js.map +1 -0
  7. package/dist/esm/next/components/DragDrop/DragDropSort.d.ts +5 -10
  8. package/dist/esm/next/components/DragDrop/DragDropSort.d.ts.map +1 -1
  9. package/dist/esm/next/components/DragDrop/DragDropSort.js +12 -59
  10. package/dist/esm/next/components/DragDrop/DragDropSort.js.map +1 -1
  11. package/dist/esm/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
  12. package/dist/esm/next/components/DragDrop/Droppable.d.ts +15 -9
  13. package/dist/esm/next/components/DragDrop/Droppable.d.ts.map +1 -1
  14. package/dist/esm/next/components/DragDrop/Droppable.js +21 -4
  15. package/dist/esm/next/components/DragDrop/Droppable.js.map +1 -1
  16. package/dist/esm/next/components/DragDrop/index.d.ts +2 -0
  17. package/dist/esm/next/components/DragDrop/index.d.ts.map +1 -1
  18. package/dist/esm/next/components/DragDrop/index.js +2 -0
  19. package/dist/esm/next/components/DragDrop/index.js.map +1 -1
  20. package/dist/esm.tsbuildinfo +1 -1
  21. package/dist/js/next/components/DragDrop/DragButton.js.map +1 -1
  22. package/dist/js/next/components/DragDrop/DragDropContainer.d.ts +42 -0
  23. package/dist/js/next/components/DragDrop/DragDropContainer.d.ts.map +1 -0
  24. package/dist/js/next/components/DragDrop/DragDropContainer.js +151 -0
  25. package/dist/js/next/components/DragDrop/DragDropContainer.js.map +1 -0
  26. package/dist/js/next/components/DragDrop/DragDropSort.d.ts +5 -10
  27. package/dist/js/next/components/DragDrop/DragDropSort.d.ts.map +1 -1
  28. package/dist/js/next/components/DragDrop/DragDropSort.js +12 -59
  29. package/dist/js/next/components/DragDrop/DragDropSort.js.map +1 -1
  30. package/dist/js/next/components/DragDrop/DraggableDualListSelectorListItem.js.map +1 -1
  31. package/dist/js/next/components/DragDrop/Droppable.d.ts +15 -9
  32. package/dist/js/next/components/DragDrop/Droppable.d.ts.map +1 -1
  33. package/dist/js/next/components/DragDrop/Droppable.js +21 -4
  34. package/dist/js/next/components/DragDrop/Droppable.js.map +1 -1
  35. package/dist/js/next/components/DragDrop/index.d.ts +2 -0
  36. package/dist/js/next/components/DragDrop/index.d.ts.map +1 -1
  37. package/dist/js/next/components/DragDrop/index.js +2 -0
  38. package/dist/js/next/components/DragDrop/index.js.map +1 -1
  39. package/package.json +6 -10
  40. package/src/next/components/DragDrop/DragDropContainer.tsx +305 -0
  41. package/src/next/components/DragDrop/DragDropSort.tsx +21 -133
  42. package/src/next/components/DragDrop/Droppable.tsx +69 -15
  43. package/src/next/components/DragDrop/__tests__/DragDrop.test.tsx +5 -0
  44. package/src/next/components/DragDrop/__tests__/__snapshots__/DragDrop.test.tsx.snap +81 -15
  45. package/src/next/components/DragDrop/examples/BasicSortingWithDragButton.tsx +0 -1
  46. package/src/next/components/DragDrop/examples/DataListDraggable.tsx +1 -0
  47. package/src/next/components/DragDrop/examples/DragDrop.md +4 -4
  48. package/src/next/components/DragDrop/examples/DragDropContainerBasic.tsx +45 -0
  49. package/src/next/components/DragDrop/examples/DragDropContainerDataList.tsx +75 -0
  50. package/src/next/components/DragDrop/examples/DragDropContainerDualListSelector.tsx +191 -0
  51. package/src/next/components/DragDrop/examples/DragDropDemos.md +25 -1
  52. package/src/next/components/DragDrop/index.ts +2 -0
  53. package/dist/esm/next/components/DragDrop/DroppableContext.d.ts +0 -6
  54. package/dist/esm/next/components/DragDrop/DroppableContext.d.ts.map +0 -1
  55. package/dist/esm/next/components/DragDrop/DroppableContext.js +0 -6
  56. package/dist/esm/next/components/DragDrop/DroppableContext.js.map +0 -1
  57. package/dist/js/next/components/DragDrop/DroppableContext.d.ts +0 -6
  58. package/dist/js/next/components/DragDrop/DroppableContext.d.ts.map +0 -1
  59. package/dist/js/next/components/DragDrop/DroppableContext.js +0 -10
  60. package/dist/js/next/components/DragDrop/DroppableContext.js.map +0 -1
  61. package/src/next/components/DragDrop/DroppableContext.ts +0 -6
  62. package/src/next/components/DragDrop/examples/BasicSorting.tsx +0 -19
@@ -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 vairant 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
+ * TableComposable variant wraps the draggable objects in TODO
65
+ * */
66
+ variant?: 'default' | 'DataList' | 'DualListSelectorList' | 'TableComposable';
67
+ /** Additional classes to apply to the drag overlay */
68
+ overlayProps?: any;
69
+ }
70
+
71
+ export const DragDropContainer: React.FunctionComponent<DragDropContainerProps> = ({
72
+ children,
73
+ items,
74
+ onDrag = () => {},
75
+ onContainerMove = () => {},
76
+ onDrop = () => {},
77
+ onCancel = () => {},
78
+ variant = 'default',
79
+ overlayProps,
80
+ ...props
81
+ }: DragDropContainerProps) => {
82
+ const itemsCopy = React.useRef<Record<string, DraggableObject[]> | null>(null);
83
+ const hasRecentlyMovedContainer = React.useRef(false);
84
+ const [activeId, setActiveId] = React.useState<UniqueIdentifier>(null);
85
+ const lastOverId = React.useRef<UniqueIdentifier | null>(null);
86
+
87
+ const findItem = React.useCallback(
88
+ (id: UniqueIdentifier, containerId: UniqueIdentifier) => items[containerId].find((item) => item.id === id),
89
+ [items]
90
+ );
91
+
92
+ const findContainer = React.useCallback(
93
+ (id: UniqueIdentifier) => {
94
+ if (id in items) {
95
+ return id;
96
+ }
97
+ return Object.keys(items).find((key) => items[key].find((obj) => obj.id === id));
98
+ },
99
+ [items]
100
+ );
101
+
102
+ const sensors = useSensors(
103
+ useSensor(PointerSensor),
104
+ useSensor(KeyboardSensor, {
105
+ coordinateGetter: sortableKeyboardCoordinates
106
+ })
107
+ );
108
+
109
+ const collisionDetectionStrategy: CollisionDetection = React.useCallback(
110
+ (args) => {
111
+ if (activeId && activeId in items) {
112
+ return closestCenter({
113
+ ...args,
114
+ droppableContainers: args.droppableContainers.filter((container) => container.id in items)
115
+ });
116
+ }
117
+
118
+ const pointerIntersections = pointerWithin(args);
119
+ const intersections = pointerIntersections.length > 0 ? pointerIntersections : rectIntersection(args);
120
+ let overId = getFirstCollision(intersections, 'id');
121
+
122
+ if (overId != null) {
123
+ if (overId in items) {
124
+ const containerItems = items[overId];
125
+
126
+ if (containerItems.length > 0) {
127
+ overId = closestCenter({
128
+ ...args,
129
+ droppableContainers: args.droppableContainers.filter(
130
+ (container) => container.id !== overId && containerItems.find((obj) => obj.id === container.id)
131
+ )
132
+ })[0]?.id;
133
+ }
134
+ }
135
+
136
+ lastOverId.current = overId;
137
+
138
+ return [{ id: overId }];
139
+ }
140
+
141
+ if (hasRecentlyMovedContainer.current) {
142
+ lastOverId.current = activeId;
143
+ }
144
+ return lastOverId.current ? [{ id: lastOverId.current }] : [];
145
+ },
146
+ [activeId, items]
147
+ );
148
+
149
+ React.useEffect(() => {
150
+ requestAnimationFrame(() => {
151
+ hasRecentlyMovedContainer.current = false;
152
+ });
153
+ }, [items]);
154
+
155
+ const handleDragStart = (event: DragStartEvent) => {
156
+ const { active } = event;
157
+ itemsCopy.current = { ...items };
158
+ setActiveId(active.id);
159
+
160
+ onDrag(event);
161
+ };
162
+
163
+ const handleDragOver = (event: DragOverEvent) => {
164
+ const { active, over } = event;
165
+ const { id: activeId } = active;
166
+ const { id: overId } = over;
167
+
168
+ if (!overId || activeId in items) {
169
+ return;
170
+ }
171
+
172
+ const activeContainer = findContainer(activeId);
173
+ const overContainer = findContainer(overId);
174
+
175
+ if (!overContainer || !activeContainer) {
176
+ return;
177
+ }
178
+
179
+ if (activeContainer !== overContainer) {
180
+ const activeItems = items[activeContainer];
181
+ const overItems = items[overContainer];
182
+ const overIndex = overItems.findIndex((draggableItem) => draggableItem.id === overId);
183
+ const activeIndex = activeItems.findIndex((draggableItem) => draggableItem.id === activeId);
184
+
185
+ const isBelowOverItem =
186
+ over && active.rect.current.translated && active.rect.current.translated.top > over.rect.top + over.rect.height;
187
+
188
+ const modifier = isBelowOverItem ? 1 : 0;
189
+ const newIndex = overIndex >= 0 ? overIndex + modifier : overItems.length + 1;
190
+
191
+ const newItems = {
192
+ ...items,
193
+ [activeContainer]: items[activeContainer].filter((item) => item.id !== active.id),
194
+ [overContainer]: [
195
+ ...items[overContainer].slice(0, newIndex),
196
+ items[activeContainer][activeIndex],
197
+ ...items[overContainer].slice(newIndex, items[overContainer].length)
198
+ ]
199
+ };
200
+
201
+ hasRecentlyMovedContainer.current = true;
202
+ onContainerMove(event, newItems);
203
+ }
204
+ };
205
+
206
+ const handleDragEnd = (event: DragEndEvent) => {
207
+ const { active, over } = event;
208
+ const { id: activeId } = active;
209
+ const { id: overId } = over;
210
+
211
+ const activeContainer = findContainer(activeId);
212
+ if (!over || !activeContainer) {
213
+ setActiveId(null);
214
+ return;
215
+ }
216
+
217
+ const overContainer = findContainer(overId);
218
+ if (!overContainer) {
219
+ setActiveId(null);
220
+ return;
221
+ }
222
+
223
+ const activeIndex = items[activeContainer].findIndex((draggableItem) => draggableItem.id === activeId);
224
+ const overIndex = items[overContainer].findIndex((draggableItem) => draggableItem.id === overId);
225
+
226
+ if (activeIndex !== overIndex) {
227
+ const newItems = { ...items, [overContainer]: arrayMove(items[overContainer], activeIndex, overIndex) };
228
+ onDrop(event, newItems);
229
+ }
230
+ setActiveId(null);
231
+ };
232
+
233
+ const handleDragCancel = (event: DragCancelEvent) => {
234
+ onCancel(event, itemsCopy.current);
235
+ itemsCopy.current = null;
236
+ setActiveId(null);
237
+ };
238
+
239
+ const getDragOverlay = () => {
240
+ if (!activeId) {
241
+ return;
242
+ }
243
+ const item = findItem(activeId, findContainer(activeId));
244
+
245
+ let content;
246
+ switch (variant) {
247
+ case 'DualListSelectorList':
248
+ content = (
249
+ <DraggableDualListSelectorListItem key={item.id} id={item.id} {...item.props}>
250
+ {item.content}
251
+ </DraggableDualListSelectorListItem>
252
+ );
253
+ break;
254
+ case 'DataList':
255
+ content = (
256
+ <DraggableDataListItem key={item.id} id={item.id} {...item.props}>
257
+ {item.content}
258
+ </DraggableDataListItem>
259
+ );
260
+ break;
261
+ default:
262
+ content = (
263
+ <Draggable useDragButton={variant === 'default'} key={item.id} id={item.id} {...item.props}>
264
+ {item.content}
265
+ </Draggable>
266
+ );
267
+ }
268
+
269
+ return (
270
+ <div
271
+ className={css(styles.draggable, styles.modifiers.dragging)}
272
+ style={
273
+ {
274
+ '--pf-v5-c-draggable--m-dragging--BackgroundColor': 'var(--pf-v5-global--BackgroundColor--100)'
275
+ } as React.CSSProperties
276
+ }
277
+ >
278
+ {variant === 'DualListSelectorList' && <ul className="pf-v5-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 { css } from '@patternfly/react-styles';
3
- import {
4
- DndContext,
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
  */
@@ -55,6 +26,8 @@ export interface DragDropSortProps extends DndContextProps {
55
26
  * TableComposable variant wraps the draggable objects in TODO
56
27
  * */
57
28
  variant?: 'default' | 'defaultWithHandle' | 'DataList' | 'DualListSelectorList' | 'TableComposable';
29
+ /** Additional classes to apply to the drag overlay */
30
+ overlayProps?: any;
58
31
  }
59
32
 
60
33
  export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
@@ -63,120 +36,35 @@ export const DragDropSort: React.FunctionComponent<DragDropSortProps> = ({
63
36
  onDrag = () => {},
64
37
  variant = 'default',
65
38
  children,
39
+ overlayProps,
66
40
  ...props
67
41
  }: DragDropSortProps) => {
68
- const [activeId, setActiveId] = React.useState<string>(null);
69
42
  const itemIds = React.useMemo(() => (items ? Array.from(items, (item) => item.id as string) : []), [items]);
70
43
 
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
44
  const handleDragStart = (event: DragStartEvent) => {
90
- setActiveId(event.active.id as string);
91
45
  onDrag(event, itemIds.indexOf(event.active.id as string));
92
46
  };
93
47
 
94
- const getDragOverlay = () => {
95
- if (!activeId) {
96
- return;
97
- }
98
- const item = getItemById(activeId);
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 === 'defaultWithHandle'} 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-v5-c-draggable--m-dragging--BackgroundColor': 'var(--pf-v5-global--BackgroundColor--100)'
130
- } as React.CSSProperties
131
- }
132
- >
133
- {content}
134
- </div>
135
- );
48
+ const handleDragEnd = (event: DragEndEvent, newItems: Record<string, DraggableObject[]>) => {
49
+ const { active, over } = event;
50
+ const oldIndex = itemIds.indexOf(active.id as string);
51
+ const newIndex = itemIds.indexOf(over.id as string);
52
+ onDrop(event, newItems[dropZoneId], oldIndex, newIndex);
136
53
  };
137
54
 
138
- const renderedChildren = (
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 === 'defaultWithHandle'} key={item.id} id={item.id} {...item.props}>
157
- {item.content}
158
- </Draggable>
159
- );
160
- }
161
- })}
162
- <DragOverlay>{activeId && getDragOverlay()}</DragOverlay>
163
- </SortableContext>
164
- );
55
+ const dropZoneId = props.id ? props.id : 'droppable';
56
+ const containerVariant = variant === 'defaultWithHandle' ? 'default' : variant;
165
57
 
166
58
  return (
167
- <DndContext
168
- sensors={sensors}
169
- collisionDetection={closestCenter}
170
- onDragEnd={handleDragEnd}
171
- onDragStart={handleDragStart}
172
- {...props}
59
+ <DragDropContainer
60
+ variant={containerVariant}
61
+ items={{ [dropZoneId]: items }}
62
+ onDrag={handleDragStart}
63
+ onDrop={handleDragEnd}
64
+ overlayProps={overlayProps}
173
65
  >
174
- {children &&
175
- React.cloneElement(children, {
176
- children: renderedChildren
177
- })}
178
- {!children && <div>{renderedChildren}</div>}
179
- </DndContext>
66
+ <Droppable items={items} id={dropZoneId} variant={variant} {...(children && { wrapper: children })} />
67
+ </DragDropContainer>
180
68
  );
181
69
  };
182
70
  DragDropSort.displayName = 'DragDropSort';
@@ -1,27 +1,81 @@
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
- /** Content rendered inside DragDrop */
6
- children?: React.ReactNode;
7
- /** Class to add to outer div */
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
- /** Name of zone that items can be dragged between. Should specify if there is more than one Droppable on the page. */
10
- zone?: string;
11
- /** Id to be passed back on drop events */
12
- droppableId?: string;
13
- /** Don't wrap the component in a div. Requires passing a single child. */
14
- hasNoWrapper?: boolean;
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' | 'defaultWithHandle' | 'DataList' | 'DualListSelectorList' | 'TableComposable';
15
25
  }
16
26
 
17
- export const Droppable: React.FunctionComponent<DroppableProps> = ({ children, ...props }: DroppableProps) => {
18
- const { isOver, setNodeRef } = useDroppable({ id: 'droppable' });
19
- const style = { color: isOver ? 'green' : undefined };
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
54
+ useDragButton={variant === 'default' || variant === 'defaultWithHandle'}
55
+ key={item.id}
56
+ id={item.id}
57
+ {...item.props}
58
+ >
59
+ {item.content}
60
+ </Draggable>
61
+ );
62
+ }
63
+ });
20
64
 
21
65
  return (
22
- <div ref={setNodeRef} style={style} {...props}>
23
- {children}
24
- </div>
66
+ <SortableContext items={itemIds} strategy={verticalListSortingStrategy} id={id}>
67
+ {wrapper &&
68
+ React.cloneElement(wrapper, {
69
+ children: content,
70
+ ref: setNodeRef,
71
+ ...props
72
+ })}
73
+ {!wrapper && (
74
+ <div ref={setNodeRef} {...props}>
75
+ {content}
76
+ </div>
77
+ )}
78
+ </SortableContext>
25
79
  );
26
80
  };
27
81
  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">