import arrayMove from 'array-move'
/**
* @item 当前移动的节点
* @direction: up | down 移动的方向
*/
import arrayMove = from 'arrayMove'
function moveDone (item, direction = 'up') {
const arr = [...data]
const lastPosition = arr.length - 1 // 最大的位置
const index = arr.indexOf(item) // 查找当前位置
let position = direction === 'down' ? index + 1 : index - 1 // 记录移动的位置
// 如果没这条数据
if (index === -1) return false
// 如果上移 第一条数据
if (direction === 'up' && index === 0) return false
// 如果下移 最后一条数据
if (direction === 'down' && lastPosition === index) return false
// 移动位置 并且赋值给 data
setData(arrayMove(arr, index, position))
}
上面的是不需要第一个去最后一个那种的
这个是普通的:
上移:
const top = (text) => {
let arr = [...data]
let index = arr.indexOf(text)
if(index === 0) return false
let nowindex = index <= 0 ? arr.length - 1:arr.indexOf(text) - 1
arr.splice(index, 1)
arr.splice(nowindex, 0 , text)
setData(arr)
}
下移:
const bom = (text) => {
let arr = [...data]
let index = arr.indexOf(text)
if(index === arr.length - 1) return false
let nowindex = index >= arr.length - 1 ? 0 :arr.indexOf(text) + 1
arr.splice(index, 1)
arr.splice(nowindex, 0 , text)
setData(arr)
}