@@ -5,21 +5,32 @@ import Prelude
55import Data.Array ((!!), drop , mapWithIndex , take )
66import Data.Foldable (for_ )
77import Data.Maybe (Maybe (Nothing), fromMaybe , maybe )
8- import React.Basic as React
8+ import React.Basic ( Component , JSX , StateUpdate (..), createComponent , fragment , make )
99import React.Basic.DOM as R
1010import React.Basic.DOM.Events (targetChecked )
1111import React.Basic.Events as Events
12- import React.Basic.ReactDND (DragDrop , DragDropContextProps , DragDropItemType (..), createDragDrop , createDragDropContext )
12+ import React.Basic.ReactDND (DragDrop , DragDropItemType (..), createDragDrop , createDragDropContext )
1313import React.Basic.ReactDND.Backends.HTML5Backend (html5Backend )
1414
15- dndContext :: React.Component DragDropContextProps
15+ dndContext :: JSX -> JSX
1616dndContext = createDragDropContext html5Backend
1717
1818dnd :: DragDrop { itemId :: String , index :: Int }
1919dnd = createDragDrop (DragDropItemType " TODO_ITEM" )
2020
21- component :: React.Component { }
22- component = React .component { displayName: " BasicExample" , initialState, receiveProps, render }
21+ data Action
22+ = Move { from :: Int , to :: Int }
23+ | SetDone String Boolean
24+
25+ component :: Component
26+ component = createComponent " TodoExample"
27+
28+ todoExample :: JSX
29+ todoExample = unit # make component
30+ { initialState = initialState
31+ , update = update
32+ , render = render
33+ }
2334 where
2435 initialState =
2536 { todos:
@@ -29,22 +40,29 @@ component = React.component { displayName: "BasicExample", initialState, receive
2940 ]
3041 }
3142
32- receiveProps _ =
33- pure unit
43+ update self = case _ of
44+ Move { from, to } ->
45+ Update self.state { todos = moveItem from to self.state.todos }
3446
35- render { state, setState } =
36- React .element dndContext
37- { child:
38- React .fragment
39- [ R .h1_ [ R .text " Todos" ]
40- , R .p_ [ R .text " Drag to reorder the list:" ]
41- , R .section_ (mapWithIndex renderTodo state.todos)
42- ]
43- }
47+ SetDone id done ->
48+ Update self.state
49+ { todos = self.state.todos <#> \t ->
50+ if t.id == id
51+ then t { done = done }
52+ else t
53+ }
54+
55+ render { state, send } =
56+ dndContext $
57+ fragment
58+ [ R .h1_ [ R .text " Todos" ]
59+ , R .p_ [ R .text " Drag to reorder the list:" ]
60+ , R .section_ (mapWithIndex renderTodo state.todos)
61+ ]
4462
4563 where
4664 renderTodo index todo =
47- React .element dnd.dragSource
65+ dnd.dragSource
4866 { beginDrag: \_ -> pure
4967 { itemId: todo.id
5068 , index
@@ -54,12 +72,10 @@ component = React.component { displayName: "BasicExample", initialState, receive
5472 , isDragging: \{ item: draggingItem } ->
5573 pure $ maybe false (\i -> i.itemId == todo.id) draggingItem
5674 , render: \{ connectDragSource, isDragging } ->
57- React .element dnd.dropTarget
75+ dnd.dropTarget
5876 { drop: \{ item: dragItem } -> do
5977 for_ (_.index <$> dragItem) \dragItemIndex ->
60- setState \s -> s
61- { todos = moveItem dragItemIndex index s.todos
62- }
78+ send $ Move { from: dragItemIndex, to: index }
6379 pure Nothing
6480 , hover: const (pure unit)
6581 , canDrop: const (pure true )
@@ -84,12 +100,7 @@ component = React.component { displayName: "BasicExample", initialState, receive
84100 { " type" : " checkbox"
85101 , checked: todo.done
86102 , onChange: Events .handler targetChecked \checked -> do
87- setState \s -> s
88- { todos = s.todos <#> \t ->
89- if t.id == todo.id
90- then t { done = fromMaybe false checked }
91- else t
92- }
103+ send $ SetDone todo.id $ fromMaybe false checked
93104 }
94105 , R .text todo.text
95106 ]
0 commit comments