インライン並び替え(↑↓ボタン)
:LiTarget: 用途
配列要素を上下ボタンで並び替え。D&Dより安心・ミス少ない。
:LiSparkle: 特徴
- ↑↓ ボタン
- 一発実装
- 配列インデックス操作
- モバイル安心
:LiCode: コード(コピペ用)
function reorder(arr, fromIdx, direction) {
const toIdx = direction === 'up' ? fromIdx - 1 : fromIdx + 1;
if (toIdx < 0 || toIdx >= arr.length) return arr;
const next = [...arr];
[next[fromIdx], next[toIdx]] = [next[toIdx], next[fromIdx]];
return next;
}
// 使い方:
// const items = PD('items', []);
// const newOrder = reorder(items, 3, 'up'); // index 3 を上へ
// setPD('items', newOrder);
// HTML:
// <button onclick="moveUp(idx)">↑</button>
// <button onclick="moveDown(idx)">↓</button>
:LiHandPointer: 使い方
対象プロジェクトに該当ファイルをコピーして、props を流し込むだけ。
:LiAlertCircle: 注意事項
- 依存パッケージを忘れず追加