playbook_ui 15.5.0 → 15.6.0.pre.alpha.draggableask12898
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.
- checksums.yaml +4 -4
- data/app/pb_kits/playbook/pb_advanced_table/_advanced_table.scss +96 -6
- data/app/pb_kits/playbook/pb_advanced_table/docs/_advanced_table_table_props.html.erb +1 -1
- data/app/pb_kits/playbook/pb_background/_background.tsx +6 -6
- data/app/pb_kits/playbook/pb_background/background.test.js +5 -1
- data/app/pb_kits/playbook/pb_background/docs/_background_light.html.erb +1 -1
- data/app/pb_kits/playbook/pb_background/docs/_background_light.jsx +0 -1
- data/app/pb_kits/playbook/pb_background/docs/_background_light.md +1 -0
- data/app/pb_kits/playbook/pb_background/docs/example.yml +2 -2
- data/app/pb_kits/playbook/pb_dialog/docs/_dialog_compound_components.html.erb +31 -0
- data/app/pb_kits/playbook/pb_draggable/context/index.tsx +455 -7
- data/app/pb_kits/playbook/pb_draggable/context/types.ts +8 -3
- data/app/pb_kits/playbook/pb_draggable/docs/_draggable_multiple_containers_dropzone.jsx +180 -0
- data/app/pb_kits/playbook/pb_draggable/docs/_draggable_multiple_containers_dropzone.md +22 -0
- data/app/pb_kits/playbook/pb_draggable/docs/example.yml +3 -2
- data/app/pb_kits/playbook/pb_draggable/docs/index.js +2 -1
- data/app/pb_kits/playbook/pb_draggable/draggable.test.jsx +77 -1
- data/app/pb_kits/playbook/pb_file_upload/_file_upload.scss +4 -4
- data/app/pb_kits/playbook/pb_home_address_street/_home_address_street.tsx +34 -22
- data/app/pb_kits/playbook/pb_home_address_street/city_emphasis.html.erb +16 -12
- data/app/pb_kits/playbook/pb_home_address_street/docs/_home_address_street_default.html.erb +1 -1
- data/app/pb_kits/playbook/pb_home_address_street/none_emphasis.html.erb +16 -12
- data/app/pb_kits/playbook/pb_home_address_street/street_emphasis.html.erb +16 -12
- data/app/pb_kits/playbook/pb_multiple_users/_multiple_users.scss +10 -0
- data/app/pb_kits/playbook/pb_multiple_users/_multiple_users.tsx +66 -15
- data/app/pb_kits/playbook/pb_multiple_users/docs/_multiple_users_with_tooltip.jsx +42 -0
- data/app/pb_kits/playbook/pb_multiple_users/docs/_multiple_users_with_tooltip.md +1 -0
- data/app/pb_kits/playbook/pb_multiple_users/docs/example.yml +1 -0
- data/app/pb_kits/playbook/pb_multiple_users/docs/index.js +1 -0
- data/app/pb_kits/playbook/pb_multiple_users/multiple_users.test.js +25 -0
- data/app/pb_kits/playbook/pb_phone_number_input/_phone_number_input.tsx +44 -10
- data/app/pb_kits/playbook/pb_phone_number_input/docs/_phone_number_input_validation.html.erb +34 -4
- data/app/pb_kits/playbook/pb_phone_number_input/docs/_phone_number_input_validation.jsx +16 -7
- data/app/pb_kits/playbook/pb_table/styles/_vertical_border.scss +49 -0
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.test.jsx +15 -0
- data/app/pb_kits/playbook/pb_typeahead/_typeahead.tsx +3 -0
- data/app/pb_kits/playbook/pb_typeahead/components/ClearIndicator.tsx +13 -2
- data/app/pb_kits/playbook/pb_typeahead/components/MultiValue.tsx +6 -1
- data/app/pb_kits/playbook/pb_typeahead/components/ValueContainer.tsx +34 -7
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_input_display.html.erb +30 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_input_display.jsx +37 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/_typeahead_input_display.md +3 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/example.yml +2 -0
- data/app/pb_kits/playbook/pb_typeahead/docs/index.js +2 -1
- data/app/pb_kits/playbook/pb_typeahead/typeahead.rb +6 -1
- data/dist/chunks/_typeahead-319r1pyq.js +6 -0
- data/dist/chunks/{lib-Dk4GKPut.js → lib-CgpqUb6l.js} +2 -2
- data/dist/chunks/vendor.js +2 -2
- data/dist/playbook-rails-react-bindings.js +1 -1
- data/dist/playbook-rails.js +1 -1
- data/dist/playbook.css +1 -1
- data/lib/playbook/version.rb +2 -2
- metadata +12 -5
- data/app/pb_kits/playbook/pb_bar_graph/BarGraphStyles.scss +0 -58
- data/dist/chunks/_typeahead-Bx4QsIEU.js +0 -6
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { createContext, useReducer, useContext, useEffect, useMemo } from "react";
|
|
1
|
+
import React, { createContext, useReducer, useContext, useEffect, useMemo, useRef } from "react";
|
|
2
2
|
import { InitialStateType, ActionType, DraggableProviderType } from "./types";
|
|
3
3
|
|
|
4
4
|
const initialState: InitialStateType = {
|
|
@@ -39,6 +39,72 @@ const reducer = (state: InitialStateType, action: ActionType) => {
|
|
|
39
39
|
|
|
40
40
|
return { ...state, items: newItems };
|
|
41
41
|
}
|
|
42
|
+
|
|
43
|
+
// Used only when enableCrossContainerPreview is true
|
|
44
|
+
case "REORDER_ITEMS_CROSS_CONTAINER": {
|
|
45
|
+
const { dragId, targetId, newContainer } = action.payload;
|
|
46
|
+
const newItems = [...state.items];
|
|
47
|
+
const draggedItem = newItems.find((item) => item && item.id === dragId);
|
|
48
|
+
|
|
49
|
+
if (!draggedItem) return state;
|
|
50
|
+
|
|
51
|
+
const draggedIndex = newItems.indexOf(draggedItem);
|
|
52
|
+
const targetIndex = newItems.findIndex(
|
|
53
|
+
(item) => item && item.id === targetId
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
if (draggedIndex === -1 || targetIndex === -1) return state;
|
|
57
|
+
|
|
58
|
+
const updatedItem = { ...draggedItem, container: newContainer };
|
|
59
|
+
newItems.splice(draggedIndex, 1);
|
|
60
|
+
newItems.splice(targetIndex, 0, updatedItem);
|
|
61
|
+
|
|
62
|
+
return { ...state, items: newItems };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Used only when enableCrossContainerPreview is true
|
|
66
|
+
case "MOVE_TO_CONTAINER_END": {
|
|
67
|
+
const { dragId, newContainer } = action.payload;
|
|
68
|
+
const newItems = [...state.items];
|
|
69
|
+
const draggedItem = newItems.find((item) => item && item.id === dragId);
|
|
70
|
+
|
|
71
|
+
if (!draggedItem) return state;
|
|
72
|
+
|
|
73
|
+
const draggedIndex = newItems.indexOf(draggedItem);
|
|
74
|
+
if (draggedIndex === -1) return state;
|
|
75
|
+
|
|
76
|
+
const updatedItem = { ...draggedItem, container: newContainer };
|
|
77
|
+
|
|
78
|
+
// Remove from current position
|
|
79
|
+
newItems.splice(draggedIndex, 1);
|
|
80
|
+
|
|
81
|
+
// Insert at end of target container
|
|
82
|
+
const lastIndexInContainer = newItems
|
|
83
|
+
.map((item) => item && item.container)
|
|
84
|
+
.lastIndexOf(newContainer);
|
|
85
|
+
|
|
86
|
+
if (lastIndexInContainer === -1) {
|
|
87
|
+
newItems.push(updatedItem);
|
|
88
|
+
} else {
|
|
89
|
+
newItems.splice(lastIndexInContainer + 1, 0, updatedItem);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { ...state, items: newItems };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Reset item back to its original container (e.g., when drag ends without valid drop)
|
|
96
|
+
case "RESET_DRAG_CONTAINER": {
|
|
97
|
+
const { itemId, originalContainer } = action.payload;
|
|
98
|
+
return {
|
|
99
|
+
...state,
|
|
100
|
+
items: state.items.map(item =>
|
|
101
|
+
item.id === itemId
|
|
102
|
+
? { ...item, container: originalContainer }
|
|
103
|
+
: item
|
|
104
|
+
)
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
42
108
|
default:
|
|
43
109
|
return state;
|
|
44
110
|
}
|
|
@@ -61,9 +127,32 @@ export const DraggableProvider = ({
|
|
|
61
127
|
onDrop,
|
|
62
128
|
onDragOver,
|
|
63
129
|
dropZone = { type: 'ghost', color: 'neutral', direction: 'vertical' },
|
|
64
|
-
providerId = 'default', // fallback provided for backward compatibility
|
|
130
|
+
providerId = 'default', // fallback provided for backward compatibility
|
|
131
|
+
// Opt-in flag for cross-container preview
|
|
132
|
+
enableCrossContainerPreview = false,
|
|
65
133
|
}: DraggableProviderType) => {
|
|
66
134
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
135
|
+
|
|
136
|
+
// Track drag state for global listener
|
|
137
|
+
const dragStateRef = useRef<{
|
|
138
|
+
isDragging: boolean;
|
|
139
|
+
draggedItemId: string;
|
|
140
|
+
originalContainer: string;
|
|
141
|
+
currentContainer: string;
|
|
142
|
+
dropOccurred: boolean;
|
|
143
|
+
}>({
|
|
144
|
+
isDragging: false,
|
|
145
|
+
draggedItemId: '',
|
|
146
|
+
originalContainer: '',
|
|
147
|
+
currentContainer: '',
|
|
148
|
+
dropOccurred: false,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Track current state for use in gated event listeners (avoid stale closures)
|
|
152
|
+
const stateRef = useRef(state);
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
stateRef.current = state;
|
|
155
|
+
}, [state]);
|
|
67
156
|
|
|
68
157
|
// Parse dropZone prop - handle both string format (backward compatibility) and object format
|
|
69
158
|
let dropZoneType = 'ghost';
|
|
@@ -93,7 +182,224 @@ export const DraggableProvider = ({
|
|
|
93
182
|
onReorder(state.items);
|
|
94
183
|
}, [state.items]);
|
|
95
184
|
|
|
185
|
+
// Monitor for failed drops by detecting mouse/pointer release during drag (this is needed for cross container preview)
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
if (!enableCrossContainerPreview) return;
|
|
188
|
+
|
|
189
|
+
// Allow drops anywhere on the document by preventing default dragover
|
|
190
|
+
const handleGlobalDragOver = (e: DragEvent) => {
|
|
191
|
+
if (dragStateRef.current.isDragging) {
|
|
192
|
+
e.preventDefault();
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// Handle drops anywhere on the document (including non-container areas)
|
|
197
|
+
const handleGlobalDrop = (e: DragEvent) => {
|
|
198
|
+
if (!dragStateRef.current.isDragging) return;
|
|
199
|
+
|
|
200
|
+
// If a container already handled the drop, don't process again
|
|
201
|
+
if (dragStateRef.current.dropOccurred) return;
|
|
202
|
+
|
|
203
|
+
e.preventDefault();
|
|
204
|
+
|
|
205
|
+
const currentContainer = dragStateRef.current.currentContainer;
|
|
206
|
+
|
|
207
|
+
// If item is in a different container than original, treat it as a successful drop
|
|
208
|
+
if (currentContainer && currentContainer !== dragStateRef.current.originalContainer) {
|
|
209
|
+
|
|
210
|
+
// Mark as dropped so other handlers know
|
|
211
|
+
dragStateRef.current.dropOccurred = true;
|
|
212
|
+
|
|
213
|
+
// Trigger onDrop callback with the current container
|
|
214
|
+
if (onDrop) {
|
|
215
|
+
const draggedItem = stateRef.current.items.find(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
216
|
+
const updatedItem = draggedItem ? { ...draggedItem, container: currentContainer } : null;
|
|
217
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === currentContainer);
|
|
218
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
219
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
220
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
221
|
+
|
|
222
|
+
onDrop(
|
|
223
|
+
dragStateRef.current.draggedItemId,
|
|
224
|
+
currentContainer,
|
|
225
|
+
dragStateRef.current.originalContainer,
|
|
226
|
+
updatedItem,
|
|
227
|
+
itemAbove,
|
|
228
|
+
itemBelow
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// Trigger onDragEnd callback
|
|
233
|
+
if (onDragEnd) {
|
|
234
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === currentContainer);
|
|
235
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
236
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
237
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
238
|
+
|
|
239
|
+
onDragEnd(
|
|
240
|
+
dragStateRef.current.draggedItemId,
|
|
241
|
+
currentContainer,
|
|
242
|
+
dragStateRef.current.originalContainer,
|
|
243
|
+
itemAbove,
|
|
244
|
+
itemBelow
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
dispatch({ type: 'SET_IS_DRAGGING', payload: "" });
|
|
249
|
+
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: "" });
|
|
250
|
+
dispatch({ type: 'SET_DRAG_DATA', payload: { id: "", initialGroup: "", originId: "" } });
|
|
251
|
+
} else {
|
|
252
|
+
// Reset to original container (item didn't move or dropped in invalid zone)
|
|
253
|
+
const originalContainer = dragStateRef.current.originalContainer;
|
|
254
|
+
|
|
255
|
+
dispatch({
|
|
256
|
+
type: 'RESET_DRAG_CONTAINER',
|
|
257
|
+
payload: {
|
|
258
|
+
itemId: dragStateRef.current.draggedItemId,
|
|
259
|
+
originalContainer: originalContainer,
|
|
260
|
+
},
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Call onDrop with original container info
|
|
264
|
+
if (onDrop) {
|
|
265
|
+
const draggedItem = stateRef.current.items.find(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
266
|
+
const updatedItem = draggedItem ? { ...draggedItem, container: originalContainer } : null;
|
|
267
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === originalContainer);
|
|
268
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
269
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
270
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
271
|
+
|
|
272
|
+
onDrop(
|
|
273
|
+
dragStateRef.current.draggedItemId,
|
|
274
|
+
originalContainer,
|
|
275
|
+
originalContainer,
|
|
276
|
+
updatedItem,
|
|
277
|
+
itemAbove,
|
|
278
|
+
itemBelow
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
dispatch({ type: 'SET_IS_DRAGGING', payload: "" });
|
|
283
|
+
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: "" });
|
|
284
|
+
dispatch({ type: 'SET_DRAG_DATA', payload: { id: "", initialGroup: "", originId: "" } });
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
const handleGlobalMouseUp = () => {
|
|
289
|
+
// If we're dragging and mouse is released, wait a bit to see if drop occurs
|
|
290
|
+
if (dragStateRef.current.isDragging) {
|
|
291
|
+
setTimeout(() => {
|
|
292
|
+
const currentContainer = dragStateRef.current.currentContainer;
|
|
293
|
+
|
|
294
|
+
// If drop still hasn't occurred, check if item is in a different container
|
|
295
|
+
if (dragStateRef.current.isDragging && !dragStateRef.current.dropOccurred) {
|
|
296
|
+
// If item is in a different container than original, treat it as a successful drop
|
|
297
|
+
if (currentContainer && currentContainer !== dragStateRef.current.originalContainer) {
|
|
298
|
+
// Trigger onDrop callback with the current container
|
|
299
|
+
if (onDrop) {
|
|
300
|
+
const draggedItem = stateRef.current.items.find(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
301
|
+
const updatedItem = draggedItem ? { ...draggedItem, container: currentContainer } : null;
|
|
302
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === currentContainer);
|
|
303
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === dragStateRef.current.draggedItemId);
|
|
304
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
305
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
306
|
+
|
|
307
|
+
onDrop(
|
|
308
|
+
dragStateRef.current.draggedItemId,
|
|
309
|
+
currentContainer,
|
|
310
|
+
dragStateRef.current.originalContainer,
|
|
311
|
+
updatedItem,
|
|
312
|
+
itemAbove,
|
|
313
|
+
itemBelow
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
} else {
|
|
317
|
+
dispatch({
|
|
318
|
+
type: 'RESET_DRAG_CONTAINER',
|
|
319
|
+
payload: {
|
|
320
|
+
itemId: dragStateRef.current.draggedItemId,
|
|
321
|
+
originalContainer: dragStateRef.current.originalContainer,
|
|
322
|
+
},
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
dispatch({ type: 'SET_IS_DRAGGING', payload: "" });
|
|
326
|
+
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: "" });
|
|
327
|
+
dispatch({ type: 'SET_DRAG_DATA', payload: { id: "", initialGroup: "", originId: "" } });
|
|
328
|
+
|
|
329
|
+
// Clear drag state
|
|
330
|
+
dragStateRef.current = {
|
|
331
|
+
isDragging: false,
|
|
332
|
+
draggedItemId: '',
|
|
333
|
+
originalContainer: '',
|
|
334
|
+
currentContainer: '',
|
|
335
|
+
dropOccurred: false,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
}, 50); // Small delay to let drop event fire if it's going to
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
document.addEventListener('dragover', handleGlobalDragOver);
|
|
343
|
+
document.addEventListener('drop', handleGlobalDrop);
|
|
344
|
+
document.addEventListener('mouseup', handleGlobalMouseUp);
|
|
345
|
+
document.addEventListener('pointerup', handleGlobalMouseUp);
|
|
346
|
+
|
|
347
|
+
return () => {
|
|
348
|
+
document.removeEventListener('dragover', handleGlobalDragOver);
|
|
349
|
+
document.removeEventListener('drop', handleGlobalDrop);
|
|
350
|
+
document.removeEventListener('mouseup', handleGlobalMouseUp);
|
|
351
|
+
document.removeEventListener('pointerup', handleGlobalMouseUp);
|
|
352
|
+
};
|
|
353
|
+
}, [enableCrossContainerPreview]);
|
|
354
|
+
|
|
355
|
+
// Detect when dragging stops (isDragging goes from truthy to empty)
|
|
356
|
+
const prevIsDraggingRef = useRef(state.isDragging);
|
|
357
|
+
|
|
358
|
+
useEffect(() => {
|
|
359
|
+
if (!enableCrossContainerPreview) return;
|
|
360
|
+
|
|
361
|
+
const wasDragging = prevIsDraggingRef.current;
|
|
362
|
+
const isNowDragging = state.isDragging;
|
|
363
|
+
|
|
364
|
+
// Drag just ended (was dragging, now not)
|
|
365
|
+
if (wasDragging && !isNowDragging) {
|
|
366
|
+
|
|
367
|
+
// If drop didn't occur, reset to original container
|
|
368
|
+
if (!dragStateRef.current.dropOccurred && dragStateRef.current.draggedItemId) {
|
|
369
|
+
dispatch({
|
|
370
|
+
type: 'RESET_DRAG_CONTAINER',
|
|
371
|
+
payload: {
|
|
372
|
+
itemId: dragStateRef.current.draggedItemId,
|
|
373
|
+
originalContainer: dragStateRef.current.originalContainer,
|
|
374
|
+
},
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Clear drag state
|
|
379
|
+
dragStateRef.current = {
|
|
380
|
+
isDragging: false,
|
|
381
|
+
draggedItemId: '',
|
|
382
|
+
originalContainer: '',
|
|
383
|
+
currentContainer: '',
|
|
384
|
+
dropOccurred: false,
|
|
385
|
+
};
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
prevIsDraggingRef.current = isNowDragging;
|
|
389
|
+
}, [state.isDragging, enableCrossContainerPreview]);
|
|
390
|
+
|
|
96
391
|
const handleDragStart = (id: string, container: string) => {
|
|
392
|
+
// Track drag in ref for global listener
|
|
393
|
+
if (enableCrossContainerPreview) {
|
|
394
|
+
dragStateRef.current = {
|
|
395
|
+
isDragging: true,
|
|
396
|
+
draggedItemId: id,
|
|
397
|
+
originalContainer: container,
|
|
398
|
+
currentContainer: container,
|
|
399
|
+
dropOccurred: false,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
97
403
|
dispatch({ type: 'SET_DRAG_DATA', payload: { id: id, initialGroup: container, originId: providerId } });
|
|
98
404
|
dispatch({ type: 'SET_IS_DRAGGING', payload: id });
|
|
99
405
|
if (onDragStart) onDragStart(id, container);
|
|
@@ -103,17 +409,93 @@ export const DraggableProvider = ({
|
|
|
103
409
|
if (state.dragData.originId !== providerId) return; // Ignore drag events from other providers
|
|
104
410
|
|
|
105
411
|
if (state.dragData.id !== id) {
|
|
106
|
-
|
|
107
|
-
|
|
412
|
+
if (enableCrossContainerPreview) {
|
|
413
|
+
// Used only when enableCrossContainerPreview is true
|
|
414
|
+
const draggedItem = state.items.find(
|
|
415
|
+
(item) => item && item.id === state.dragData.id
|
|
416
|
+
);
|
|
417
|
+
const currentContainer =
|
|
418
|
+
draggedItem && draggedItem.container
|
|
419
|
+
? draggedItem.container
|
|
420
|
+
: state.dragData.initialGroup;
|
|
421
|
+
|
|
422
|
+
const isCrossContainer =
|
|
423
|
+
currentContainer !== container &&
|
|
424
|
+
(currentContainer !== undefined || container !== undefined);
|
|
425
|
+
|
|
426
|
+
if (isCrossContainer) {
|
|
427
|
+
dispatch({
|
|
428
|
+
type: "REORDER_ITEMS_CROSS_CONTAINER",
|
|
429
|
+
payload: {
|
|
430
|
+
dragId: state.dragData.id,
|
|
431
|
+
targetId: id,
|
|
432
|
+
newContainer: container,
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
// Update current container in ref
|
|
436
|
+
if (enableCrossContainerPreview) {
|
|
437
|
+
dragStateRef.current.currentContainer = container;
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
// Same container: keep original behavior
|
|
441
|
+
dispatch({
|
|
442
|
+
type: "REORDER_ITEMS",
|
|
443
|
+
payload: { dragId: state.dragData.id, targetId: id },
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
} else {
|
|
447
|
+
// Original behavior (no preview across containers)
|
|
448
|
+
dispatch({type: "REORDER_ITEMS", payload: { dragId: state.dragData.id, targetId: id }});
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// When enableCrossContainerPreview is true, preserve the original initialGroup
|
|
452
|
+
// Otherwise, update it to track the current container
|
|
453
|
+
const newInitialGroup = enableCrossContainerPreview ? state.dragData.initialGroup : container;
|
|
454
|
+
dispatch({type: "SET_DRAG_DATA",payload: {id: state.dragData.id, initialGroup: newInitialGroup, originId: providerId}});
|
|
108
455
|
}
|
|
109
456
|
if (onDragEnter) onDragEnter(id, container);
|
|
110
457
|
};
|
|
111
458
|
|
|
112
459
|
const handleDragEnd = () => {
|
|
460
|
+
const draggedItemId = state.dragData.id;
|
|
461
|
+
const originalContainer = state.dragData.initialGroup;
|
|
462
|
+
|
|
463
|
+
// If enableCrossContainerPreview is true and no drop occurred, reset item to original container
|
|
464
|
+
if (enableCrossContainerPreview && !dragStateRef.current.dropOccurred && draggedItemId && originalContainer) {
|
|
465
|
+
dispatch({ type: 'RESET_DRAG_CONTAINER', payload: { itemId: draggedItemId, originalContainer } });
|
|
466
|
+
}
|
|
467
|
+
|
|
113
468
|
dispatch({ type: 'SET_IS_DRAGGING', payload: "" });
|
|
114
469
|
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: "" });
|
|
115
470
|
dispatch({ type: 'SET_DRAG_DATA', payload: { id: "", initialGroup: "", originId: "" } });
|
|
116
|
-
|
|
471
|
+
|
|
472
|
+
// Only call onDragEnd if drop didn't already occur (for enableCrossContainerPreview)
|
|
473
|
+
// If drop occurred, handleDrop or global drop handler already called onDragEnd
|
|
474
|
+
if (enableCrossContainerPreview && dragStateRef.current.dropOccurred) {
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (onDragEnd) {
|
|
479
|
+
if (!enableCrossContainerPreview) {
|
|
480
|
+
onDragEnd();
|
|
481
|
+
} else {
|
|
482
|
+
const draggedItem = stateRef.current.items.find(item => item && item.id === draggedItemId);
|
|
483
|
+
const finalContainer = draggedItem ? draggedItem.container : originalContainer;
|
|
484
|
+
|
|
485
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === finalContainer);
|
|
486
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === draggedItemId);
|
|
487
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
488
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
489
|
+
|
|
490
|
+
onDragEnd(
|
|
491
|
+
draggedItemId,
|
|
492
|
+
finalContainer,
|
|
493
|
+
originalContainer,
|
|
494
|
+
itemAbove,
|
|
495
|
+
itemBelow
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
117
499
|
};
|
|
118
500
|
|
|
119
501
|
const changeCategory = (itemId: string, container: string) => {
|
|
@@ -123,10 +505,60 @@ export const DraggableProvider = ({
|
|
|
123
505
|
const handleDrop = (container: string) => {
|
|
124
506
|
if (state.dragData.originId !== providerId) return; // Ignore drop events from other providers
|
|
125
507
|
|
|
508
|
+
const draggedItemId = state.dragData.id;
|
|
509
|
+
const originalContainer = state.dragData.initialGroup;
|
|
510
|
+
|
|
511
|
+
// Mark drop as successful in ref for global listener
|
|
512
|
+
if (enableCrossContainerPreview) {
|
|
513
|
+
dragStateRef.current.dropOccurred = true;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Gather data for callbacks BEFORE clearing state
|
|
517
|
+
const isCrossContainer = container !== originalContainer;
|
|
518
|
+
let callbackData = null;
|
|
519
|
+
|
|
520
|
+
if (enableCrossContainerPreview) {
|
|
521
|
+
const draggedItem = stateRef.current.items.find(item => item && item.id === draggedItemId);
|
|
522
|
+
const updatedItem = draggedItem ? { ...draggedItem, container } : null;
|
|
523
|
+
const itemsInContainer = stateRef.current.items.filter(item => item && item.container === container);
|
|
524
|
+
const indexInContainer = itemsInContainer.findIndex(item => item && item.id === draggedItemId);
|
|
525
|
+
const itemAbove = indexInContainer > 0 ? itemsInContainer[indexInContainer - 1] : null;
|
|
526
|
+
const itemBelow = indexInContainer < itemsInContainer.length - 1 ? itemsInContainer[indexInContainer + 1] : null;
|
|
527
|
+
|
|
528
|
+
callbackData = { updatedItem, itemAbove, itemBelow };
|
|
529
|
+
}
|
|
530
|
+
|
|
126
531
|
dispatch({ type: 'SET_IS_DRAGGING', payload: "" });
|
|
127
532
|
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: "" });
|
|
533
|
+
dispatch({ type: 'SET_DRAG_DATA', payload: { id: "", initialGroup: "", originId: "" } });
|
|
128
534
|
changeCategory(state.dragData.id, container);
|
|
129
|
-
|
|
535
|
+
|
|
536
|
+
if (onDrop) {
|
|
537
|
+
if (!enableCrossContainerPreview) {
|
|
538
|
+
onDrop(container);
|
|
539
|
+
} else {
|
|
540
|
+
onDrop(
|
|
541
|
+
draggedItemId,
|
|
542
|
+
container,
|
|
543
|
+
originalContainer,
|
|
544
|
+
callbackData.updatedItem,
|
|
545
|
+
callbackData.itemAbove,
|
|
546
|
+
callbackData.itemBelow
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Trigger onDragEnd ONLY for cross-container drops (dragend doesn't fire reliably in that case)
|
|
552
|
+
// For same-container drops, handleDragEnd will be called normally
|
|
553
|
+
if (enableCrossContainerPreview && isCrossContainer && onDragEnd && callbackData) {
|
|
554
|
+
onDragEnd(
|
|
555
|
+
draggedItemId,
|
|
556
|
+
container,
|
|
557
|
+
originalContainer,
|
|
558
|
+
callbackData.itemAbove,
|
|
559
|
+
callbackData.itemBelow
|
|
560
|
+
);
|
|
561
|
+
}
|
|
130
562
|
};
|
|
131
563
|
|
|
132
564
|
const handleDragOver = (e: Event, container: string) => {
|
|
@@ -134,6 +566,22 @@ export const DraggableProvider = ({
|
|
|
134
566
|
|
|
135
567
|
e.preventDefault();
|
|
136
568
|
dispatch({ type: 'SET_ACTIVE_CONTAINER', payload: container });
|
|
569
|
+
|
|
570
|
+
if (enableCrossContainerPreview && state.dragData.id) {
|
|
571
|
+
// Only when enableCrossContainerPreview is true: when hovering over a different container, move item to end
|
|
572
|
+
const draggedItem = state.items.find(
|
|
573
|
+
(item) => item && item.id === state.dragData.id
|
|
574
|
+
);
|
|
575
|
+
if (draggedItem && draggedItem.container !== container) {
|
|
576
|
+
dispatch({
|
|
577
|
+
type: "MOVE_TO_CONTAINER_END",
|
|
578
|
+
payload: { dragId: state.dragData.id, newContainer: container },
|
|
579
|
+
});
|
|
580
|
+
// Update current container in ref
|
|
581
|
+
dragStateRef.current.currentContainer = container;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
137
585
|
if (onDragOver) onDragOver(e, container);
|
|
138
586
|
};
|
|
139
587
|
|
|
@@ -157,4 +605,4 @@ export const DraggableProvider = ({
|
|
|
157
605
|
return (
|
|
158
606
|
<DragContext.Provider value={contextValue}>{children}</DragContext.Provider>
|
|
159
607
|
);
|
|
160
|
-
};
|
|
608
|
+
};
|
|
@@ -18,8 +18,12 @@ export type ActionType =
|
|
|
18
18
|
} }
|
|
19
19
|
| { type: 'SET_IS_DRAGGING'; payload: string }
|
|
20
20
|
| { type: 'SET_ACTIVE_CONTAINER'; payload: string }
|
|
21
|
+
| { type: 'SET_CROSS_CONTAINER_PREVIEW'; payload: boolean }
|
|
21
22
|
| { type: 'CHANGE_CATEGORY'; payload: { itemId: string; container: string } }
|
|
22
|
-
| { type: 'REORDER_ITEMS'; payload: { dragId: string; targetId: string } }
|
|
23
|
+
| { type: 'REORDER_ITEMS'; payload: { dragId: string; targetId: string } }
|
|
24
|
+
| { type: 'REORDER_ITEMS_CROSS_CONTAINER'; payload: { dragId: string; targetId: string; newContainer: string } }
|
|
25
|
+
| { type: 'MOVE_TO_CONTAINER_END'; payload: { dragId: string; newContainer: string } }
|
|
26
|
+
| { type: 'RESET_DRAG_CONTAINER'; payload: { itemId: string; originalContainer: string } };
|
|
23
27
|
|
|
24
28
|
export interface DropZoneConfig {
|
|
25
29
|
type?: 'ghost' | 'outline' | 'shadow' | 'line';
|
|
@@ -33,9 +37,10 @@ export type ActionType =
|
|
|
33
37
|
onReorder: (items: ItemType[]) => void;
|
|
34
38
|
onDragStart?: (id: string, container: string) => void;
|
|
35
39
|
onDragEnter?: (id: string, container: string) => void;
|
|
36
|
-
onDragEnd?: () => void;
|
|
37
|
-
onDrop?: (
|
|
40
|
+
onDragEnd?: (...args: any[]) => void;
|
|
41
|
+
onDrop?: (...args: any[]) => void;
|
|
38
42
|
onDragOver?: (e: Event, container: string) => void;
|
|
39
43
|
dropZone?: DropZoneConfig | string; // Can accept string for backward compatibility
|
|
40
44
|
providerId?: string;
|
|
45
|
+
enableCrossContainerPreview?: boolean;
|
|
41
46
|
}
|