Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(useSortable): fixed moveArrayElement repeatedly triggering side effects #3322

Merged
merged 3 commits into from Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
})
}
}