react拖拽组件dnd-kit拖拽事件与点击事件冲突问题解决

记录在拖拽功能中拖拽组件与点击事件冲突,导致点击事件无法触发,点击只会触发拖拽事件。

需求:目前需要在拖拽组件上添加一个功能按钮,点击功能按钮进行编辑删除等操作,但是点击功能按钮无法触发点击事件。

事件捕获用过都不行、寻找了很多办法无果,dnd-kit官方文档给出了Sensors传感器的概念,功能大概意思就是比如鼠标按下拖拽组件需要移动多少个像素才触发拖拽事件、或者是等待多少毫秒触发拖拽事件。

例如我写的传感器如下,鼠标按下需要移动5个像素才激活拖拽事件, 这样当地点击功能按钮时,鼠标没有移动,自然就触发了点击事件。

//拖拽传感器,在移动像素5px范围内,不触发拖拽事件
    const sensors = useSensors(
        useSensor(MouseSensor,{
            activationConstraint: {
                distance: 5, // 按住不动移动5px 时 才进行拖拽, 这样就可以触发点击事件
            },
        })
    )

传感器如何应用:应用起来也非常简单,在DndContext标签上加个sensors属性就应用好了

import {DndContext,useSensors,useSensor,MouseSensor} from "@dnd-kit/core";
import {Droppable} from "./Droppable";
import {Draggable} from "./Draggable";


function App() {
  return (
    <DndContext sensors={sensors}>
      <Draggable /> //拖拽组件
      <Droppable /> //可落下的容器组件
    </DndContext>
  )
}

dnd-kit拖拽组件的使用方法在我上一篇文章中https://blog.csdn.net/A15029296293/article/details/134141264

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React-dnd 拖拽排序是一种常见的前端交互方式,可以通过以下代码实现:1. 首先需要安装 react-dndreact-dnd-html5-backend 两个库:``` npm install --save react-dnd react-dnd-html5-backend ```2. 在组件中引入 DragDropContext、Droppable 和 Draggable 组件:``` import { DragDropContext, Droppable, Draggable } from 'react-dnd'; import { HTML5Backend } from 'react-dnd-html5-backend'; ```3. 定义一个数组作为拖拽列表的数据源:``` const items = [ { id: 1, name: 'Item 1' }, { id: 2, name: 'Item 2' }, { id: 3, name: 'Item 3' }, { id: 4, name: 'Item 4' }, { id: 5, name: 'Item 5' }, ]; ```4. 在组件中使用 DragDropContext 组件包裹整个列表,并在其中使用 Droppable 组件包裹每个拖拽项:``` <DragDropContext backend={HTML5Backend}> <Droppable droppableId="items"> {(provided) => ( <ul {...provided.droppableProps} ref={provided.innerRef}> {items.map((item, index) => ( <Draggable key={item.id} draggableId={item.id.toString()} index={index}> {(provided) => ( <li {...provided.draggableProps} {...provided.dragHandleProps} ref={provided.innerRef} > {item.name} </li> )} </Draggable> ))} {provided.placeholder} </ul> )} </Droppable> </DragDropContext> ```5. 在 Draggable 组件中使用 provided.draggableProps 和 provided.dragHandleProps 属性来实现拖拽功能,同时使用 provided.innerRef 属性来获取拖拽元素的引用。6. 在 Droppable 组件中使用 provided.droppableProps 和 provided.innerRef 属性来实现拖拽排序功能。7. 最后,需要在 DragDropContext 组件中定义 onDragEnd 回调函数来处理拖拽结束后的逻辑:``` function onDragEnd(result) { if (!result.destination) { return; } const newItems = Array.from(items); const [reorderedItem] = newItems.splice(result.source.index, 1); newItems.splice(result.destination.index, , reorderedItem); setItems(newItems); }<DragDropContext backend={HTML5Backend} onDragEnd={onDragEnd}> ... </DragDropContext> ```8. 在 onDragEnd 回调函数中,首先判断是否有目标位置,如果没有则直接返回。然后使用 Array.from 方法复制一份原始数据源,从中取出被拖拽的元素并删除,再将其插入到目标位置中,最后使用 setItems 函数更新数据源。以上就是 react-dnd 拖拽排序的代码实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值