Skip to content

Commit 76c0610

Browse files
fix: update:model-value not triggering on add and remove (#138)
1 parent c3e3f96 commit 76c0610

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/useDraggable.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
142142
const element = evt.item[CLONE_ELEMENT_KEY]
143143
if (isUndefined(element)) return
144144
removeNode(evt.item)
145+
if (isRef<any[]>(list)) {
146+
const newList = [...unref(list)]
147+
list.value = insertElement(newList, evt.newDraggableIndex!, element)
148+
return
149+
}
145150
insertElement(unref(list), evt.newDraggableIndex!, element)
146151
}
147152

@@ -151,11 +156,16 @@ export function useDraggable<T>(...args: any[]): UseDraggableReturn {
151156
*/
152157
function onRemove(evt: DraggableEvent) {
153158
const { from, item, oldIndex, oldDraggableIndex, pullMode, clone } = evt
159+
insertNodeAt(from, item, oldIndex!)
154160
if (pullMode === 'clone') {
155-
insertNodeAt(from, item, oldIndex!)
156161
removeNode(clone)
157162
return
158163
}
164+
if (isRef<any[]>(list)) {
165+
const newList = [...unref(list)]
166+
list.value = removeElement(newList, oldDraggableIndex!)
167+
return
168+
}
159169
removeElement(unref(list), oldDraggableIndex!)
160170
}
161171

src/utils/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { warn } from './log'
2+
13
/**
24
* Moves an element in an array from one position to another.
35
* @param {T[]} array
46
* @param {number} from
57
* @param {number} to
8+
* @returns {T[]}
69
*/
7-
import { warn } from './log'
8-
910
export function moveArrayElement<T>(array: T[], from: number, to: number): T[] {
1011
if (to >= 0 && to < array.length) {
1112
array.splice(to, 0, array.splice(from, 1)[0])
@@ -43,17 +44,20 @@ export function objectMap(object: Record<any, any>) {
4344
* @returns {T[]}
4445
*/
4546
export function removeElement<T>(array: T[], index: number) {
46-
if (Array.isArray(array)) return array.splice(index, 1)
47+
if (Array.isArray(array)) array.splice(index, 1)
48+
return array
4749
}
4850

4951
/**
5052
* Inserts an element into an array.
5153
* @param {T[]} array
5254
* @param {number} index
5355
* @param element
56+
* @returns {T[]}
5457
*/
5558
export function insertElement<T>(array: T[], index: number, element: any) {
56-
if (Array.isArray(array)) return array.splice(index, 0, element)
59+
if (Array.isArray(array)) array.splice(index, 0, element)
60+
return array
5761
}
5862

5963
/**

0 commit comments

Comments
 (0)