forked from PersianArt/react-sortable-hoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (58 loc) · 1.62 KB
/
index.js
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
import 'babel-polyfill';
import React, {Component} from 'react';
import {render} from 'react-dom';
import {SortableContainer, SortableElement, arrayMove} from './src/index';
import range from 'lodash/range';
import random from 'lodash/random';
const SortableItem = SortableElement(({height, value}) => (
<div style={{
position: 'relative',
width: '100%',
display: 'block',
padding: 20,
backgroundColor: '#FFF',
borderBottom: '1px solid #EFEFEF',
boxSizing: 'border-box',
WebkitUserSelect: 'none',
height: height
}}>
Item {value}
</div>
));
const SortableList = SortableContainer(({items}) => (
<div style={{
width: '80%',
height: '80vh',
maxWidth: '500px',
margin: '0 auto',
overflow: 'auto',
backgroundColor: '#f3f3f3',
border: '1px solid #EFEFEF',
borderRadius: 3
}}>
{items.map(({height, value}, index) => <SortableItem key={`item-${index}`} index={index} value={value} height={height}/>)}
</div>
));
class Example extends Component {
state = {
items: range(100).map((value) => {
return {
value,
height: random(49, 120)
};
})
};
onSortEnd = ({oldIndex, newIndex}) => {
let {items} = this.state;
this.setState({
items: arrayMove(items, oldIndex, newIndex)
});
};
render() {
const {items} = this.state;
return <SortableList items={items} onSortEnd={this.onSortEnd} />;
}
}
render(<Example />,
document.getElementById('root')
)