forked from atlassian/react-beautiful-dnd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.jsx
103 lines (94 loc) · 2.55 KB
/
list.jsx
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
// @flow
import React, { useState } from 'react';
import { FixedSizeList as List, areEqual } from 'react-window';
import type { Quote } from '../../types';
import {
Droppable,
Draggable,
DragDropContext,
type DroppableProvided,
type DraggableProvided,
type DraggableStateSnapshot,
type DraggableRubric,
type DropResult,
} from '../../../../src';
import QuoteItem from '../../primatives/quote-item';
import reorder from '../../reorder';
type Props = {|
initial: Quote[],
|};
type RowProps = {
data: Quote[],
index: number,
style: Object,
};
const Row = React.memo(({ data: quotes, index, style }: RowProps) => {
const quote: Quote = quotes[index];
return (
<Draggable draggableId={quote.id} index={index} key={quote.id}>
{(provided: DraggableProvided, snapshot: DraggableStateSnapshot) => (
<QuoteItem
provided={provided}
quote={quote}
isDragging={snapshot.isDragging}
isGroupedOver={Boolean(snapshot.combineTargetFor)}
style={{ margin: 0, ...style }}
index={index}
/>
)}
</Draggable>
);
}, areEqual);
function App(props: Props) {
const [quotes, setQuotes] = useState(() => props.initial);
function onDragEnd(result: DropResult) {
if (!result.destination) {
return;
}
if (result.source.index === result.destination.index) {
return;
}
const newQuotes: Quote[] = reorder(
quotes,
result.source.index,
result.destination.index,
);
setQuotes(newQuotes);
}
return (
<DragDropContext onDragEnd={onDragEnd}>
<Droppable
droppableId="droppable"
mode="virtual"
renderClone={(
provided: DraggableProvided,
snapshot: DraggableStateSnapshot,
rubric: DraggableRubric,
) => (
<QuoteItem
provided={provided}
isDragging={snapshot.isDragging}
quote={quotes[rubric.source.index]}
style={{ margin: 0 }}
index={rubric.source.index}
/>
)}
>
{(droppableProvided: DroppableProvided) => (
<List
height={500}
itemCount={quotes.length}
itemSize={100}
width={300}
// you will want to use List.outerRef rather than List.innerRef as it has the correct height when the list is unpopulated
outerRef={droppableProvided.innerRef}
itemData={quotes}
>
{Row}
</List>
)}
</Droppable>
</DragDropContext>
);
}
export default App;