• Stars
    star
    459
  • Rank 95,377 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A ReactJS component that can sort any children with touch support and IE8 compatibility

This project is in INACTIVE status, bugfix will be maintained, but no new feature will be added. Feel free to use it if it suits your need, for complicated sorting features I'd recommend react-dnd by dan.


react-anything-sortable Build Status npm version Coverage Status

Features

  • Sort any React element you like, images, composite components, etc.
  • No external dependencies but React itself
  • Touch event support
  • Thoroughly tested

Quick Demo

Live Demo

Sort custom style children

react-anything-sortable

Sort images

react-anything-sortable

Children with custom event handler

react-anything-sortable

Installation

$ npm install --save react-anything-sortable

// UMD build is provided as well, but please do consider use modern module bundlers like webpack or browserify.

You have to add necessary styles for sortable to work properly, if you're using bundle tools like webpack, just

import 'react-anything-sortable/sortable.css';

Or copy this css to your own style base.

How to use

You can check the straight-forward demo by examining demo folder, or here's a brief example.

In app.js

var ReactDOM = require('react-dom');
var Sortable = require('react-anything-sortable');
var SortableItem = require('./SortableItem');

ReactDOM.render(
<Sortable onSort={handleSort}>
  <SortableItem sortData="1" />
  <SortableItem sortData="2" />
</Sortable>
, document.body);

and in SortableItem.js

A modern usage would be

import React from 'react';
import { SortableContainer } from 'react-anything-sortable';

@sortable
class SortableItem extends React.Component {
  render() {
    return (
      <SortableContainer>
        <div>
          your item
        </div>
      </SortableContainer>
    );
  }
};

Or you want to construct it manually

import React from 'react';
import { sortable } from 'react-anything-sortable';

@sortable
class SortableItem extends React.Component {
  render() {
    return (
      <div                       // <-- make sure pass props to your own item,
        className={this.props.className}
        style={this.props.style}
        onMouseDown={this.props.onMouseDown}
        onTouchStart={this.props.onTouchStart}
      >
        your item                //     it contains required `className`s and
      </div>                     //     event handlers
    );
  }
};

Or if you favor the old fashion way

var React = require('react');
var createReactClass = require('create-react-class');
var SortableItemMixin = require('react-anything-sortable').SortableItemMixin;

var SortableItem = createReactClass({
  mixins: [SortableItemMixin],

  render: function(){
    return this.renderWithSortable(  // <-- this.renderWithSortable call is essential
      <div>your item</div>
    );
  }
});

You can even pass un-sortable children to <Sortable /> and it just works, checkout this demo to find out more. If you do so, remember to add according style to your un-sortable items.

Props

onSort

Type: Function Default: () => {}

Being called with sorted data when a sort operation is finished.

Arguments

  1. sortedArray (Array) Sorted array consists of sortData plucked from each sortable item
  2. currentDraggingSortData (Any) The sortData of dragging element
  3. currentDraggingIndex (Number) The index of dragging element

containment

Type: Bool Default: false

Constrain dragging area within sortable container.

demo

dynamic

Type: Bool Default: false

Dynamically update the sortable when its children change. If using this option, make sure to use the onSort callback to update the order of the children passed to the Sortable component when the user sorts!

demo

sortHandle

Type: String Default: undefined

A className to allow only matching element of sortable item to trigger sort operation.

demo

sortData

Add this props to SortableItem rather than Sortable !

Type: Any Default: undefined

Will be returned by onSort callback in the form of array.

direction

Type: String Default: false Options: vertical, horizontal

Will force dragging direction to vertical or horizontal mode.

Notice

  1. Specify your style for Sortable and Sortable Items, check sortable.css, it is NOT optional!
  2. Don't forget the this.renderWithSortable call in SortableItem, or spread props to your component if using decorators.
  3. In order to dynamically add or remove SortableItems or change their order from outside the Sortable, you must use the dynamic option. This also requires using the onSort callback to update the order of the children when sorting happens.
  4. Make sure to add draggable={false} to images within sortable components to prevent glitching. See here for an example.

Scripts

$ npm run test
$ npm run watch
$ npm run build
$ npm run demo
$ npm run demo:watch

Contributors

  1. stayradiated
  2. vizath
  3. zthomas
  4. jakubruffer
  5. Fabeline
  6. antialiasis
  7. JasonKleban

More Repositories

1

a-cartoon-intro-to-redux-cn

看漫画,学 Redux。不写一行代码,轻松看懂 Redux 原理!
HTML
768
star
2

react-menu-aim

A React mixin version of Amazon's jQuery-menu-aim plugin
JavaScript
160
star
3

redux-sequence-action

Dispatch your action one by one with previously updated store
JavaScript
101
star
4

react-marquee

A <marquee> component for React
JavaScript
81
star
5

redux-form-utils

Ease the pain of handling form bindings in Redux
JavaScript
73
star
6

china-city-select

简单可用的中国省份、城市、县区三级联动选择组件
JavaScript
60
star
7

react-lifecycle

A ReactJS mixin for logging component lifecycle method call
JavaScript
54
star
8

redux-composable-fetch

A fetch middleware with extensible and opt-in functionalities like cache or log for Redux
JavaScript
37
star
9

react-redux-async-example

A react, redux, ajax working example
JavaScript
33
star
10

bjut_crawler

BJUT secrets~
JavaScript
32
star
11

alarm

A simple & super fast & responsive Ghost theme
CSS
31
star
12

undefinedblog

我的个人博客(源码)
CSS
23
star
13

test-react

JavaScript
8
star
14

hexo-migrator-ghost

Ghost migrator for Hexo.
JavaScript
7
star
15

healthreport

A toy app to analyze health report and ask questions
JavaScript
5
star
16

In-page-Highlighter

Instantly highlight anything you select in current page (Chrome plug-in)
JavaScript
4
star
17

gulp-legacy-ie-css-lint

Ensure your CSS won't be discarded by IE 9 and below because of crazy stylesheets limitation of legacy IE.
JavaScript
3
star
18

grunt-wordpress

Simple & working Grunt configuration set for Wordpress development
JavaScript
3
star
19

easy-js-perf

A human readable wrapper of `window.performance` API
JavaScript
2
star
20

ultimate-gitlab-gitbook

Receive Gitlab web hook, generate content using Gitbook, enhance search functionality using ElasticSearch.
JavaScript
2
star
21

Ali_learning

JavaScript
2
star
22

webpack-debug-plugin

Inject useful debug info to each entry in the runtime
JavaScript
2
star
23

jsbin-nude

JS Bin with NO Backend Dependency
JavaScript
1
star
24

react-analyzer

Give you a human readable structure of your React component
JavaScript
1
star
25

canon-blog

Blog for canon
PHP
1
star
26

ceresdb

A simple node.js client for CeresDB
TypeScript
1
star
27

excel-gantt-chart

Automatically exported from code.google.com/p/excel-gantt-chart
1
star
28

jasonslyvia.github.io

1
star
29

grunt-wp-replace

Replace new static assets reference(js or css) in Wordpress theme source file
JavaScript
1
star
30

canon

Just another WordPress theme.
JavaScript
1
star