项目作者: JakeSidSmith

项目描述 :
Drag & drop, touch enabled, reorderable / sortable list, React component
高级语言: JavaScript
项目地址: git://github.com/JakeSidSmith/react-reorder.git
创建时间: 2015-01-21T14:10:55Z
项目社区:https://github.com/JakeSidSmith/react-reorder

开源协议:MIT License

下载


React Reorder (v2)

Drag & drop, touch enabled, reorder / sortable list, React component

If you are using v3 alpha, 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.

It fully supports touch devices and auto-scrolls when a component is being dragged (check out the demo, link below).

It also allows the user to set a hold time (duration before drag begins) allowing additional events like clicking / tapping to be applied.

Although this project is very new, it has been thoroughly tested in all modern browsers (see supported browsers).

Demo

Installation

  • Using npm

    1. npm install react-reorder

    Add --save or -S to update your package.json

  • Using bower

    1. bower install react-reorder

    Add --save or -S to update your bower.json

Example

  1. If using requirejs: add react-reorder to your require.config

    1. paths:
    2. // <name> : <path/to/module>
    3. 'react-reorder': 'react-reorder/reorder'
    4. }
  2. If using a module loader (requirejs / browserfiy / commonjs): require react-reorder where it will be used in your project

    1. var Reorder = require('react-reorder');

    If using requirejs you’ll probably want to wrap your module e.g.

    1. define(function (require) {
    2. // Require react-reorder here
    3. });
  3. Configuration

    Note: If your array is an array of primitives (e.g. strings) then itemKey is not required as the string itself will be used as the key, however item keys must be unique in any case

    1. Using JSX

      1. <Reorder
      2. // The key of each object in your list to use as the element key
      3. itemKey='name'
      4. // Lock horizontal to have a vertical list
      5. lock='horizontal'
      6. // The milliseconds to hold an item for before dragging begins
      7. holdTime='500'
      8. // The list to display
      9. list={[
      10. {name: 'Item 1'},
      11. {name: 'Item 2'},
      12. {name: 'Item 3'}
      13. ]}
      14. // A template to display for each list item
      15. template={ListItem}
      16. // Function that is called once a reorder has been performed
      17. callback={this.callback}
      18. // Class to be applied to the outer list element
      19. listClass='my-list'
      20. // Class to be applied to each list item's wrapper element
      21. itemClass='list-item'
      22. // A function to be called if a list item is clicked (before hold time is up)
      23. itemClicked={this.itemClicked}
      24. // The item to be selected (adds 'selected' class)
      25. selected={this.state.selected}
      26. // The key to compare from the selected item object with each item object
      27. selectedKey='uuid'
      28. // Allows reordering to be disabled
      29. disableReorder={false}
      30. ></Reorder>
    2. Using standard Javascript

      1. React.createElement(Reorder, {
      2. // The key of each object in your list to use as the element key
      3. itemKey: 'name',
      4. // Lock horizontal to have a vertical list
      5. lock: 'horizontal',
      6. // The milliseconds to hold an item for before dragging begins
      7. holdTime: '500',
      8. // The list to display
      9. list: [
      10. {name: 'Item 1'},
      11. {name: 'Item 2'},
      12. {name: 'Item 3'}
      13. ],
      14. // A template to display for each list item
      15. template: ListItem,
      16. // Function that is called once a reorder has been performed
      17. callback: this.callback,
      18. // Class to be applied to the outer list element
      19. listClass: 'my-list',
      20. // Class to be applied to each list item's wrapper element
      21. itemClass: 'list-item',
      22. // A function to be called if a list item is clicked (before hold time is up)
      23. itemClicked: this.itemClicked,
      24. // The item to be selected (adds 'selected' class)
      25. selected: this.state.selected,
      26. // The key to compare from the selected item object with each item object
      27. selectedKey: 'uuid',
      28. // Allows reordering to be disabled
      29. disableReorder: false
      30. })
  4. Callback functions

    1. The callback function that is called once a reorder has been performed

      1. function callback(event, itemThatHasBeenMoved, itemsPreviousIndex, itemsNewIndex, reorderedArray) {
      2. // ...
      3. }
    2. The itemClicked function that is called when an item is clicked before any dragging begins

      1. function itemClicked(event, itemThatHasBeenClicked, itemsIndex) {
      2. // ...
      3. }

      Note: event will be the synthetic React event for either mouseup or touchend, and both contain clientX & clientY values (for ease of use)

Compatibility / Requirements

  • Version 2.x tested and working with React 0.14

  • Versions 1.x tested and working with React 0.12 - 0.13

  • requirejs / commonjs / browserify (Optional, but recommended*)

* Has only been tested with requirejs & browserify

Supported Browsers

Desktop

  • Internet Explorer 9+ (may support IE8**)

  • Google Chrome (tested in version 39.0.2171.95(64-bit))

  • Mozilla Firefox (tested in version 33.0)

  • Opera (tested in version 26.0.1656.60)

  • Safari (tested in version 7.1.2 (9537.85.11.5))

** Have not had a chance to test in IE8, but IE8 is supported by React

Mobile

  • Chrome (tested in version 40.0.2214.89)

  • Safari (tested on iOS 8)

Untested Browsers

  • Internet Explorer 8* (the lowest version that React supports)

*** If anyone could confirm that this works in IE8, that’d be awesome