Git Product home page Git Product logo

Comments (6)

gaearon avatar gaearon commented on May 8, 2024

I meant to add some examples this week to the repo but I agree it's probably best to start with most simple one in JSFiddle. I'll try to get it done a bit later today or tomorrow. Thanks.

from react-dnd.

gaearon avatar gaearon commented on May 8, 2024

@devangpaliwal I made a very simple fiddle showing basic drag-n-drop:

http://jsbin.com/cahenaninu/2/edit?html,js,output

It lets you drag “persons” into a “box”. This fiddle only shows the most simple interaction (dragging items into a target), but it's possible to add sorting, several item types, several drop targets, et cetera). I may extend this example later.

screen shot 2014-10-21 at 3 18 42

/** @jsx React.DOM */

var DragDropMixin = ReactDND.DragDropMixin;

var ItemTypes = {
  PERSON: 'person'
};

// Drag source
var Person = React.createClass({
  mixins: [DragDropMixin],

  propTypes: {
    name: React.PropTypes.string,
    isInBox: React.PropTypes.bool
  },

  configureDragDrop: function (registerType) {
    registerType(ItemTypes.PERSON, {
      dragSource: {
        canDrag: function () {
          return !this.props.isInBox;
        },

        beginDrag: function () {
          return {
            item: { name: this.props.name }
          };
        }
      }
    });
  },

  render: function() {
    return (
      <div className='Person'
           {...this.dragSourceFor(ItemTypes.PERSON)}>
        {this.props.name}
      </div>
    );
  }
});

// Drop target
var Box = React.createClass({
  mixins: [DragDropMixin],

  propTypes: {
    inside: React.PropTypes.arrayOf(React.PropTypes.string),
    onAddPerson: React.PropTypes.func.isRequired
  },

  configureDragDrop: function (registerType) {
    registerType(ItemTypes.PERSON, {
      dropTarget: {
        acceptDrop: function (person) {
          // Specify action on drop
          this.props.onAddPerson(person.name);
        }
      }
    });
  },

  render: function () {
    return (
      <div className='Box'
           {...this.dropTargetFor(ItemTypes.PERSON)}>

        <h3>Drop folks here!</h3>

        {this.props.inside.map(function (name) {
          return <Person name={name} key={name}
                         isInBox />;
        })}
      </div>
    );
  }
});

var App = React.createClass({
  getInitialState: function () {
    return {
      outsideBox: [
        'Pete',
        'Ryan',
        'Andrey',
        'Anita'
      ],
      insideBox: []
    };
  },

  render: function () {
    return (
      <div>
        <h1>These folks are not yet in the box:</h1>

        {this.state.outsideBox.map(function (name) {
          return <Person name={name} key={name} />;
        })}
        <hr />

        <Box inside={this.state.insideBox}
             onAddPerson={this.handleAddPerson} />
      </div>
    )
  },

  handleAddPerson: function (name) {
    this.setState({
      outsideBox: this.state.outsideBox.filter(function (n) { return n !== name }),
      insideBox: this.state.insideBox.concat([name])
    });
  }
})

React.renderComponent(<App />, document.body);

Note that I needed Object.assign polyfill (JSX Spread Attributes syntax requires it).
I also included a UMD build in dist folder so react-dnd can be used in a fiddle.

from react-dnd.

gaearon avatar gaearon commented on May 8, 2024

Oops, wrong link at first. Fixed.

from react-dnd.

devangpaliwal avatar devangpaliwal commented on May 8, 2024

Would it make sense to create a demo like : http://html5demos.com/drag to showcase how this repo can be used? Let me know. I may be able to help. I created a quick and dirty version on my machine. Have a look at the screenshot.

screen shot 2014-10-21 at 5 32 56 pm

from react-dnd.

gaearon avatar gaearon commented on May 8, 2024

Please do share it! I plan to have a comprehensive set of examples in examples folder, runnable with Webpack dev server. If you could make this first one, it would be nice.

from react-dnd.

gaearon avatar gaearon commented on May 8, 2024

I'll close the issue now that I added some examples to the project.

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.