Search code examples
reactjsdrag-and-dropdraggablereact-dnd

connectDragSource for Each Element in map (Drag and Drop: React DnD)


How to make each item isDroppable? As the map() function return new Array I decide to pass the connectDragSource into the method, but I still get back the droppable Aray insted droppable Each Item (Shape)

for..in, forEach, for..of also doesn't get back the desire result

Has somebody resolve such issue?

import React, { Component } from "react";
import { Segment, Icon } from "semantic-ui-react";
import { DragSource } from "react-dnd";
import ShapeItem from "./ShapeItem";

class Shapes extends Component {

  displayShapes = (shapes, connectDragSource) =>
    shapes.length > 0 &&
    shapes.map((shape) =>
      connectDragSource(
        <div key={shape.id}>
          <Segment>
            <Icon name={shape.name} size={shape.size} color={shape.color} />
          </Segment>
        </div>
      )
    );

  render() {
    const { shapes, connectDragSource} = this.props;
    return (
      <div>
        {this.displayShapes(shapes, connectDragSource)}
      </div>
    );
  }
}

const spec = {
  beginDrag(props) {
    return {
      shapeId: props.shapes.id
    };
  }
};

const collect = (connect, monitor) => ({
  connectDragSource: connect.dragSource(spec),
  isDragging: monitor.isDragging()
});

export default DragSource("shape", spec, collect)(Shapes);

As for documentation http://react-dnd.github.io found just only this: React element as the draggable node. To do that, replace any element with this.props.connectDragSource(element) inside the render function.


Solution

  • You might want to create a separate component to render each item and make it the drag source. You also need to return true from canDrag function in spec.

    Disclaimer: I haven't tested the code below.

    shapes.js

    import React, { Component } from "react";
    import Item from './item.js';
    
    class Shapes extends Component {    
        render() {
            const { shapes } = this.props;
            return (
                <div>
                    {
                        shapes.length > 0 && 
                        shapes.map((shape) => <Item key={shape.id} shape={shape} />)
                    }
                </div>
            );
        }
    }
    
    export default Shapes;
    

    item.js

    import React, { Component } from "react";
    import { Segment, Icon } from "semantic-ui-react";
    import { DragSource } from "react-dnd";
    
    class Item extends Component {    
        render() {
            const { shape, connectDragSource} = this.props;
            return connectDragSource(
                <div>
                     <Segment>
                         <Icon name={shape.name} size={shape.size} color={shape.color} />
                     </Segment>
                </div>
            )
        }
    }
    
    const spec = {
        beginDrag(props) {
            return {
                shapeId: props.shapes.id
            };
        },
        canDrag(props, monitor) {
            return true;
        },
        endDrag(props, monitor) {
            console.log("You dropped .... into " + monitor.getDropResult());
        }
    };
    
    const collect = (connect, monitor) => ({
      connectDragSource: connect.dragSource(spec),
      isDragging: monitor.isDragging()
    });
    
    export default DragSource("shape", spec, collect)(Item);