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.
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
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
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.
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 />.
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?
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
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)
}
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With