简体   繁体   English

如何防止 React 中的冲突事件(我认为)?

[英]How to prevent conflicting events (I think) in React?

I'm using react-beautiful-dnd :我正在使用react-beautiful-dnd

<DragDropContext>
  <Droppable>
    {(provided, snapshot) => (
      <div>
       <Draggable><Item with Slider></Draggable>
       <Draggable><Item with Slider></Draggable>
       <Draggable><Item with Slider></Draggable>
       <Draggable><Item with Slider></Draggable>
       etc
      </div>
    )}
  </Droppable>
</DragDropContext>  

My understanding is that Draggable will have some variety of event listeners to allow it to detect when it is clicked/clicked and dragged/etc.我的理解是Draggable将具有多种事件侦听器,以允许它检测何时单击/单击和拖动/等。

The Slider component that I'm using will also have similar events (since I assume it provides similar event listeners to Draggable ).我正在使用的Slider组件也将具有类似的事件(因为我假设它提供了与Draggable类似的事件侦听器)。

How can I either:我怎样才能:

  1. Get react-beautiful-dnd to ignore events emitted from certain targets获取react-beautiful-dnd以忽略从某些目标发出的事件

or或者

  1. Stop event propagation so that react-beautiful-dnd doesn't receive an event?停止事件传播,以便react-beautiful-dnd不接收事件?

The screenshot below shows two sliders - (1) a React component ( material-ui ) and (2) an <input type="range" /> component.下面的屏幕截图显示了两个滑块 - (1) 一个 React 组件 ( material-ui ) 和 (2) 一个<input type="range" />组件。 The normal range works fine (doesn't trigger drag and drop) while the Slider does not.正常范围工作正常(不会触发拖放),而滑块则不然。

See this: code sandbox请参阅: 代码沙箱

The key to accomplish this is to only add the spread {...provided.dragHandleProps} to the inner div and make sure that {...provided.dragHandleProps} is NOT on container parent which is usually the div which is the direct child of the <Draggable>完成此操作的关键是将扩展{...provided.dragHandleProps}添加到内部 div 并确保{...provided.dragHandleProps}不在容器父级上,这通常是直接子级的 div <Draggable>

<Droppable droppableId="featureWidgets">
    {(provided: any) => (
        <div {...provided.droppableProps} ref={provided.innerRef}>
            <FeaturesListView>
            {featureWidgets.map(({id, type}, index) => {
                return (<Draggable key={id} draggableId={id} index={index}>
                            {(provided) => (
                                <div
                                    ref={provided.innerRef} {...provided.draggableProps} ///* NO NO not here  {...provided.dragHandleProps} */
                                    <div {...provided.dragHandleProps}>drag grip handle</div>
                                    <WidgetRow>
                                        <Slider/>
                                    </WidgetRow>
                                </div>
                            )}
                        </Draggable>);
                })}
            {provided.placeholder}
            </FeaturesListView>
        </div>
    )}
</Droppable> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM