Skip to content

Commit 3050d3c

Browse files
committed
Adding createElement
The createElement function takes a component factory and props and creates a new component that can be nested in other components.
1 parent ca42942 commit 3050d3c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/React.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,11 @@ exports.renderToElementById = function(id) {
7979
}
8080
}
8181
};
82+
83+
exports.createElement = function(factory) {
84+
return function(props) {
85+
return function(children){
86+
return React.createElement.apply(React, [factory, props].concat(children));
87+
};
88+
};
89+
};

src/React.purs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module React
2222
, Render()
2323

2424
, UISpec()
25+
, UIFactory()
2526

2627
, Event()
2728
, MouseEvent()
@@ -45,6 +46,7 @@ module React
4546
, renderToString
4647
, renderToBody
4748
, renderToElementById
49+
, createElement
4850
) where
4951

5052
import Prelude
@@ -202,6 +204,9 @@ type UISpec props state eff =
202204
) Unit
203205
}
204206

207+
-- | Factory function for components.
208+
type UIFactory props = props -> UI
209+
205210
-- | Create a component specification.
206211
spec :: forall props state eff. state -> Render props state eff -> UISpec props state eff
207212
spec st render =
@@ -250,8 +255,7 @@ transformState ctx f = do
250255
-- | Create a component from a component spec.
251256
foreign import mkUI :: forall props state eff.
252257
UISpec props state eff ->
253-
props ->
254-
UI
258+
UIFactory props
255259

256260
-- | Create an event handler.
257261
foreign import handle :: forall eff ev props state result.
@@ -266,3 +270,6 @@ foreign import renderToBody :: forall eff. UI -> Eff (dom :: DOM | eff) UI
266270

267271
-- | Render a component to the element with the specified ID.
268272
foreign import renderToElementById :: forall eff. String -> UI -> Eff (dom :: DOM | eff) UI
273+
274+
-- | Create an element from a component factory.
275+
foreign import createElement :: forall props. UIFactory props -> props -> Array UI -> UI

0 commit comments

Comments
 (0)