Skip to content

useSortable

类别
导出体积
720 B
依赖包
@vueuse/integrations
上次更改
3 minutes ago

sortable 的封装。

要了解可以传递的选项的更多信息,请参阅 Sortable 文档中的 Sortable.options

示例

a
b
c
[{"id":1,"name":"a"},{"id":2,"name":"b"},{"id":3,"name":"c"}]
Available in the @vueuse/integrations add-on.

安装

bash
npm i sortablejs@^1

使用方法

使用模板引用

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const el = ref<HTMLElement | null>(null)
const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

useSortable(el, list)
</script>

<template>
  <div ref="el">
    <div v-for="item in list" :key="item.id">
      {{ item.name }}
    </div>
  </div>
</template>

使用指定的选择器操作

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const el = ref<HTMLElement | null>(null)
const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

const animation = 200

const { option } = useSortable(el, list, {
  handle: '.handle',
  // 或者设置 option
  // animation
})

// 你可以使用 option 方法设置和获取 Sortable 的选项
option('animation', animation)
// option('animation') // 200
</script>

<template>
  <div ref="el">
    <div v-for="item in list" :key="item.id">
      <span>{{ item.name }}</span>
      <span class="handle">*</span>
    </div>
  </div>
</template>

使用选择器获取根元素

vue
<script setup lang="ts">
import { useSortable } from '@vueuse/integrations/useSortable'
import { ref } from 'vue'

const list = ref([{ id: 1, name: 'a' }, { id: 2, name: 'b' }, { id: 3, name: 'c' }])

useSortable('#dv', list)
</script>

<template>
  <div id="dv">
    <div v-for="item in list" :key="item.id">
      <span>{{ item.name }}</span>
    </div>
  </div>
</template>

提示

如果你想要自己处理 onUpdate,可以传递 onUpdate 参数,我们还提供了一个移动项目位置的函数。

ts
import { moveArrayElement } from '@vueuse/integrations/useSortable'

useSortable(el, list, {
  onUpdate: (e) => {
    // 处理更新
    moveArrayElement(list.value, e.oldIndex, e.newIndex)
    // 这里需要 nextTick,因为 moveArrayElement 在微任务中执行
    // 所以我们需要等到下一个刻度直到它完成。
    nextTick(() => {
      /* do something */
    })
  }
})

类型声明

typescript
export interface UseSortableReturn {
  /**
   * 启动可排序实例
   */
  start: () => void
  /**
   * 销毁可排序实例
   */
  stop: () => void
  /**
   * 选项的获取器/设置器
   * @param name Sortable.Options 的属性名称。
   * @param value 一个值。
   */
  option: (<K extends keyof Sortable.Options>(
    name: K,
    value: Sortable.Options[K],
  ) => void) &
    (<K extends keyof Sortable.Options>(name: K) => Sortable.Options[K])
}
export type UseSortableOptions = Options & ConfigurableDocument
export declare function useSortable<T>(
  selector: string,
  list: MaybeRefOrGetter<T[]>,
  options?: UseSortableOptions,
): UseSortableReturn
export declare function useSortable<T>(
  el: MaybeRefOrGetter<HTMLElement | null | undefined>,
  list: MaybeRefOrGetter<T[]>,
  options?: UseSortableOptions,
): UseSortableReturn
export declare function moveArrayElement<T>(
  list: MaybeRefOrGetter<T[]>,
  from: number,
  to: number,
): void

源码

源码演示文档

贡献者

Anthony Fu
丶远方
一纸忘忧
Doctorwu
Anthony Fu
Tycho
George Kinsman

更新日志

v10.8.0 on 2/20/2024
a086e - fix: stricter types
v10.6.0 on 11/9/2023
d9846 - fix: prevent from creating multi instances (#3501)
v10.4.0 on 8/25/2023
b8515 - fix: fixed moveArrayElement repeatedly triggering side effects (#3322)
v10.2.0 on 6/16/2023
14283 - feat: add option set get method (#3108)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.3 on 4/12/2023
3a508 - fix: order of dom and array is different (#2926)
v10.0.0-beta.0 on 3/14/2023
6bc60 - feat: new function (#2763)