@@ -8,11 +8,16 @@ A higher order React component for [Sortable](https://github.com/RubaXa/Sortable
8
8
9
9
The sample code can be found in the [ examples] ( https://github.com/cheton/react-sortable/tree/master/examples ) directory.
10
10
11
+
12
+ ## Notice
13
+ There is a major breaking change since v1.0. Checkout [ Migration Guide] ( https://github.com/i18next/i18next-scanner/wiki/Migration-Guide ) while upgrading from earlier versions.
14
+
11
15
## Installation
12
16
13
17
### Webpack or Browserify
14
18
The easiest way to use react-sortablejs is to install it from npm and include it in your React build process using webpack or browserify.
15
19
``` bash
20
+ npm install --save react react-dom sortablejs # Install peerDependencies
16
21
npm install --save react-sortablejs
17
22
```
18
23
@@ -24,7 +29,7 @@ You can create a standalone ES5 module as shown below:
24
29
$ git clone https://github.com/cheton/react-sortable.git
25
30
$ cd react-sortable
26
31
$ npm install
27
- $ npm run dist
32
+ $ npm run build && npm run dist
28
33
```
29
34
30
35
Then, include these scripts into your html file:
@@ -38,201 +43,156 @@ Then, include these scripts into your html file:
38
43
</body >
39
44
```
40
45
41
- A simple example without using JSX syntax:
42
- ``` js
43
- var MySortable = React .createClass ({
44
- displayName: ' MySortable' ,
45
- getInitialState : function () {
46
- return { items: [' Apple' , ' Banana' , ' Cherry' ] };
47
- },
48
- render : function () {
49
- return (
50
- React .createElement (' ul' , { ref: " list" },
51
- this .state .items .map (function (item , key ) {
52
- return React .createElement (' li' , { key: key }, item);
53
- })
54
- )
55
- );
56
- }
57
- });
46
+ ## Usage
47
+ File: sortable-list.jsx
48
+ ``` jsx
49
+ import React from ' react' ;
50
+ import Sortable from ' react-sortable' ;
58
51
59
- ReactDOM . render (
60
- React . createElement ( SortableMixin . default ({ ref : ' list ' , model : ' items' })(MySortable), null ),
61
- document . getElementById ( ' container ' )
62
- );
63
- ```
52
+ // Functional Component
53
+ const SortableList = ({ items }) => {
54
+ let sortable = null ; // the sortable instance
55
+
56
+ items = items . map (( val , key ) => ( < li key = {key} data - id = {val} > List Item : {val} < / li > ));
64
57
65
- ## Options
58
+ return (
59
+ < Sortable
60
+ // Sortable options
61
+ options= {{
62
+ // See all options at https://github.com/RubaXa/Sortable#options
63
+ }}
64
+
65
+ // Use ref to get the sortable instance
66
+ ref= {(c ) => {
67
+ if (c) {
68
+ sortable = c .sortable ;
69
+ }
70
+ }}
71
+
72
+ // An optional tag to specify the wrapping element. Defaults to "div".
73
+ tag= " ul"
74
+
75
+ // The optional onChange method allows you to keep DOM nodes untouched
76
+ // and render the sorted items via state change.
77
+ // See an example at
78
+ // onChange={(order) => {
79
+ // this.setState({ items: order });
80
+ // }}
81
+ >
82
+ {items}
83
+ < / Sortable>
84
+ );
85
+ };
66
86
67
- #### ` ref ` option
68
- Specify which items inside the ` ref ` attribute should be sortable.
87
+ SortableList .propTypes = {
88
+ items: React .PropTypes .array
89
+ };
69
90
70
- #### ` model ` option
71
- The state attribute for creating a sortable list.
91
+ export default SortableList ;
92
+ ```
72
93
94
+ File: index.jsx
95
+ ``` jsx
96
+ import React from ' react' ;
97
+ import ReactDOM from ' react-dom' ;
98
+ import SortableList from ' ./sortable-list' ;
73
99
74
- See more options at https://github.com/RubaXa/Sortable#options
75
- ``` js
76
- {
77
- ref: ' list' ,
78
- model: ' items' ,
79
- onStart: ' handleStart' ,
80
- onEnd: ' handleEnd' ,
81
- onAdd: ' handleAdd' ,
82
- onUpdate: ' handleUpdate' ,
83
- onRemove: ' handleRemove' ,
84
- onSort: ' handleSort' ,
85
- onFilter: ' handleFilter' ,
86
- onMove: ' handleMove' ,
87
- // Sortable options
88
- group: " name" , // or { name: "...", pull: [true, false, clone], put: [true, false, array] }
89
- sort: true , // sorting inside list
90
- delay: 0 , // time in milliseconds to define when the sorting should start
91
- disabled: false , // Disables the sortable if set to true.
92
- store: null , // @see Store
93
- animation: 150 , // ms, animation speed moving items when sorting, `0` — without animation
94
- handle: " .my-handle" , // Drag handle selector within list items
95
- filter: " .ignore-elements" , // Selectors that do not lead to dragging (String or Function)
96
- draggable: " .item" , // Specifies which items inside the element should be sortable
97
- ghostClass: " sortable-ghost" , // Class name for the drop placeholder
98
- chosenClass: " sortable-chosen" , // Class name for the chosen item
99
- dataIdAttr: ' data-id' ,
100
- forceFallback: false , // ignore the HTML5 DnD behaviour and force the fallback to kick in
101
- fallbackClass: " sortable-fallback" // Class name for the cloned DOM Element when using forceFallback
102
- fallbackOnBody: false // Appends the cloned DOM Element into the Document's Body
103
- scroll: true , // or HTMLElement
104
- scrollSensitivity: 30 , // px, how near the mouse must be to an edge to start scrolling.
105
- scrollSpeed: 10 , // px
106
- setData : function (dataTransfer , dragEl ) {
107
- dataTransfer .setData (' Text' , dragEl .textContent );
108
- }
109
- }
100
+ ReactDOM .render (
101
+ < SortableList items= {[1 , 2 , 3 , 4 , 5 , 6 ]} / > ,
102
+ document .getElementById (' container' )
103
+ );
110
104
```
111
105
112
- ## Usage
106
+ ## Examples
113
107
108
+ ### Uncontrolled Component
114
109
``` js
115
110
import React from ' react' ;
116
111
import Sortable from ' react-sortablejs' ;
117
112
118
- class MySortableList extends React .Component {
119
- static propTypes = {
120
- sortableInstance: React .PropTypes .object ,
121
- items: React .PropTypes .array
122
- };
123
- static defaultProps = {
124
- sortableInstance: null
125
- items: []
126
- };
113
+ class App extends React .Component {
127
114
state = {
128
- items: this . props . items
115
+ items: [ ' Apple ' , ' Banana ' , ' Cherry ' , ' Guava ' , ' Peach ' , ' Strawberry ' ]
129
116
};
130
117
131
- componentDidUpdate () {
132
- // Note: The sortableInstance is null for the initial render
133
- const { sortableInstance } = this .props ;
134
-
135
- // You can see all the methods at https://github.com/RubaXa/Sortable#method
136
- console .log (sortableInstance .toArray ());
137
- }
138
- handleStart (evt ) { // Dragging started
139
- }
140
- handleEnd (evt ) { // Dragging ended
141
- }
142
- handleAdd (evt ) { // Element is dropped into the list from another list
143
- }
144
- handleUpdate (evt ) { // Changed sorting within list
145
- }
146
- handleRemove (evt ) { // Element is removed from the list into another list
147
- }
148
- handleSort (evt ) { // Called by any change to the list (add / update / remove)
149
- }
150
- handleFilter (evt ) { // Attempt to drag a filtered element
151
- }
152
- handleMove (evt ) { // Event when you move an item in the list or between lists
118
+ sortable = null ;
119
+
120
+ handleReverseOrder () {
121
+ const order = this .sortable .toArray ();
122
+ this .sortable .sort (order .reverse ());
153
123
}
154
124
render () {
155
- const items = this .state .items .map ((val , index ) => (
156
- < li key= {index}> {val}< / li>
157
- ));
158
-
159
- return (
125
+ const items = this .state .items .map ((val , key ) => (< li key= {key} data- id= {val}> {val}< / li> ));
126
+
127
+ retrun (
160
128
< div>
161
- < ul ref= " list" > {items}< / ul>
129
+ < button type= " button" onClick= {:: this .handleReverseOrder }> Reverse Order< / button>
130
+ < Sortable
131
+ // Check out options at https://github.com/RubaXa/Sortable#options
132
+ options= {{
133
+ handle: " .my-handle" , // Drag handle selector within list items
134
+ draggable: " .item" // Specifies which items inside the element should be sortable
135
+ }}
136
+ ref= {(c ) => {
137
+ if (c) {
138
+ this .sortable = sortable;
139
+ }
140
+ }}
141
+ tag= " ul" // Defaults to "div"
142
+ >
143
+ {items}
144
+ < / Sortable>
162
145
< / div>
163
146
);
164
147
}
165
148
}
166
-
167
- const sortableOptions = {
168
- ref: ' list' ,
169
- model: ' items'
170
- };
171
- export default Sortable (sortableOptions)(MySortableList);
172
- ```
173
-
174
- You can also use HOCs as ES7 decorators:
175
- ``` js
176
- import React from ' react' ;
177
- import Sortable from ' react-sortablejs' ;
178
-
179
- @Sortable ({ ref: ' list' , model: ' items' })
180
- export default class SortableList extends React .Component {
181
- ...
182
- }
183
149
```
184
150
185
- ## Examples
186
-
187
- ### Simple List
188
-
189
- File: index.jsx
190
- ``` js
191
- import React from ' react' ;
192
- import ReactDOM from ' react-dom' ;
193
- import SimpleList from ' ./simple-list' ;
151
+ ### Controlled Component
194
152
195
- ReactDOM .render (
196
- < SimpleList items= {[1 , 2 , 3 , 4 , 5 , 6 ]} / > ,
197
- document .getElementById (' container' )
198
- );
199
- ```
200
-
201
- File: simple-list.jsx
202
153
``` js
203
154
import React from ' react' ;
204
155
import Sortable from ' react-sortablejs' ;
205
156
206
- const sortableOptions = {
207
- ref: ' list' ,
208
- model: ' items'
209
- };
210
-
211
- class SimpleList extends React .Component {
212
- static propTypes = {
213
- items: React .PropTypes .array
214
- };
215
- static defaultProps = {
216
- items: []
217
- };
157
+ class App extends React .Component {
218
158
state = {
219
- items: this . props . items
159
+ items: [ ' Apple ' , ' Banana ' , ' Cherry ' , ' Guava ' , ' Peach ' , ' Strawberry ' ]
220
160
};
221
161
162
+ sortable = null ;
163
+
164
+ handleReverseOrder () {
165
+ const order = this .sortable .toArray ();
166
+ this .sortable .sort (order .reverse ());
167
+ }
222
168
render () {
223
- const items = this .state .items .map ((val , index ) => (
224
- < li key= {index}> {val}< / li>
225
- ));
169
+ const items = this .state .items .map ((val , key ) => (< li key= {key} data- id= {val}> {val}< / li> ));
226
170
227
- return (
171
+ retrun (
228
172
< div>
229
- < ul ref= " list" > {items}< / ul>
173
+ < button type= " button" onClick= {:: this .handleReverseOrder }> Reverse Order< / button>
174
+ < Sortable
175
+ // Check out options at https://github.com/RubaXa/Sortable#options
176
+ options= {{
177
+ handle: " .my-handle" , // Drag handle selector within list items
178
+ draggable: " .item" // Specifies which items inside the element should be sortable
179
+ }}
180
+ ref= {(c ) => {
181
+ if (c) {
182
+ this .sortable = sortable;
183
+ }
184
+ }}
185
+ tag= " ul" // Defaults to "div"
186
+ onChange= {(order, sortable) { // [Optional] Controlled Component
187
+ this .setState ({ items: order });
188
+ }}
189
+ >
190
+ {items}
191
+ < / Sortable>
230
192
< / div>
231
193
);
232
194
}
233
195
}
234
-
235
- export default Sortable (sortableOptions)(SimpleList);
236
196
```
237
197
238
198
### Shared Group
@@ -265,35 +225,20 @@ File: shared-group.jsx
265
225
import React from ' react' ;
266
226
import Sortable from ' react-sortablejs' ;
267
227
268
- const sortableOptions = {
269
- ref: ' list' ,
270
- model: ' items' ,
271
- group: ' shared'
272
- };
273
-
274
- class SharedGroup extends React .Component {
275
- static propTypes = {
276
- items: React .PropTypes .array
277
- };
278
- static defaultProps = {
279
- items: []
280
- };
281
- state = {
282
- items: this .props .items
283
- };
228
+ const SharedGroup = ({ items }) => {
229
+ item = items .map (item => < li> {item}< / li> );
284
230
285
- render () {
286
- const items = this .state .items .map ((text , index ) => (
287
- < li key= {index}> {text}< / li>
288
- ));
289
-
290
- return (
291
- < div>
292
- < ul ref= " list" > {items}< / ul>
293
- < / div>
294
- );
295
- }
296
- }
231
+ return (
232
+ < Sortable
233
+ options= {{
234
+ group: ' shared'
235
+ }}
236
+ tag= " ul"
237
+ >
238
+ {items}
239
+ < / Sortable>
240
+ );
241
+ };
297
242
298
- export default Sortable (sortableOptions)( SharedGroup) ;
243
+ export default SharedGroup ;
299
244
```
0 commit comments