-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathDraggableListExamples.tsx
74 lines (60 loc) · 2.08 KB
/
DraggableListExamples.tsx
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
import React, { Component } from 'react';
import uniqueId from 'lodash/uniqueId';
import Checkbox from '../../src/components/checkbox/Checkbox';
import DraggableList from '../../src/components/draggable-list';
import DraggableListItem from '../../src/components/draggable-list/DraggableListItem';
import reorder, { ReorderListItem } from '../../src/components/draggable-list/draggable-list-utils/reorder';
import '../styles/DraggableListExamples.scss';
interface Props {
isDraggableViaHandle?: boolean;
}
interface State {
items: Array<ReorderListItem>;
listId: string;
}
class DraggableListExamples extends Component<Props, State> {
state = {
items: [],
listId: '',
};
componentDidMount() {
this.setState({
items: this.getItems(10),
listId: uniqueId(),
});
}
getItems = (count: number): Array<ReorderListItem> => {
return Array.from({ length: count }, (v, k) => k).map(k => ({
id: uniqueId('item_'),
label: `item ${k}`,
}));
};
onDragEnd = (sourceIndex: number, destinationIndex: number) => {
if (!destinationIndex) {
return;
}
const items = reorder(this.state.items, sourceIndex, destinationIndex);
this.setState({
items,
});
};
render() {
const { isDraggableViaHandle } = this.props;
const { items, listId } = this.state;
return (
<DraggableList className="draggable-list-example-container" listId={listId} onDragEnd={this.onDragEnd}>
{items.map((item: ReorderListItem, index) => (
<DraggableListItem
key={`draggable-${index}`}
id={item.id}
index={index}
isDraggableViaHandle={isDraggableViaHandle}
>
<Checkbox label={item.label} name={item.label} />
</DraggableListItem>
))}
</DraggableList>
);
}
}
export default DraggableListExamples;