-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
5-Virtualized.story.tsx
132 lines (122 loc) · 3.36 KB
/
5-Virtualized.story.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
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
import React, {useState} from 'react';
import {createPortal} from 'react-dom';
import VirtualList from 'react-tiny-virtual-list';
import {
closestCenter,
DndContext,
DragOverlay,
PointerSensor,
KeyboardSensor,
useSensor,
useSensors,
} from '@dnd-kit/core';
import {
arrayMove,
sortableKeyboardCoordinates,
SortableContext,
verticalListSortingStrategy,
} from '@dnd-kit/sortable';
import styles from './Virtualized.module.css';
import {createRange} from '../../utilities';
import {SortableItem, Props} from './Sortable';
import {Item, Wrapper} from '../../components';
export default {
title: 'Presets/Sortable/Virtualized',
};
function Sortable({
adjustScale = false,
strategy = verticalListSortingStrategy,
itemCount = 100,
handle = false,
getItemStyles = () => ({}),
modifiers,
}: Props) {
const [items, setItems] = useState(() =>
createRange<string>(itemCount, (index) => `${index + 1}`)
);
const [activeId, setActiveId] = useState<string | null>(null);
const sensors = useSensors(
useSensor(PointerSensor),
useSensor(KeyboardSensor, {
coordinateGetter: sortableKeyboardCoordinates,
})
);
const getIndex: (id: string) => number = items.indexOf.bind(items);
const activeIndex = activeId ? getIndex(activeId) : -1;
return (
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragStart={({active}) => {
setActiveId(active.id);
}}
onDragEnd={({over}) => {
if (over) {
const overIndex = getIndex(over.id);
if (activeIndex !== overIndex) {
setItems((items) => arrayMove(items, activeIndex, overIndex));
}
}
setActiveId(null);
}}
onDragCancel={() => setActiveId(null)}
modifiers={modifiers}
>
<Wrapper center>
<SortableContext items={items} strategy={strategy}>
<VirtualList
width={500}
height={600}
className={styles.VirtualList}
itemCount={items.length}
itemSize={64}
renderItem={({index, style}) => {
const id = items[index];
return (
<SortableItem
key={id}
id={id}
index={index}
handle={handle}
wrapperStyle={() => ({
...style,
opacity: id === activeId ? 0 : undefined,
padding: 5,
})}
style={getItemStyles}
/>
);
}}
/>
</SortableContext>
</Wrapper>
{createPortal(
<DragOverlay adjustScale={adjustScale}>
{activeId ? (
<Item
value={items[activeIndex]}
handle={handle}
style={getItemStyles({
id: activeId,
index: activeIndex,
isDragging: true,
isSorting: true,
overIndex: -1,
isDragOverlay: true,
})}
wrapperStyle={{
padding: 5,
}}
dragOverlay
/>
) : null}
</DragOverlay>,
document.body
)}
</DndContext>
);
}
const props = {
strategy: verticalListSortingStrategy,
};
export const BasicSetup = () => <Sortable {...props} />;