React Reorder
Drag & drop, touch enabled, reorder / sortable list, React component
If you are using v2, please refer to this documentation.
About
React Reorder is a React component that allows the user to drag-and-drop items in a list (horizontal / vertical), or a grid. You can also allow dragging items from one list to another.
It fully supports touch devices and auto-scrolls when a component is being dragged (check out the demo).
It also allows the user to set a hold time (duration before drag begins) allowing additional events like clicking / tapping to be applied.
Installation
Using npm
Add --save
or -S
to update your package.json
npm install react-reorder
Using bower
Add --save
or -S
to update your bower.json
bower install react-reorder
Example
If using a module loader (requirejs / browserfiy / commonjs): require react-reorder
where it will be used in your project
var Reorder = require('react-reorder');
var reorder = Reorder.reorder;
var reorderImmutable = Reorder.reorderImmutable;
var reorderFromTo = Reorder.reorderFromTo;
var reorderFromToImmutable = Reorder.reorderFromToImmutable;
import Reorder, {
reorder,
reorderImmutable,
reorderFromTo,
reorderFromToImmutable
} from 'react-reorder';
Configuration
<Reorder
reorderId="my-list"
reorderGroup="reorder-group"
getRef={this.storeRef.bind(this)}
component="ul"
placeholderClassName="placeholder"
draggedClassName="dragged"
lock="horizontal"
holdTime={500}
touchHoldTime={500}
mouseHoldTime={200}
onReorder={this.onReorder.bind(this)}
autoScroll={true}
disabled={false}
disableContextMenus={true}
placeholder={
<div className="custom-placeholder" />
}
>
{
this.state.list.map((item) => (
<li key={item.name}>
{item.name}
</li>
)).toArray()
}
</Reorder>
Callback functions
The onReorder
function that is called once a reorder has been performed.
You can use our helper functions for reordering your arrays.
import { reorder, reorderImmutable, reorderFromTo, reorderFromToImmutable } from 'react-reorder';
onReorder (event, previousIndex, nextIndex, fromId, toId) {
this.setState({
myList: reorder(this.state.myList, previousIndex, nextIndex);
});
}
onReorderGroup (event, previousIndex, nextIndex, fromId, toId) {
if (fromId === toId) {
const list = reorderImmutable(this.state[fromId], previousIndex, nextIndex);
this.setState({
[fromId]: list
});
} else {
const lists = reorderFromToImmutable({
from: this.state[fromId],
to: this.state[toId]
}, previousIndex, nextIndex);
this.setState({
[fromId]: lists.from,
[toId]: lists.to
});
}
}
Compatibility / Requirements
- Version
3.x
tested and working with React 15
, but should be backward compatible at least a couple of versions.