GithubHelp home page GithubHelp logo

bvaughn / react-sortable-hoc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clauderic/react-sortable-hoc

4.0 2.0 1.0 339 KB

✌️ A set of higher-order components to turn any list into an animated, touch-friendly, sortable list.

Home Page: http://clauderic.github.io/react-sortable-hoc/

License: MIT License

JavaScript 91.87% HTML 0.92% CSS 7.21%

react-sortable-hoc's Introduction

React Sortable (HOC)

A set of higher-order components to turn any list into an animated, touch-friendly, sortable list.

npm version npm downloads license XO code style Gitter

Features

  • Higher Order Components – Integrates with your existing components
  • Drag handle, auto-scrolling, locked axis, events, and more!
  • Suuuper smooth animations – Chasing the 60FPS dream 🌈
  • Works with React Virtualized, React-Infinite, etc.
  • Horizontal or vertical lists ↔ ↕
  • Touch support 👌

Installation

Using npm:

$ npm install react-sortable-hoc --save

Then, using a module bundler that supports either CommonJS or ES2015 modules, such as webpack:

// Using an ES6 transpiler like Babel
import {SortableContainer, SortableElement} from 'react-sortable-hoc';

// Not using an ES6 transpiler
var Sortable = require('react-sortable-hoc');
var SortableContainer = Sortable.SortableContainer;
var SortableElement = Sortable.SortableElement;

Alternatively, an UMD build is also available:

<script src="react-sortable-hoc/dist/umd/react-sortable-hoc.js"></script>

Usage

Basic Example

import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement, arrayMove} from 'react-sortable-hoc';

const SortableItem = SortableElement(({value}) => <li>{value}</li>);

const SortableList = SortableContainer(({items}) => {
	return (
		<ul>
			{items.map((value, index) =>
                <SortableItem key={`item-${index}`} index={index} value={value} />
            )}
		</ul>
	);
});

class SortableComponent extends Component {
    state = {
        items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6']
    }
    onSortEnd = ({oldIndex, newIndex}) => {
        this.setState({
            items: arrayMove(this.state.items, oldIndex, newIndex)
        });
    };
    render() {
        return (
            <SortableList items={this.state.items} onSortEnd={this.onSortEnd} />
        )
    }
}

render(<SortableComponent/>, document.getElementById('root'));

That's it! React Sortable does not come with any styles by default, since it's meant to enhance your existing components.

More code examples are available here.

Prop Types

SortableContainer HOC

Property Type Default Description
axis String y The axis you want to sort on, either 'x' or 'y'
lockAxis String If you'd like, you can lock movement to an axis while sorting. This is not something that is possible with HTML5 Drag & Drop
helperClass String You can provide a class you'd like to add to the sortable helper to add some styles to it
transitionDuration Number 300 The duration of the transition when elements shift positions. Set this to 0 if you'd like to disable transitions
pressDelay Number 0 If you'd like elements to only become sortable after being pressed for a certain time, change this property. A good sensible default value for mobile is 200
onSortStart Function Callback that get's invoked when sorting begins. function({node, index, collection}, event)
onSortMove Function Callback that get's invoked during sorting as the cursor moves. function(event)
onSortEnd Function Callback that get's invoked when sortin ends. function({oldIndex, newIndex, collection}, e)
useDragHandle Boolean false If you're using the SortableHandle HOC, set this to true
useWindowAsScrollContainer Boolean false If you want, you can set the window as the scrolling container
hideSortableGhost Boolean true Whether to auto-hide the ghost element. By default, as a convenience, React Sortable List will automatically hide the element that is currently being sorted. Set this to false if you would like to apply your own styling.
lockToContainerEdges Boolean false You can lock movement of the sortable element to it's parent SortableContainer
lockOffset OffsetValue* | [OffsetValue*, OffsetValue*] "50%" When lockToContainerEdges is set to true, this controls the offset distance between the sortable helper and the top/bottom edges of it's parent SortableContainer. Percentage values are relative to the height of the item currently being sorted. If you wish to specify different behaviours for locking to the top of the container vs the bottom, you may also pass in an array (For example: ["0%", "100%"]).
getContainer Function Optional function to return the scrollable container element. This property defaults to the SortableContainer element itself or (if useWindowAsScrollContainer is true) the window. Use this function to specify a custom container object (eg this is useful for integrating with certain 3rd party components such as FlexTable). This function is passed a single parameter (the wrappedInstance React element) and it is expected to return a DOM element.

* OffsetValue is either a finite Number or a String made-up of a number and a unit (px or %). Examples: 10 (is the same as "10px"), "50%"

SortableElement HOC

Property Type Default Required? Description
index Number This is the element's sortableIndex within it's collection. This prop is required.
collection Number or String 0 The collection the element is part of. This is useful if you have multiple groups of sortable elements within the same SortableContainer. Example
disabled Boolean false Whether the element should be sortable or not
Why shoud I use this?

There are already a number of great Drag & Drop libraries out there (for instance, react-dnd is fantastic). If those libraries fit your needs, you should definitely give them a try first. However, most of those libraries rely on the HTML5 Drag & Drop API, which has some severe limitations. For instance, things rapidly become tricky if you need to support touch devices, if you need to lock dragging to an axis, or want to animate the nodes as they're being sorted. React Sortable HOC aims to provide a simple set of higher-order components to fill those gaps. If you're looking for a dead-simple, mobile-friendly way to add sortable functionality to your lists, then you're in the right place.

Dependencies

React Sortable List has very few dependencies. It depends on invariant and a couple lodash functions. It has the following peerDependencies: react, react-dom

Reporting Issues

If believe you've found an issue, please report it along with any relevant details to reproduce it. The easiest way to do so is to fork this jsfiddle.

Contributions

Yes please! Feature requests / pull requests are welcome.

Made with ❤︎ in the heart of Montreal.

react-sortable-hoc's People

Contributors

ahmedelgabri avatar brianreavis avatar bvaughn avatar clauderic avatar zaygraveyard avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dimduj

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.