-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathTodoForm.purs
More file actions
65 lines (53 loc) · 1.46 KB
/
TodoForm.purs
File metadata and controls
65 lines (53 loc) · 1.46 KB
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
module Example.TodoForm where
import Prelude
import Effect (Effect)
import Data.Maybe (Maybe, maybe, isNothing)
import React as React
import React.SyntheticEvent as Event
import React.DOM as DOM
import React.DOM.Props as Props
import Unsafe.Coerce (unsafeCoerce)
import Example.Types (Todo(..), TodoStatus(..))
type TodoFormProps
= { todo :: Maybe Todo
, onEdit :: Todo -> Effect Unit
, onAdd :: Todo -> Effect Unit
}
todoFormClass :: React.ReactClass TodoFormProps
todoFormClass = React.component "TodoForm" component
where
component this =
pure { state: {}
, render: render <$> React.getProps this
}
where
render
{ todo
, onEdit
, onAdd
} =
DOM.form
[ Props.onSubmit onSubmit ]
[ DOM.input
[ Props._type "text"
, Props.value value
, Props.onChange onChange
]
, DOM.button
[ Props._type "submit"
, Props.disabled isDisabled
]
[ DOM.text "Add" ]
]
where
value = maybe "" (\(Todo { text }) -> text) todo
isDisabled = isNothing todo
onSubmit event = do
Event.preventDefault event
maybe (pure unit) onAdd todo
onChange event = onEdit $
maybe (Todo { text, status: TodoPending })
(\(Todo todo_) -> Todo todo_ { text = text })
todo
where
text = (unsafeCoerce event).target.value