-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.jsx
112 lines (103 loc) · 2.98 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
104
105
106
107
108
109
110
111
112
// @flow
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import 'react-virtualized/styles.css';
import { List } from 'react-virtualized';
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 = {
index: number,
style: Object,
};
// Using a higher order function so that we can look up the quotes data to retrieve
// our quote from within the rowRender function
const getRowRender = (quotes: Quote[]) => ({ 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}
style={{ margin: 0, ...style }}
index={index}
/>
)}
</Draggable>
);
};
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}
rowCount={quotes.length}
rowHeight={110}
width={300}
ref={(ref) => {
// react-virtualized has no way to get the list's ref that I can so
// So we use the `ReactDOM.findDOMNode(ref)` escape hatch to get the ref
if (ref) {
// eslint-disable-next-line react/no-find-dom-node
const whatHasMyLifeComeTo = ReactDOM.findDOMNode(ref);
if (whatHasMyLifeComeTo instanceof HTMLElement) {
droppableProvided.innerRef(whatHasMyLifeComeTo);
}
}
}}
rowRenderer={getRowRender(quotes)}
/>
)}
</Droppable>
</DragDropContext>
);
}
export default App;