react-anything-sortable
is a ReactJS component that can sort any component passed as this.props.children
. It is compatible with IE8 and all modern browsers.
It has no external dependencies but React
itself.
Sort custom style children
Sort images
Children with custom event handler
$ npm install --save react-anything-sortable
// or use bower
$ bower install --save react-anything-sortable
You can check the straight-forward demo by examining demo
folder, or here's a brief example.
In YourComponent.jsx
var React = require('react');
var Sortable = require('react-anything-sortable');
var YourSortableItem = require('./YourItem');
React.renderComponent(
<Sortable onSort={handleSort}>
<YourItem sortData="1" />
<YourItem sortData="2" />
</Sortable>
, document.body);
and in YourItem.jsx
Notice: There's a breaking change in requring SortableItemMixin
in version 0.2.0
var React = require('react');
var SortableItemMixin = require('react-anything-sortable').SortableItemMixin;
var YourItem = React.createClass({
mixins: [SortableItemMixin],
render: function(){
return this.renderWithSortable(
<div>your item</div>
);
}
});
- Specify your style for
Sortable
andSortable Items
, checkdemo/style.css
, it is NOT optional! - Don't forget the
this.renderWithSortable
call inYourItem.jsx
- Specify
sortData
inYourItem.jsx
so thatSortable
can return the sorted array - Add
onSort
props toSortable
to be noticed when a sort operation finished - Since we can't track any children modification in
Sortable
, you have to usekey
to force updateSortable
when adding/removing children.
$ npm run test