-
Notifications
You must be signed in to change notification settings - Fork 211
/
README.md
307 lines (224 loc) · 7.73 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# `react-sortablejs`
React bindings to [SortableJS](https://github.com/SortableJS/Sortable)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
Please note that this is not considered ready for production, as there are still a number of bugs being sent through.
## Features
## Installation
`sortablejs` and `@types/sortablejs` are peer dependencies. The latter is only used if intellisense/typescript is desired.
```shell
npm install --save react-sortablejs sortablejs
npm install --save-dev @types/sortablejs
# OR
yarn add react-sortablejs sortablejs
yarn add -D @types/sortablejs
```
## Learn
Here is the TLDR of what sortable is:
```md
- Shopping List: # list of items / sortable. This represents `react-sortablejs`
- eggs # list item. These are all the items in the list and are what you move around.
- bread # list item
- milk # list item
```
## Usage/Examples
### Function Component
```tsx
import React, { FC, useState } from "react";
import { ReactSortable } from "react-sortablejs";
interface ItemType {
id: number;
name: string;
}
export const BasicFunction: FC = (props) => {
const [state, setState] = useState<ItemType[]>([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable list={state} setList={setState}>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
```
### Class Component
```tsx
import React, { Component } from "react";
import { ReactSortable } from "react-sortablejs";
interface BasicClassState {
list: { id: string; name: string }[];
}
export class BasicClass extends Component<{}, BasicClassState> {
state: BasicClassState = {
list: [{ id: "1", name: "shrek" }],
};
render() {
return (
<ReactSortable
list={this.state.list}
setList={(newState) => this.setState({ list: newState })}
>
{this.state.list.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
}
}
```
## Plugins
Sortable has some pretty cool plugins such as MultiDrag and Swap.
By Default:
- AutoScroll is premounted and enabled.
- OnSpill is premounted and NOT enabled.
- MultiDrag and Swap and NOT premounted and NOT enabled
You must mount the plugin with sortable **ONCE ONLY**.
```tsx
import React from "react";
import { ReactSortable, Sortable, MultiDrag, Swap } from "react-sortablejs";
// mount whatever plugins you'd like to. These are the only current options.
Sortable.mount(new MultiDrag(), new Swap());
const App = () => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable
multiDrag // enables mutidrag
// OR
swap // enables swap
>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
```
## Sortable API
For a comprehensive list of options, please visit https://github.com/SortableJS/Sortable#options.
Those options are applied as follows.
```tsx
Sortable.create(element, {
group: " groupName",
animation: 200,
delayOnTouchStart: true,
delay: 2,
});
// --------------------------
// Will now be...
// --------------------------
import React from "react";
import { ReactSortable } from "react-sortablejs";
const App = () => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable
// here they are!
group="groupName"
animation={200}
delayOnTouchStart={true}
delay={2}
>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
```
## React API
### id, className, style
These are all default DOM attributes. Nothing special here.
### list
The same as `state` in `const [ state, setState] = useState([{ id: 1}, {id: 2}])`
`state` must be an array of items, with each item being an object that has the following shape:
```ts
/** The unique id associated with your item. It's recommended this is the same as the key prop for your list item. */
id: string | number;
/** When true, the item is selected using MultiDrag */
selected?: boolean;
/** When true, the item is deemed "chosen", which basically just a mousedown event. */
chosen?: boolean;
/** When true, it will not be possible to pick this item up in the list. */
filtered?: boolean;
[property: string]: any;
```
### setList
The same as `setState` in `const [ state, setState] = useState([{ id: 1}, {id: 2}])`
### clone
If you're using `{group: { name: 'groupName', pull: 'clone'}}`, this means you're in 'clone' mode. You should provide a function for this.
Check out the source code of the clone example for more information. I'll write it here soon.
### tag
ReactSortable is a `div` element by default. This can be changed to be any HTML element (for example `ul`, `ol`)
or can be a React component.
This value, be it the component or the HTML element, should be passed down under `props.tag`.
Let's explore both here.
#### HTML Element
Here we will use an `ul`. You can use any HTML.
Just add the string and ReactSortable will use a `ul` instead of a `div`.
```tsx
import React, { FC, useState } from "react";
import { ReactSortable } from "react-sortablejs";
export const BasicFunction: FC = (props) => {
const [state, setState] = useState([{ id: "1", name: "shrek" }]);
return (
<ReactSortable tag="ul" list={state} setList={setState}>
{state.map((item) => (
<li key={item.id}>{item.name}</li>
))}
</ReactSortable>
);
};
```
#### Custom Component
When using a custom component in the `tag` prop, the only component it allows is a `forwardRef` component.
Currently, we only support components that use the `React.forwardRef` API.
If it doesn't have one, you can add one using `React.forwardRef()`.
> todo: Some third-party UI components may have nested elements to create the look they're after.
> This could be an issue and not sure how to fix it.
```tsx
import React, { FC, useState, forwardRef } from "react";
import { ReactSortable } from "react-sortablejs";
// This is just like a normal component, but now has a ref.
const CustomComponent = forwardRef<HTMLDivElement, any>((props, ref) => {
return <div ref={ref}>{props.children}</div>;
});
export const BasicFunction: FC = (props) => {
const [state, setState] = useState([
{ id: 1, name: "shrek" },
{ id: 2, name: "fiona" },
]);
return (
<ReactSortable tag={CustomComponent} list={state} setList={setState}>
{state.map((item) => (
<div key={item.id}>{item.name}</div>
))}
</ReactSortable>
);
};
```
## How does it work?
Sortable affects the DOM, adding, and removing nodes/css when it needs to in order to achieve the smooth transitions we all know an love.
This component reverses many of its actions of the DOM so React can handle this when the state changes.
## Caveats / Gotchas
### `key !== index`
DO NOT use the index as a key for your list items. Sorting will not work.
In all the examples above, I used an object with an ID. You should do the same!
I may even enforce this into the design to eliminate errors.
### Nesting
#### Problem
Basically, the child updates the state twice. I'm working on this.
#### What does work?
Our usage indicates that as long as we only move items between lists that don't use the same `setState` function.
I hope to provide an example soon.
#### Solutions
We don't have anything that works 100%, but here I'd like to spitball some potential avenues to look down.
- Use `onMove` to handle state changes instead of `onAdd`,`onRemove`, etc.
- Create a Sortable plugin specifically for react-sortbalejs