react-dnd icon indicating copy to clipboard operation
react-dnd copied to clipboard

ConnectDragSource the mapped list in component issue

Open hyphenwrex opened this issue 9 years ago • 5 comments

We all know how it is done for array of items in React, we map it and return <li> item with some data injected. I have <ul> that contains some <li>'s

// ListPanel class:
render() {
    let list = this.props.items.map(i => {
        return connectDragSource(
          <li
            data-id={i.id}
            key={i.id}
            onClick={this.props.clickHandler.bind(this, i)}
          >
            {i.name}
          </li>
        );
    });


    return connectDropTarget(
      <div className={listPanel}>
        {p.children}
        <ul>
          {list}
        </ul>
      </div>
    );
}

export default DropTarget(dndConstants.ITEM, listTarget, collectDrop)(
  DragSource(dndConstants.ITEM, itemSource, collectDrag)(ListPanel)
);

and I want to drag <li> into other ListPanel component, I have multiple instances of those presented. Issue is that I don't know a way of knowing which <li> is being dragged. And I need the id of it to implement transfer to another list.

beginDrag(props, monitor, component) method of drag source, and component argument specifically would point me to whole component with all list rendered instead of the list item.

So my question is: how do I get access to specific dragged list item? I mean I could do something like childs to ListPanel or make it own the list item component, but React is known to be very unopinionated in those kind of things, and I want to keep it in single component.

Or do I miss something very obvious here? Please point me out.

Edit: I went with moving <li> item logic into its own component. Solution is quite straightforward now. I'm still curious is it possible to go with my initial approach. So if you can think of a way to achieve this, please comment on that. If not or it will take you a lot of time, - feel free to close the issue. I will leave it open for now.

hyphenwrex avatar Mar 03 '17 03:03 hyphenwrex

@hyphen-wrex I hope I understood your question right (Correct me if I miss anything?). When you are implementing connectinDragSource to a li element, you can pass

beginDrag(props){
   return {
     index: props.index, // Index of li element
  }
}

We can capture index in targetand fire interested actions

anilreddykatta avatar Mar 18 '17 07:03 anilreddykatta

hey! I'm new to React-Redux/dnd. Working through the same problem now. Do you have the complete code for your solution (moving

  • item logic into its own component) with connectDragSource? Thanks!

    vishalpatel2890 avatar May 22 '17 04:05 vishalpatel2890
  • @vishalpatel2890 sure man, here you go:

    import React, { Component } from 'react';
    import { DragSource } from 'react-dnd';
    
    const dndConstants = {
      ITEM: 'listItem'
    };
    
    // D R A G    S O U R C E
    // source object:
    const itemSource = {
     beginDrag(props, monitor, component) {
       return {
         id: props.id
       }
     }
    }
    // collect function:
    function collectDrag(connect, monitor) {
     return {
       connectDragSource: connect.dragSource(),
      //  isDragging: monitor.isDragging(),
       dragItemId: monitor.getItem()
     }
    }
    // D R A G    S O U R C E    E N D
    
    class Item extends Component {
      render() {
        const { name, id, style, onClick, flags, connectDragSource } = this.props;
        return connectDragSource(
          <div className="">
            <li
              key={id}
              style={style}
              onClick={onClick}
            >
              {name}
              {flags.map(f => (
                <img key={f} src={require(`../misc/${f}.svg`)}/>
              ))}
            </li>
          </div>
        );
      }
    }
    
    export default DragSource(dndConstants.ITEM, itemSource, collectDrag)(Item);
    
    import React, { Component } from 'react';
    import { listPanel } from '../styles/listPanel.css';
    import { DropTarget } from 'react-dnd';
    import ListItem from './listItem';
    
    class ListPanel extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          selectedId: null,
        }
      }
    
      render() {
        const { connectDropTarget, items, selected, children } = this.props;
    
        return connectDropTarget(
          <div className={listPanel}>
            {children}
            <ul>
              {items.map(i => {
                let bgc;
                if(selected == i.id)
                  bgc = 'inherit';
                else
                  bgc = 'lightgreen';
                return <ListItem
                  key={i.id}
                  style={{backgroundColor: bgc}}
                  onClick={this.props.clickHandler.bind(this, i)}
                  {...i}
                />
              })}
            </ul>
          </div>
        );
      }
    }
    
    
    /*
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * REACT DND
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     */
     const dndConstants = {
       ITEM: 'listItem'
     };
    
    // D R O P    T A R G E T
    // source object:
    const listTarget = {
      drop(props, monitor) {
        const item = monitor.getItem();
        props.moveItem(item.id, props.panelId);
      }
    };
    // collect function:
    function collectDrop(connect, monitor) {
      return {
        connectDropTarget: connect.dropTarget(),
      }
    }
    
    // D R O P    T A R G E T    E N D
    
    /*
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * REACT DND START
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     */
    
    export default DropTarget(dndConstants.ITEM, listTarget, collectDrop)(ListPanel);
    
    // DragSource
    

    hyphenwrex avatar May 23 '17 05:05 hyphenwrex

    Hello @hyphen-wrex Can I have your ListItem component please? thx

    ibelick avatar Mar 16 '18 16:03 ibelick

    No man, not anymore. I've deleted the project as I wasn't investing in it.

    hyphenwrex avatar Mar 17 '18 03:03 hyphenwrex