Skip to content

Create a stateless class with children #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/React.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,14 @@ createClassStateless :: forall props. (props -> ReactElement) -> ReactClass prop

Create a stateless React class.

#### `createClassStateless'`

``` purescript
createClassStateless' :: forall props. (props -> Array ReactElement -> ReactElement) -> ReactClass props
```

Create a stateless React class with children access.

#### `handle`

``` purescript
Expand Down
17 changes: 12 additions & 5 deletions src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ function getRefs(this_) {
}
exports.getRefs = getRefs;

function childrenToArray(children) {
var result = [];

React.Children.forEach(children, function(child){
result.push(child);
});

return result;
}
exports.childrenToArray = childrenToArray;

function getChildren(this_) {
return function(){
var children = this_.props.children;

var result = [];

React.Children.forEach(children, function(child){
result.push(child);
});
var result = childrenToArray(children);

return result;
};
Expand Down
11 changes: 11 additions & 0 deletions src/React.purs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module React

, createClass
, createClassStateless
, createClassStateless'
, createElement
, createElementDynamic
, createElementTagName
Expand Down Expand Up @@ -296,6 +297,10 @@ foreign import createClass :: forall props state eff. ReactSpec props state eff
createClassStateless :: forall props. (props -> ReactElement) -> ReactClass props
createClassStateless = unsafeCoerce

-- | Create a stateless React class with children access.
createClassStateless' :: forall props. (props -> Array ReactElement -> ReactElement) -> ReactClass props
createClassStateless' k = createClassStateless \props -> k props (childrenToArray (unsafeCoerce props).children)

-- | Create an event handler.
foreign import handle :: forall eff ev props state result. (ev -> EventHandlerContext eff props state result) -> EventHandler ev

Expand All @@ -313,3 +318,9 @@ foreign import createElementTagNameDynamic :: forall props. TagName -> props ->

-- | Create a factory from a React class.
foreign import createFactory :: forall props. ReactClass props -> props -> ReactElement

-- | Internal representation for the children elements passed to a component
foreign import data Children :: *

-- | Internal conversion function from children elements to an array of React elements
foreign import childrenToArray :: Children -> Array React.ReactElement