react-smooth-dnd 拖拽实例

 

import React, { useState } from 'react'
import { Container, Draggable } from 'react-smooth-dnd'
import { v4 as uuidv4 } from 'uuid'

export default function CardPlus() {
  const [toolList, setToolList] = useState([
    {
      id: 't0',
      name: '1',
    },
    {
      id: 't1',
      name: '2',
    },
    {
      id: 't2',
      name: '3',
    },
  ])

  const [contentList, setContentList] = useState([
    {
      id: 'c0',
      name: 'a',
    },
    {
      id: 'c1',
      name: 'b',
    },
    {
      id: 'c2',
      name: 'c',
    },
  ])

  const applyDrag = (arr, dragResult) => {
    const { removedIndex, addedIndex, payload } = dragResult
    if (removedIndex === null && addedIndex === null) return arr

    const result = [...arr]
    let itemToAdd = payload

    if (removedIndex !== null) {
      itemToAdd = result.splice(removedIndex, 1)[0]
    }

    if (addedIndex !== null) {
      result.splice(addedIndex, 0, itemToAdd)
    }

    return result
  }

  const handleGetChildPayload = ({type, index}) => {
    if (type === 'tool') {
      const id = uuidv4()
      return {...toolList[index], id}
    } else if (type === 'content') {
      return contentList[index]
    }
  }

  const handleCardDrop = ({ type, dragResult }) => {
    console.log(dragResult)
    if (type === 'tool') {
      const result = applyDrag(toolList, dragResult)
      setToolList(result)
    } else if (type === 'content') {
      const result = applyDrag(contentList, dragResult)
      setContentList(result)
    }
  }

  return (
    <div className="m-test-wrap">
      <div className="m-test-tool">
        <Container
          orientation="vertical"
          onDrop={(dragResult) => handleCardDrop({ type: 'tool', dragResult })}
          getChildPayload={(index) => handleGetChildPayload({type: 'tool', index})}
          behaviour="copy"
          groupName="col"
        >
          {toolList.map((item) => (
            <Draggable key={item.id} className="m-test-list-item">
              <div>{item.name}</div>
            </Draggable>
          ))}
        </Container>
      </div>
      <div className="m-test-list">
        <Container
          orientation="vertical"
          onDrop={(dragResult) =>
            handleCardDrop({ type: 'content', dragResult })
          }
          getChildPayload={(index) => handleGetChildPayload({type: 'content', index})}
          groupName="col"
        >
          {contentList.map((item) => (
            <Draggable key={item.id} className="m-test-list-item">
              <div>{item.name}</div>
            </Draggable>
          ))}
        </Container>
      </div>
    </div>
  )
}

参考链接:

https://kutlugsahin.github.io/smooth-dnd-demo/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐同保

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值