forked from shakacode/rescript-dnd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGettingStartedGuide.res
48 lines (39 loc) · 1.25 KB
/
GettingStartedGuide.res
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
type item = int
module Item = {
type t = item
let eq: (item, item) => bool = (x1, x2) => x1 == x2
let cmp: (item, item) => int = Pervasives.compare
}
module Container = Dnd.MakeSingletonContainer()
module Items = Dnd.Make(Item, Container)
type state = array<item>
type action = ReorderItems(Dnd.result<Item.t, Container.t>)
let reducer = (state, action) =>
switch action {
| ReorderItems(Some(SameContainer(item, placement))) =>
state->ArrayExt.reinsert(
~value=item,
~place=switch placement {
| Before(id) => #Before(id)
| Last => #Last
},
)
| ReorderItems(Some(NewContainer(_)))
| ReorderItems(None) => state
}
let initialState = [1, 2, 3, 4, 5, 6, 7]
@react.component
let make = () => {
let (state, dispatch) = reducer->React.useReducer(initialState)
<Items.DndManager onReorder={result => ReorderItems(result)->dispatch}>
<Items.DroppableContainer id={Container.id()} axis=Y>
{state
->Array.mapWithIndex((index, item) =>
<Items.DraggableItem id=item key={item->Int.toString} containerId={Container.id()} index>
#Children(item->Int.toString->React.string)
</Items.DraggableItem>
)
->React.array}
</Items.DroppableContainer>
</Items.DndManager>
}