UNPKG

5.33 kBMarkdownView Raw
1# react-sortable [![build status](https://travis-ci.org/cheton/react-sortable.svg?branch=master)](https://travis-ci.org/cheton/react-sortable) [![Coverage Status](https://coveralls.io/repos/cheton/react-sortable/badge.svg)](https://coveralls.io/r/cheton/react-sortable)
2[![NPM](https://nodei.co/npm/react-sortablejs.png?downloads=true&stars=true)](https://nodei.co/npm/react-sortablejs/)
3
4A higher order React component for [Sortable](https://github.com/RubaXa/Sortable).
5
6## Installation
7The easiest way to use react-sortablejs is to install it from npm and include it in your React build process using webpack or browserify.
8```bash
9npm install --save react-sortablejs
10```
11
12You can create a standalone module using webpack:
13```bash
14$ npm install
15$ webpack
16```
17
18## Options
19
20#### `ref` option
21Specify which items inside the `ref` attribute should be sortable.
22
23#### `model` option
24The state attribute for creating a sortable list.
25
26
27See more options at https://github.com/RubaXa/Sortable#options
28```js
29{
30 ref: 'list',
31 model: 'items',
32 onStart: 'handleStart',
33 onEnd: 'handleEnd',
34 onAdd: 'handleAdd',
35 onUpdate: 'handleUpdate',
36 onRemove: 'handleRemove',
37 onSort: 'handleSort',
38 onFilter: 'handleFilter',
39 onMove: 'handleMove',
40 // Sortable options
41 group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
42 sort: true, // sorting inside list
43 delay: 0, // time in milliseconds to define when the sorting should start
44 disabled: false, // Disables the sortable if set to true.
45 store: null, // @see Store
46 animation: 150, // ms, animation speed moving items when sorting, `0` — without animation
47 handle: ".my-handle", // Drag handle selector within list items
48 filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function)
49 draggable: ".item", // Specifies which items inside the element should be sortable
50 ghostClass: "sortable-ghost", // Class name for the drop placeholder
51 chosenClass: "sortable-chosen", // Class name for the chosen item
52 dataIdAttr: 'data-id',
53 forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in
54 fallbackClass: "sortable-fallback" // Class name for the cloned DOM Element when using forceFallback
55 fallbackOnBody: false // Appends the cloned DOM Element into the Document's Body
56 scroll: true, // or HTMLElement
57 scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling.
58 scrollSpeed: 10, // px
59 setData: function (dataTransfer, dragEl) {
60 dataTransfer.setData('Text', dragEl.textContent);
61 }
62}
63```
64
65## Usage
66
67```js
68import React from 'react';
69import SortableMixin from 'react-sortablejs';
70
71const sortableOptions = {
72 ref: 'list',
73 model: 'items'
74};
75
76class MySortableComponent extends React.Component {
77 state = {
78 items: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
79 };
80
81 handleStart(evt) { // Dragging started
82 }
83 handleEnd(evt) { // Dragging ended
84 }
85 handleAdd(evt) { // Element is dropped into the list from another list
86 }
87 handleUpdate(evt) { // Changed sorting within list
88 }
89 handleRemove(evt) { // Element is removed from the list into another list
90 }
91 handleSort(evt) { // Called by any change to the list (add / update / remove)
92 }
93 handleFilter(evt) { // Attempt to drag a filtered element
94 }
95 handleMove(evt) { // Event when you move an item in the list or between lists
96 }
97 render() {
98 const items = this.state.items.map((text, index) => (
99 <li key={index}>{text}</li>
100 ));
101
102 return (
103 <div>
104 <ul ref="list">{items}</ul>
105 </div>
106 );
107 }
108}
109
110export default SortableMixin(MySortableComponent, sortableOptions);
111```
112
113## Examples
114
115Using the `group` option to drag elements from one list into another.
116
117File: index.jsx
118```js
119import React from 'react';
120import ReactDOM from 'react-dom';
121import Sortable1 from './sortable1';
122import Sortable2 from './sortable2';
123
124const SortableList = (props) => {
125 return (
126 <div>
127 <Sortable1 />
128 <hr />
129 <Sortable2 />
130 </div>
131 );
132};
133
134ReactDOM.render(<SortableList />, document.body);
135```
136
137File: sortable1.jsx
138
139```js
140import React from 'react';
141import SortableMixin from 'react-sortablejs';
142
143class Sortable1 extends React.Component {
144 state = {
145 items: [0, 1, 2, 3, 4]
146 };
147
148 render() {
149 let items = this.state.items.map((text, index) => {
150 return <li key={index}>{text}</li>;
151 });
152
153 return (
154 <div>
155 <ul ref="list">{items}</ul>
156 </div>
157 );
158 }
159}
160
161export default SortableMixin(Sortable1, { group: 'shared' });
162```
163
164File: sortable2.jsx
165
166```js
167import React from 'react';
168import SortableMixin from 'react-sortablejs';
169
170class Sortable2 extends React.Component {
171 state = {
172 items: [5, 6, 7, 8, 9]
173 };
174
175 render() {
176 let items = this.state.items.map((text, index) => {
177 return <li key={index}>{text}</li>;
178 });
179
180 return (
181 <div>
182 <ul ref="list">{items}</ul>
183 </div>
184 );
185 }
186}
187
188export default SortableMixin(Sortable2, { group: 'shared' });
189```