Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DragSource, DropTarget, DragDropContext as Components? #217

Closed
jehoshua02 opened this issue Jun 28, 2015 · 3 comments
Closed

DragSource, DropTarget, DragDropContext as Components? #217

jehoshua02 opened this issue Jun 28, 2015 · 3 comments

Comments

@jehoshua02
Copy link

I'm a big fan of reusable components. I'm currently trying to figure out how I would create a reusable SortableList component in React.

Consider this dream code:

import React from 'react';
import SortableList from 'sortable-list';
import MyItem from 'components/MyItem';

var items = [...];

React.render(
  <SortableList>
    {items.map(function (item) {
      return <MyItem data={item} key={item.id} />;
    })}
  </SortableList>,
  document.body
);

This SortableList component would magically enable items in the list to be reordered via drag and drop. I'm also dreaming that if two of these SortableLists were on the page, items could be dragged between them.

I'm having a hard time wrapping my head around how this would be done. Can anyone foresee the obstacles to doing this with React DnD? Is it feasible? Or should I give up and just make every DnD component custom without intention of reusing across projects?

@gaearon
Copy link
Member

gaearon commented Jun 28, 2015

You can try. ;-)

There'll be the issue of contexts I'm not sure yet how to solve properly. But once you figure out the rest I think you'll figure out something about it.

You could do something like

class SortableList {
  render() {
    return React.Children.map(this.props.children, child => <SortableItem>{child}</SortableItem>);
  }
}

@DragSource(...)
@DropTarget(...)
class SortableItem {
  render() {
    return this.props.connectDragSource(
      this.props.connectDropTarget(
        React.Children.only(this.props.children)
      )
    );
  }
}

So yeah, I think it's doable.

@gaearon
Copy link
Member

gaearon commented Sep 17, 2015

Closing as non-actionable: you can build these in userland, and React DnD is a lower level library.

@gaearon gaearon closed this as completed Sep 17, 2015
@sasidhar-d
Copy link

sasidhar-d commented Jul 6, 2016

@jehoshua02 any luck on this component? I am trying to build something similar, and the DragSource is messed up as the entire div gets dragged (the preview is wrong) instead of single SortableItem. It only works when the SortableItem is a simple div with no children, I am trying to do this with ListItem from react material UI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants