Skip to content

Commit

Permalink
fix(useSortable): fixed moveArrayElement repeatedly triggering side e…
Browse files Browse the repository at this point in the history
…ffects (#3322)
  • Loading branch information
Alfred-Skyblue committed Aug 25, 2023
1 parent 192bb54 commit b85154f
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/integrations/useSortable/index.ts
@@ -1,7 +1,7 @@
import { defaultDocument, toValue, tryOnMounted, tryOnScopeDispose, unrefElement } from '@vueuse/core'
import type { ConfigurableDocument, MaybeRefOrGetter } from '@vueuse/core'
import Sortable, { type Options } from 'sortablejs'
import { nextTick } from 'vue-demi'
import { isRef, nextTick } from 'vue-demi'

export interface UseSortableReturn {
/**
Expand Down Expand Up @@ -77,9 +77,17 @@ export function moveArrayElement<T>(
from: number,
to: number,
): void {
const array = toValue(list)
const _valueIsRef = isRef(list)
// When the list is a ref, make a shallow copy of it to avoid repeatedly triggering side effects when moving elements
const array = _valueIsRef ? [...toValue(list)] : toValue(list)

if (to >= 0 && to < array.length) {
const element = array.splice(from, 1)[0]
nextTick(() => array.splice(to, 0, element))
nextTick(() => {
array.splice(to, 0, element)
// When list is ref, assign array to list.value
if (_valueIsRef)
list.value = array
})
}
}

0 comments on commit b85154f

Please sign in to comment.