简体   繁体   English

在 react-grid-layout 中捕获网格项点击事件

[英]Capturing grid item click event in react-grid-layout

Is it possible to capture the grid item's click and cancel the drag event?是否可以捕获网格项的点击并取消拖动事件? I want to open a modal window when a grid item is clicked, but I can't figure out how to implement this.单击网格项时,我想打开模式 window,但我不知道如何实现它。 I'm capturing the click with onClick , but stopPropagation and preventDefault don't prevent the mousedown event that starts the dragging process.我正在使用onClick捕获点击,但stopPropagationpreventDefault不会阻止启动拖动过程的 mousedown 事件。

This can be done by passing a onMouseDown to a child div element. 这可以通过将onMouseDown传递给子div元素来完成。

<RGL ... >
  <div> // If you add onMouseDown here react-draggable will override it. You will have to use nested element to capture clicks
    <div onMouseDown={ e => e.stopPropagation() }>
      ...
    </div>
  <div>
</RGL>

I managed to get a workable solution by using movementx and movementy to see if there was actual real movement. 我通过使用movementxmovementy来查看是否存在实际的实际运动,从而获得了可行的解决方案。

private onDrag = (
_layout: Layout[],
oldItem: Layout,
_newItem: Layout,
_placeholder: Layout,
e: MouseEvent,
_element: HTMLElement) => {
if (e.movementX !== 0 || e.movementY !== 0) {
  this.props.onWidgetDragStart(oldItem.i);
}  };

onWidgetDragStart fires an event that sets a variable (redux reducer:) onWidgetDragStart会触发一个设置变量的事件(redux reducer :)

{
\[actionConstants.DASHBOARD.WIDGET_DRAGGING_STARTED\]: (
    state: DraftObject<DashboardsState>,
    action: Action<string>,
  ) => {
   state.isWidgetDragging = true;
   state.indexOfCurrentDraggedItem = action.payload;
 }
}

the click event then looks like this : click事件如下所示:

private onTitleClicked = (e: React.MouseEvent<HTMLDivElement>) => {
if (this.props.indexOfCurrentDraggedItem === this.props.options.id) {
  e.preventDefault();
  this.props.updatingIndexOfCurrentDraggedItem();
  return;
}

if (!this.props.isWidgetDragging && !this.state.editMode) {
  this.setState({
    editMode: true,
  });
}
{
\[actionConstants.DASHBOARD.WIDGET_DRAGGING_STARTED\]: (
state: DraftObject<DashboardsState>,
action: Action<string>,
) => {
  state.isWidgetDragging = true;
  state.indexOfCurrentDraggedItem = action.payload;
  },
};

where the updatingIndexOfCurrentDraggedItem action sets the indexOfCurrentDraggedItem to null and indexOfCurrentDraggedItem is injected as a prop to my widget component. 其中updatingIndexOfCurrentDraggedItem动作设置indexOfCurrentDraggedItemnullindexOfCurrentDraggedItem注入为道具,以我的小工具组件。

I was on a mobile browser, and so the react-grid-layout was eating the onClick event.我在移动浏览器上,所以 react-grid-layout 正在处理onClick事件。 Instead, I also listened for the onTouchStart listener, which worked.相反,我还监听了有效的onTouchStart侦听器。

<IconButton onTouchStart={() => onEditItem(id)} onClick={() => onEditItem(id)}>

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

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