Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested Drag and Drop with react-beautiful-dnd

I'm creating vertical nested drag and drop with react-beautiful-dnd. I have referred some answers in github

I have forked from sandbox and created new one for vertical nested drag and drop. When I change the inside DND, outside DND changes the values not inside DND.

enter image description here

CODE

 onDragEnd(result) {
    // dropped outside the list
    console.log("innner drag");
    if (!result.destination) {
      return;
    }

    const items = reorder(
      this.state.items,
      result.source.index,
      result.destination.index
    );

    this.setState({
      items
    });
  }

DEMO Code: https://codesandbox.io/s/ozq53zmj6

like image 776
Selvin Avatar asked Oct 15 '18 09:10

Selvin


People also ask

How to make a list draggable and dropable with react beautiful DND?

1 Installing React Beautiful DnD First step is to install the library via npm. ... 2 Making a list draggable and droppable with React Beautiful DnD With our library installed, we can give our list the ability to drag and drop. ... 3 Saving list order after reordering items with React Beautiful DnD

Should I use React Dnd or react-beautiful-DND?

As noted in their documentation, react-beautiful-dnd is best suited for lists (vertical, horizontal, movements between lists, nested lists, and so on), and has limitations to provide more extensive drag and drop functionality. We ultimately went with React DnD for their powerful primitives to support handling user interfaces based on data changes.

How do I use drag and drop in react?

DragDropContext: In order to use drag and drop, you need to have the part of your React tree that you want to be able to use drag and drop in wrapped in a <DragDropContext />. It is advised to just wrap your entire application in a <DragDropContext />.

What is drag and drop in DND?

Drag and Drop is a common interaction technique added to allow people to intuitively move things around on a page. This could be reordering a list or even putting together a puzzle. How can we add that interaction when building a React app with React Beautiful DnD? What is Drag and Drop? What is React Beautiful DnD? What are we going to build?


Video Answer


2 Answers

react-beautiful-dnd doesn't support nested drag-drop as of now and it's low priority item as per their roadmap. You need to handle everything on outer <DragDropContext onDragEnd={this.handleDragEnd}>. It doesn't give parent information by default in result object, so I have kept that information in child Droppable component. See the below demo if this works for you.

Code: https://codesandbox.io/s/jp4ow4r45v

Edit: Refer below sandbox for a more generic code snippet where you will be able to apply nested drag-drop across parent container.

Code: https://codesandbox.io/s/5v2yvpjn7n

like image 84
Debajit Majumder Avatar answered Oct 21 '22 23:10

Debajit Majumder


just give type to your outer and inner droppable, on drag end you have to check the type of your droppable,and reorder accordingly.

onDragEnd = ({ type, destination, source }) => {

    if (source && destination && type) {
        let parentId = source.droppableId;
        let srcIndex = source.index;
        let destIndex = destination.index;

        if (type == "Inner") {
            //method for reordering the order of the inner items
            reorderInner(parentId, srcIndex, destIndex)
        }
        else if (type == "Outer") {
            //method for reordering the order of parent items
            reorderOuter(srcIndex,destIndex)
        }
    }

};
like image 1
Yash Ojha Avatar answered Oct 21 '22 23:10

Yash Ojha