Comments (6)
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.
@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.
/** @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.
Oops, wrong link at first. Fixed.
from react-dnd.
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.
from react-dnd.
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.
I'll close the issue now that I added some examples to the project.
from react-dnd.
Related Issues (20)
- please keyboard functionality
- Preview Image only works once, on the first drag. HOT 3
- Uncaught Invariant Violation: Expected an existing source.
- Library breaks textareas, inputs etc. HOT 1
- table 拖拽时 使用最新版本的React-dnd Api时 出现node.addEventListener is not a function
- 拖动时useDrag无法访问组件内的变量 HOT 1
- @react-dnd/asap trigger swc error `env` and `jsc.target` cannot be used together
- Custom Drag Layer elements don't update until AFTER I drag it. HOT 1
- Redux v5
- Where is dropEffect coming from?
- Hello! I am kupraa from Indonesia, I am very excited to join the project and the communitystar_struck. Wish the project success and strong developmentheart
- [Question] Working with DnD and PrimeReact's DataTable HOT 1
- Will there be a new version released ? and when will the next version be released? HOT 1
- Missing license for some inner packages
- Unable to drag list item after removing/unchecking particular items. Need help.
- Cannot write a unit test to make sure that it is not possible to drag some draggable HOT 1
- Adding accessibility features to the library
- Jest encountered an unexpected token HOT 2
- Styling not working in the beginning of the tutorial chessboard.
- run demo online throw error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-dnd.