无法在同一个可放置的react-beautiful-dnd中拖放组件

kn0*_*n0t 1 javascript reactjs react-beautiful-dnd

发生的情况是,当我在一列中有多个项目并尝试拖动其中一个时,只显示一个项目,根据此处的经验教训,我应该处于可以移动同一列中的项目但不能移动项目的位置。在 React 开发工具中,状态和 rb-dnd id 看起来不错,但我知道什么?我只是一个菜鸟。onDragEnd这是我到目前为止所拥有的。

  onDragEnd = result => {
    const { destination, source, draggableId } = result;
    if (!destination) return;
    if (
      destination.droppableId === source.droppableId &&
      destination.index === source.index
    ) {
      return;
    }
    let start = this.state.list[source.droppableId];
    let finish = this.state.list[destination.droppableId];
    if (start === finish) {
      let updatedList = this.state.list.map(obj => {
        if (obj.id === source.droppableId) {
          obj.cards.splice(source.index, 1);
          obj.cards.splice(destination.index, 0, draggableId);
        }
        return obj;
      });
      this.setState({ list: updatedList });
    }
Run Code Online (Sandbox Code Playgroud)

我的应用程序可以在这里找到。谢谢。

Ava*_*ika 5

在您的代码中,onDragEnd您有此代码,这就是您无法重新排列同一列表上的项目的原因。当您在同一列表中移动项目时,源和目标可放置 ID 将相同。

if (destination.droppableId === source.droppableId && destination.index === source.index) {
  console.log("they're equal");
  return;
}
Run Code Online (Sandbox Code Playgroud)
  1. 在您的组件上,列表中所有项目的可拖动 ID 都是相同的。对于每个可拖动项目来说它应该是唯一的。
const Card = props => {
  return (
    // This is NOT unique. This should be dynamic, we should use idx probably.
    <Draggable draggableId={props.col + "-" + props.color} index={props.idx}>
      {provided => (
Run Code Online (Sandbox Code Playgroud)

纠正这两个后,我可以移动项目: https: //codesandbox.io/s/r5z28w85yo。希望这有帮助。