Skip to content

Commit 159175b

Browse files
feat(useSortable): Add possibility to use it with Component ref (#4684)
1 parent a0a7c75 commit 159175b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

packages/integrations/useSortable/index.browser.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mount } from '@vue/test-utils'
2-
import { templateRef } from '@vueuse/core'
2+
import { templateRef, unrefElement } from '@vueuse/core'
33
import Sortable from 'sortablejs'
44
import { describe, expect, it } from 'vitest'
55
import { defineComponent, shallowRef } from 'vue'
@@ -152,4 +152,31 @@ describe('useSortable', () => {
152152
}
153153
})
154154
})
155+
156+
it('accepts component refs', () => {
157+
const SubComponent = defineComponent({
158+
template: '<p>foo</p>',
159+
})
160+
const wrapper = mount(defineComponent({
161+
components: { SubComponent },
162+
template: '<SubComponent ref="el"></SubComponent>',
163+
setup() {
164+
const el = templateRef<InstanceType<typeof SubComponent>>('el')
165+
const list = shallowRef<string[]>([])
166+
const result = useSortable(el, list, {
167+
})
168+
169+
return { ...result, el }
170+
},
171+
}))
172+
const vm = wrapper.vm
173+
try {
174+
const el = unrefElement(vm.el) as HTMLElement
175+
const sortable = Sortable.get(el)
176+
expect(sortable).toBeDefined()
177+
}
178+
finally {
179+
wrapper.unmount()
180+
}
181+
})
155182
})

packages/integrations/useSortable/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ConfigurableDocument } from '@vueuse/core'
1+
import type { ConfigurableDocument, MaybeElement } from '@vueuse/core'
22
import type { Options } from 'sortablejs'
33
import type { MaybeRef, MaybeRefOrGetter } from 'vue'
44
import { defaultDocument, tryOnMounted, tryOnScopeDispose, unrefElement } from '@vueuse/core'
@@ -27,7 +27,7 @@ export type UseSortableOptions = Options & ConfigurableDocument
2727

2828
export function useSortable<T>(selector: string, list: MaybeRefOrGetter<T[]>,
2929
options?: UseSortableOptions): UseSortableReturn
30-
export function useSortable<T>(el: MaybeRefOrGetter<HTMLElement | null | undefined>, list: MaybeRefOrGetter<T[]>,
30+
export function useSortable<T>(el: MaybeRefOrGetter<MaybeElement>, list: MaybeRefOrGetter<T[]>,
3131
options?: UseSortableOptions): UseSortableReturn
3232

3333
/**
@@ -37,7 +37,7 @@ export function useSortable<T>(el: MaybeRefOrGetter<HTMLElement | null | undefin
3737
* @param options
3838
*/
3939
export function useSortable<T>(
40-
el: MaybeRefOrGetter<HTMLElement | null | undefined> | string,
40+
el: MaybeRefOrGetter<MaybeElement> | string,
4141
list: MaybeRefOrGetter<T[]>,
4242
options: UseSortableOptions = {},
4343
): UseSortableReturn {

0 commit comments

Comments
 (0)