Giter VIP home page Giter VIP logo

Comments (32)

melvynhills avatar melvynhills commented on May 6, 2024 17

If anyone's interested, I implemented a multiple drag / grid view: https://codesandbox.io/s/9j897k0mwy.
You can use cmd/ctrl and shift keys to select multiple items, then drag them around the grid and insert them anywhere.

This is still a demo, but I might abstract the code a little bit more and package it in a component later.

from react-dnd.

danii1 avatar danii1 commented on May 6, 2024 11

I implemented it, solution is basically what is described in the first post:

  1. Keep track of your selected items
  2. Implement CustomDragLayer (see https://gaearon.github.io/react-dnd/docs-drag-layer.html) and render your selected items yourself
  3. Handle drop if dropped item is part of the selection

from react-dnd.

danii1 avatar danii1 commented on May 6, 2024 10

Has anyone implemented dragging multiple items with react-dnd in their applications? What's your current solution for this?

from react-dnd.

znwang25 avatar znwang25 commented on May 6, 2024 9

@abhidnyag Here is my current solution with react-dnd based on @melvynhills's codesandbox. I rewrote the code using new APIs and react hooks.

You can try the live demo here.

from react-dnd.

nayemmajhar avatar nayemmajhar commented on May 6, 2024 7

I will write a post and add repo about multiple and nested drag and drop with react-dnd + redux.

from react-dnd.

paulius005 avatar paulius005 commented on May 6, 2024 2

@ianmclean2011 check it out here: #590

from react-dnd.

ianmclean2011 avatar ianmclean2011 commented on May 6, 2024 2

I'm trying to implement multi-drag in an example similar to the sortable example here: https://react-dnd.github.io/react-dnd/examples-sortable-simple.html. I want to be able to select any objects, and then move them as a unit up and down the list. I'm having trouble wrapping my head around how to use the examples/info shared above to do this. Any thoughts/ideas/feedback on how this might work?

from react-dnd.

pavel06081991 avatar pavel06081991 commented on May 6, 2024 1

@nayemmajhar, it would be great

from react-dnd.

jedwards1211 avatar jedwards1211 commented on May 6, 2024 1

@melvynhills @jmcrthrs other desktop dnd frameworks I've used rely on making lists and tables the drag sources/drop targets instead of making each individual row, cell, item etc. its own drag source and/or drop target. The implementation for dragging multiple items, as well as getting the specific drop location within the list or table, comes out a lot cleaner that way. When a list receives a drag start event it can simply look at its current selected items.

from react-dnd.

gaearon avatar gaearon commented on May 6, 2024

Not a priority. We can revisit after introducing drag source keys (#38 (comment)).

from react-dnd.

nelix avatar nelix commented on May 6, 2024

I'll be interested in this down the road, although I think you might be able to just wrap a section of your component tree with a component that has a dragSource...
Like if you hold shift and click, it expands what sections of the array are copied into the dragItem or something?

from react-dnd.

gaearon avatar gaearon commented on May 6, 2024

Kinda.

First we learn to pass null component (needed for #38) and a different component with same key (needed for #53).

Then we add groupKey which is optional. Like key, it is specified in drag source and is a string. When you begin dragging something with a groupKey, we will call beginDrag not just on the caller, but on every mounted drag source with the same groupKey, and gather their items into an array. We will pass these items instead of item to drop target methods. When dragging is over, we will call acceptDrop on drop target with an item array, and then call each drag source's endDrag with their item.

What's fun is that we can draw DragLayer with a custom “composite” thumbnail component and a counter like I screenshotted above because we know the count from items.length.

from react-dnd.

nelix avatar nelix commented on May 6, 2024

Thats a way less ugly idea than what I had in mind

from react-dnd.

gre avatar gre commented on May 6, 2024

👍 for this feature :-)

from react-dnd.

gaearon avatar gaearon commented on May 6, 2024

Closing, as I don't currently have a use case for this, and it's a significant implementation effort.

from react-dnd.

jedwards1211 avatar jedwards1211 commented on May 6, 2024

@gaearon

I used Java/Swing DnD back in the day, and they had probably an army of developers who grappled with this problem and came up with a pretty refined, extremely generic and flexible framework. I would recommend researching old desktop frameworks as they probably solved all the difficult challenges with making a good DnD framework like this.

The way to do this in Java/Swing (although it had no built-in support for drag images) was to make the tree view itself the drag source, and make it able to decide whether or not a drag should be initiated based on its current state and the drag start position. Is monitor.getInitialSourceClientOffset() available when canDrag and beginDrag are called? People will definitely want it to be available. They will probably also want modifier keys (ctrl, alt, shift) to be available and to be able to make drag sources specify whether to move or copy, and drop targets to specify whether they support move and/or copy, based upon any of these factors, because that is possible in Java/Swing and I'm guessing other frameworks. Swing also had the concept of drag gesture recognizers that could be swapped out.

I'd predict that treating all the items in the list as individual draggables will just be difficult compared to having the list component be the single drag source that figures out what kind of drop payload / preview image to construct based upon what's selected in it.

Consider also that a developer might want to change the order of the items being dragged based upon which item the user clicked and dragged from. I believe Windows Explorer behaves this way.

from react-dnd.

nayemmajhar avatar nayemmajhar commented on May 6, 2024

did you mean multiple drop target for single drag source?

from react-dnd.

stouchardIpso avatar stouchardIpso commented on May 6, 2024

+1 @danii1 : Has anyone implemented dragging multiple items ?

from react-dnd.

serle avatar serle commented on May 6, 2024

I implemented it by:

  1. Keep track of selected items in a Set
  2. Pass this as a property to the dnd Source
  3. In beginDrag retrieve the Set form the props and return it as the drag item
  4. Create a custom drag preview
  5. On EndDrag move all the items in the Set into the dnd Target

from react-dnd.

thessem avatar thessem commented on May 6, 2024

I have a slightly different use case for this I think, I want to draw nodes that are draggable with edges that aren't draggable but do move when nodes they're attached to move. My current implementation is a CustomDragLayer that draws the edges when it detects a DraggableNode is moving (also drawn on the CustomDragLayer but I think it might be better to model it as a multiple drag situation by implementing all edges as DragSources with isDragging() implemented to detect if a node they're attached to is moving so that they can draw themselves instead of having the CustomDragLayer inject the coordinates as props.

from react-dnd.

neha-brahmankar avatar neha-brahmankar commented on May 6, 2024

@danii1
Can you please share the one you have implemented for a reference

from react-dnd.

paulius005 avatar paulius005 commented on May 6, 2024

@nayemmajhar any update on an example with this?

from react-dnd.

nayemmajhar avatar nayemmajhar commented on May 6, 2024

I have developed this application using react DnD with reduxJS long ago https://www.joomshaper.com/page-builder I am writing a tutorial but need time publish

from react-dnd.

ianmclean2011 avatar ianmclean2011 commented on May 6, 2024

@serle Would you be able to share a code example of how you did this?

from react-dnd.

jmcrthrs avatar jmcrthrs commented on May 6, 2024

@melvynhills Have you investigated a solution that doesn't require passing all the selected cards to each Card component? I don't think there is a solution considering the Card's beginDrag() needs a reference to all selected Cards.

from react-dnd.

melvynhills avatar melvynhills commented on May 6, 2024

@jmcrthrs No, it doesn't seem possible to me with react-dnd API. I don't think there's a way to know before dragging which specific item is going to be dragged. That's also the only way to know whether the dragged item is within the multiple selection you made, or if it's outside of that selection that we then need to replace.

from react-dnd.

melvynhills avatar melvynhills commented on May 6, 2024

@jedwards1211 do you think it's doable with this library? Especially the use-case of starting dragging an item that wasn't selected before the drag start. I'm not sure it's the way react-dnd is supposed to work.

from react-dnd.

piotrpawlik avatar piotrpawlik commented on May 6, 2024

Does anybody solved case in which there are multiple drop targets?
Let's say one is dragging multiple items (using one of the solutions above) and one item ends up on one type of drop target and the other on the different one. drop will be triggered just for drag source which initiated drag, but not for the other one. Any ideas how to trigger drop individually for each item being moved.
Example:
Is development slow, Online Whiteboard for Visual Collaboration 2019-11-27 10-45-33

EDIT: Created separate issue for this one #1650

from react-dnd.

jedwards1211 avatar jedwards1211 commented on May 6, 2024

@melvynhills probably if a mousedown handler triggers item selection (which you should be doing, all software with DnD I have used works this way), it will work just fine with React DnD for the container component to be the drag source, because the drag events will come after mousedown.

from react-dnd.

jedwards1211 avatar jedwards1211 commented on May 6, 2024

Have you investigated a solution that doesn't require passing all the selected cards to each Card component?

This is an example of why I would recommend making the container component the drag source instead of the individual items. I haven't needed to implement multiselect with React DnD but maybe sometime I can make a sandbox to convince folks it's the more maintainable approach

from react-dnd.

abhidnyag avatar abhidnyag commented on May 6, 2024

Has anyone implemented dragging multiple items with react-beautiful-dnd in their applications? What's your current solution for this?

from react-dnd.

aniketADG avatar aniketADG commented on May 6, 2024

Feature request : Hover on target image makes copy of selected images which is given in below image
@znwang25 and @melvynhills Is it possible if I drag multiple images and hover on another image then that all selected drag images get paste another image.
Something like multiple image selection and upload
image
check above image in that green note indicate image 4 to 22 is copy inside that image.

from react-dnd.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.