@@ -14,11 +14,13 @@ import type { Fn, RefOrElement, RefOrValue } from './types'
14
14
import { error } from './utils/log'
15
15
16
16
import {
17
+ extend ,
17
18
forEachObject ,
18
19
getElementBySelector ,
19
20
insertElement ,
20
21
insertNodeAt ,
21
22
isHTMLElement ,
23
+ isOn ,
22
24
isString ,
23
25
isUndefined ,
24
26
mergeOptionsEvents ,
@@ -51,6 +53,24 @@ function tryOnMounted(fn: Fn) {
51
53
else nextTick ( fn )
52
54
}
53
55
56
+ let data : any = null
57
+ let clonedData : any = null
58
+
59
+ function setCurrentData (
60
+ _data : typeof data = null ,
61
+ _clonedData : typeof data = null
62
+ ) {
63
+ data = _data
64
+ clonedData = _clonedData
65
+ }
66
+
67
+ function getCurrentData ( ) {
68
+ return {
69
+ data,
70
+ clonedData
71
+ }
72
+ }
73
+
54
74
const CLONE_ELEMENT_KEY = Symbol ( 'cloneElement' )
55
75
56
76
interface DraggableEvent < T = any > extends SortableEvent {
@@ -135,8 +155,7 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
135
155
function onStart ( evt : DraggableEvent ) {
136
156
const data = unref ( unref ( list ) ?. [ evt . oldIndex ! ] )
137
157
const clonedData = clone ( data )
138
- evt . data = data
139
- evt . clonedData = clonedData
158
+ setCurrentData ( data , clonedData )
140
159
evt . item [ CLONE_ELEMENT_KEY ] = clonedData
141
160
}
142
161
@@ -195,14 +214,21 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
195
214
moveArrayElement ( unref ( list ) , oldIndex ! , newIndex ! )
196
215
}
197
216
217
+ function onEnd ( ) {
218
+ nextTick ( ( ) => {
219
+ setCurrentData ( )
220
+ } )
221
+ }
222
+
198
223
/**
199
224
* preset options
200
225
*/
201
226
const presetOptions : UseDraggableOptions < T > = {
202
227
onUpdate,
203
228
onStart,
204
229
onAdd,
205
- onRemove
230
+ onRemove,
231
+ onEnd
206
232
}
207
233
208
234
function getTarget ( target ?: HTMLElement ) {
@@ -222,6 +248,16 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
222
248
function mergeOptions ( ) {
223
249
// eslint-disable-next-line
224
250
const { immediate, clone, ...restOptions } = unref ( options ) ?? { }
251
+
252
+ forEachObject ( restOptions , ( key , fn ) => {
253
+ if ( ! isOn ( key ) ) return
254
+ restOptions [ key ] = ( evt : DraggableEvent , ...args : any [ ] ) => {
255
+ const data = getCurrentData ( )
256
+ extend ( evt , data )
257
+ fn ( evt , ...args )
258
+ }
259
+ } )
260
+
225
261
return mergeOptionsEvents (
226
262
list === null ? { } : presetOptions ,
227
263
restOptions
0 commit comments