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. (In upcoming releases, it will add two more dependencies since React has split addons
into separate npm modules.)
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
//
// or just include `lib/index.js` in a <script> tag and
// use it by `window['react-anything-sortable']`;
//
// technically AMD is supported too, but seriously you gonna stick with it in late 2015?
You can check the straight-forward demo by examining demo
folder, or here's a brief example.
In YourComponent.js
var React = require('react');
var Sortable = require('react-anything-sortable');
var YourSortableItem = require('./YourItem');
ReactDOM.render(
<Sortable onSort={handleSort}>
<YourItem sortData="1" />
<YourItem sortData="2" />
</Sortable>
, document.body);
and in YourItem.js
Notice: There's a breaking change for requring SortableItemMixin
since 0.2.0
ES6 import
is recommended.
import React from 'react';
import {SortableItemMixin} from 'react-anything-sortable';
const YourItem = React.createClass({
mixins: [SortableItemMixin],
render() {
return this.renderWithSortable( // <= this.renderWithSortable is important
<div>your item</div>
);
}
});
Or if your favor the old fashion way
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.js
- Specify
sortData
inYourItem.js
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
$ npm run watch
$ npm run build