Skip to content

Commit 9d93da3

Browse files
committed
Merge pull request #13 from andreypopp/master
Type DOM props
2 parents 2eb703d + 3e4c482 commit 9d93da3

File tree

4 files changed

+478
-217
lines changed

4 files changed

+478
-217
lines changed

example/app/app.purs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Main where
33
import Control.Monad.Eff
44
import Debug.Trace
55
import React
6-
import qualified React.DOM as DOM
6+
import React.DOM
77

88
foreign import interval
99
"function interval(ms) { \
@@ -13,21 +13,22 @@ foreign import interval
1313
\}"
1414
:: forall eff r. Number -> Eff (trace :: Trace) r -> Eff (eff) {}
1515

16-
helloInConsole = do
16+
helloInConsole e = do
1717
props <- getProps
1818
trace ("Hello, " ++ props.name ++ "!")
1919

2020
hello = mkUI spec do
2121
props <- getProps
22-
return $ DOM.h1 {
23-
className: "Hello",
24-
onClick: handle helloInConsole
25-
} [
26-
DOM.text "Hello, ",
27-
DOM.text props.name
22+
return $ h1 [
23+
className "Hello",
24+
onClick helloInConsole,
25+
style {background: "gray"}
26+
] [
27+
text "Hello, ",
28+
text props.name
2829
]
2930

30-
incrementCounter = do
31+
incrementCounter e = do
3132
val <- readState
3233
writeState (val + 1)
3334

@@ -40,14 +41,11 @@ counter = mkUI spec {
4041
print val
4142
} do
4243
val <- readState
43-
return $ DOM.p {
44-
className: "Counter",
45-
onClick: handle incrementCounter
46-
} [
47-
DOM.text (show val),
48-
DOM.text " Click me to increment!"
44+
return $ p [className "Counter", onClick incrementCounter] [
45+
text (show val),
46+
text " Click me to increment!"
4947
]
5048

5149
main = do
52-
let component = DOM.div {} [hello {name: "World"}, counter {}]
50+
let component = div' [hello {name: "World"}, counter {}]
5351
renderToBody component

example/tutorial/tutorial.purs

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ module Tutorial where
66
import Control.Monad.Eff
77
import Data.Array
88
import React
9+
import React.DOM
910
import Showdown
1011
import Debug.Trace
1112

12-
import qualified React.DOM as DOM
13-
1413
cBoxRender = do
1514
state <- readState
16-
pure $ DOM.div { className: "commentBox" }
17-
[ DOM.h1 {} [ DOM.text "Comments" ]
15+
pure $ div [ className "commentBox" ]
16+
[ h1' [ text "Comments" ]
1817
, commentList { data': state }
19-
, commentForm { onCommentSubmit: handle commentSubmit }
18+
, commentForm { onCommentSubmit: commentSubmit }
2019
]
2120

2221
commentBox = mkUI spec {
@@ -34,37 +33,33 @@ module Tutorial where
3433

3534
commentList = mkUI spec do
3635
props <- getProps
37-
pure $ DOM.div { className: "commentList" }
36+
pure $ div [ className "commentList" ]
3837
(commentNodes <$> props.data')
3938

4039
commentForm = mkUI spec do
4140
props <- getProps
42-
pure $ DOM.form { className: "commentForm"
43-
, onSubmit: handle submit
44-
}
45-
[ DOM.input { attrType: "text"
46-
, placeholder: "Your name"
47-
, ref: "author"
48-
}
49-
[]
50-
, DOM.input { attrType: "text"
51-
, placeholder: "Say something..."
52-
, ref: "text"
53-
}
54-
[]
55-
, DOM.input { attrType: "submit"
56-
, value: "Post"
57-
}
58-
[]
41+
pure $ form [ className "commentForm"
42+
, onSubmit submit
43+
]
44+
[ input [ typeProp "text"
45+
, placeholder "Your name"
46+
, ref "author"
47+
] []
48+
, input [ typeProp "text"
49+
, placeholder "Say something..."
50+
, ref "text"
51+
] []
52+
, input [ typeProp "submit"
53+
, value "Post"
54+
] []
5955
]
6056

6157
comment = mkUI spec do
6258
props <- getProps
63-
pure $ DOM.div { className: "comment" }
64-
[ DOM.h2 { className: "commentAuthor" }
65-
[ DOM.text props.author ]
66-
, DOM.span { dangerouslySetInnerHTML: { __html: makeHtml props.children } }
67-
[]
59+
pure $ div [ className "comment" ]
60+
[ h2 [ className "commentAuthor" ]
61+
[ text props.author ]
62+
, span [ dangerouslySetInnerHTML $ makeHtml props.children ] []
6863
]
6964

7065
commentNodes c = comment { author: c.author, children: c.text }
@@ -86,14 +81,17 @@ module Tutorial where
8681
\}" :: forall eff. Eff eff {}
8782

8883
foreign import submit
89-
"function submit() {\
90-
\ var author = this.refs.author.getDOMNode().value.trim();\
91-
\ var text = this.refs.text.getDOMNode().value.trim();\
92-
\ this.props.onCommentSubmit.call(this, {author: author, text: text});\
93-
\ this.refs.author.getDOMNode().value = '';\
94-
\ this.refs.text.getDOMNode().value = '';\
95-
\ return false;\
96-
\}" :: forall eff. Eff eff Boolean
84+
"function submit(e) {\
85+
\ e.preventDefault();\
86+
\ return function() { \
87+
\ var author = this.refs.author.getDOMNode().value.trim();\
88+
\ var text = this.refs.text.getDOMNode().value.trim();\
89+
\ this.props.onCommentSubmit.call(this, {author: author, text: text});\
90+
\ this.refs.author.getDOMNode().value = '';\
91+
\ this.refs.text.getDOMNode().value = '';\
92+
\ return false;\
93+
\ } \
94+
\}" :: Event -> forall eff. Eff eff Boolean
9795

9896
foreign import loadCommentsFromServer
9997
"function loadCommentsFromServer() {\

src/React.purs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ module React where
178178

179179
type Event = { }
180180
type MouseEvent = { pageX :: Number, pageY :: Number }
181+
type KeyboardEvent = { key :: String }
181182

182183
type EventHandlerContext eff props state result = Eff (
183184
p :: ReadPropsEff props,
@@ -187,32 +188,21 @@ module React where
187188
) result
188189

189190
foreign import handle
190-
" function handle(f) { \
191-
\ var component = __current; \
192-
\ return function(e) { \
193-
\ __current = component; \
194-
\ try { \
195-
\ var res = f.call(__current, e); \
196-
\ } finally { \
197-
\ __current = null; \
198-
\ } \
199-
\ return res; \
200-
\ } \
191+
" function handle(f) { \
192+
\ var component = __current; \
193+
\ return function(e) { \
194+
\ __current = component; \
195+
\ try { \
196+
\ var res = f.call(__current, e)(); \
197+
\ } finally { \
198+
\ __current = null; \
199+
\ } \
200+
\ return res; \
201+
\ } \
201202
\ }"
202-
:: forall eff props state result event.
203-
EventHandlerContext props state result eff -> EventHandler event
204-
205-
foreign import handleEvent
206-
"var handleEvent = handle"
207-
:: forall eff props state result.
208-
(Event -> EventHandlerContext eff props state result)
209-
-> EventHandler Event
210-
211-
foreign import handleMouseEvent
212-
"var handleMouseEvent = handle"
213-
:: forall eff props state result.
214-
(MouseEvent -> EventHandlerContext eff props state result)
215-
-> EventHandler MouseEvent
203+
:: forall eff ev props state result.
204+
(ev -> EventHandlerContext eff props state result)
205+
-> EventHandler ev
216206

217207
foreign import renderToString
218208
"var renderToString = React.renderComponentToString"

0 commit comments

Comments
 (0)