React-DnD中是否有任何选项,它可以根据拖放对象启用放置目标,该拖动对象与放置目标中超过50%的区域相交?

时间:2017-07-06 07:33:16

标签: reactjs jquery-ui drag-and-drop react-dnd

我一直在研究react-dnd(拖放组件)。因此,基于鼠标指针识别远滴目标,我想知道是否有任何更改它的选项,基于拖动对象需要识别的放置目标与放置目标的50%相交。

类似于jQuery UI拖放功能,该功能在可放置元素中包含'tolerance:intersect'

1 个答案:

答案 0 :(得分:3)

查看React-DnD的sortable example,特别是cardTarget中的悬停功能:

const cardTarget = {
  hover(props, monitor, component) {
    const dragIndex = monitor.getItem().index;
    const hoverIndex = props.index;

    // Don't replace items with themselves
    if (dragIndex === hoverIndex) {
      return;
    }

    // Determine rectangle on screen
    const hoverBoundingRect = findDOMNode(component).getBoundingClientRect();

    // Get vertical middle
    const hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;

    // Determine mouse position
    const clientOffset = monitor.getClientOffset();

    // Get pixels to the top
    const hoverClientY = clientOffset.y - hoverBoundingRect.top;

    // Only perform the move when the mouse has crossed half of the items height
    // When dragging downwards, only move when the cursor is below 50%
    // When dragging upwards, only move when the cursor is above 50%

    // Dragging downwards
    if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
      return;
    }

    // Dragging upwards
    if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
      return;
    }

    // Time to actually perform the action
    props.moveCard(dragIndex, hoverIndex);

    // Note: we're mutating the monitor item here!
    // Generally it's better to avoid mutations,
    // but it's good here for the sake of performance
    // to avoid expensive index searches.
    monitor.getItem().index = hoverIndex;
  }
};

我认为这两行是您正在寻找的:

// Dragging downwards
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
  return;
}

// Dragging upwards
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
  return;
}

它检查悬停时,如果您悬停的项目已超过移动项目的50%阈值,则它将执行重新排序操作。