コンテンツへスキップ

useSortable

カテゴリ
エクスポートサイズ
720 B
パッケージ
@vueuse/integrations
最終更新日
先週

sortable のラッパーです。

渡すことができるオプションの詳細については、SortableドキュメントのSortable.optionsを参照してください。

デモ

a
b
c
[{"id":1,"name":"a"},{"id":2,"name":"b"},{"id":3,"name":"c"}]
@vueuse/integrations アドオンで使用できます。

インストール

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',
  // or option set
  // animation
})

// You can use the option method to set and get the option of 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) => {
    // do something
    moveArrayElement(list.value, e.oldIndex, e.newIndex, e)
    // nextTick required here as moveArrayElement is executed in a microtask
    // so we need to wait until the next tick until that is finished.
    nextTick(() => {
      /* do something */
    })
  }
})

型宣言

型宣言を表示
typescript
export interface UseSortableReturn {
  /**
   * start sortable instance
   */
  start: () => void
  /**
   * destroy sortable instance
   */
  stop: () => void
  /**
   * Options getter/setter
   * @param name a Sortable.Options property.
   * @param value a 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
/**
 * Inserts a element into the DOM at a given index.
 * @param parentElement
 * @param element
 * @param {number} index
 * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L81C1-L94C2
 */
export declare function insertNodeAt(
  parentElement: Element,
  element: Element,
  index: number,
): void
/**
 * Removes a node from the DOM.
 * @param {Node} node
 * @see https://github.com/Alfred-Skyblue/vue-draggable-plus/blob/a3829222095e1949bf2c9a20979d7b5930e66f14/src/utils/index.ts#L96C1-L102C2
 */
export declare function removeNode(node: Node): void
export declare function moveArrayElement<T>(
  list: MaybeRefOrGetter<T[]>,
  from: number,
  to: number,
  e?: Sortable.SortableEvent | null,
): void

ソース

ソースデモドキュメント

コントリビューター

Anthony Fu
丶远方
Doctorwu
IlyaL
David Gonzalez
Anthony Fu
Tycho
George Kinsman

変更ログ

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v11.3.0 2024/11/21
9e78e - fix: 要素の順序が間違っている (#4332)
v10.8.0 2024/2/20
a086e - fix: より厳格な型
v10.6.0 2023/11/9
d9846 - fix: 複数インスタンスの作成を防ぐ (#3501)
v10.4.0 2023/8/25
b8515 - 修正: moveArrayElement の繰り返し実行による副作用を修正 (#3322)
v10.2.0 2023年6月16日
14283 - 機能追加: オプション設定取得メソッドを追加 (#3108)
v10.0.0-beta.4 2023年4月13日
4d757 - 機能追加(型): MaybeComputedRefMaybeRefOrGetter に名前変更
0a72b - 機能追加(toValue): resolveUnreftoValue に名前変更
v10.0.0-beta.3 2023年4月12日
3a508 - 修正: DOMと配列の順序が異なる問題を修正 (#2926)
v10.0.0-beta.0 2023年3月14日
6bc60 - 機能追加: 新しい関数を追加 (#2763)

MITライセンスの下でリリースされています。