Skip to content

Commit 65d253e

Browse files
committed
Updates for react 0.14
Resolves #50
1 parent 12f9412 commit 65d253e

File tree

5 files changed

+110
-121
lines changed

5 files changed

+110
-121
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
output
22
bower_components
33
node_modules
4+
.pulp-cache/

bower.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "purescript-react",
3-
"version": "0.0.5",
43
"homepage": "https://github.com/purescript-contrib/purescript-react",
54
"description": "Purescript bindings for React.js",
65
"keywords": [
@@ -15,15 +14,15 @@
1514
"test",
1615
"tests"
1716
],
18-
"repository": {
19-
"type": "git",
17+
"repository": {
18+
"type": "git",
2019
"url": "git://github.com/purescript-contrib/purescript-react.git"
21-
},
22-
"dependencies": {
23-
"purescript-dom": "~0.2.6"
2420
},
2521
"devDependencies": {
26-
"purescript-console": "~0.1.1",
27-
"react": "~0.13.3"
22+
"purescript-console": "~0.1.1"
23+
},
24+
"dependencies": {
25+
"purescript-eff": "~0.1.2",
26+
"purescript-unsafe-coerce": "~0.1.0"
2827
}
2928
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "purescript-react",
3+
"files": [],
4+
"peerDependencies": {
5+
"react": "^0.14.3"
6+
}
7+
}

src/React.js

Lines changed: 85 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33

44
// module React
55

6-
exports.getProps = function(ctx) {
7-
return function() {
8-
return ctx.props;
9-
};
10-
};
6+
var React = require('react');
117

12-
exports.getRefs = function(ctx) {
13-
return function() {
14-
return ctx.refs;
15-
};
16-
};
8+
function getProps(this_) {
9+
return function(){
10+
return this_.props;
11+
};
12+
}
13+
exports.getProps = getProps;
14+
15+
function getRefs(this_) {
16+
return function(){
17+
return this_.refs;
18+
};
19+
}
20+
exports.getRefs = getRefs;
1721

18-
exports.getChildren = function(ctx) {
19-
return function() {
20-
var children = ctx.props.children;
22+
function getChildren(this_) {
23+
return function(){
24+
var children = this_.props.children;
2125

2226
var result = [];
2327

@@ -27,103 +31,83 @@ exports.getChildren = function(ctx) {
2731

2832
return result;
2933
};
30-
};
34+
}
35+
exports.getChildren = getChildren;
3136

32-
exports.writeState = function(ctx) {
33-
return function(state) {
34-
return function() {
35-
ctx.replaceState({
36-
state: state
37-
});
38-
return function() {
39-
return state;
40-
}
41-
};
37+
function writeState(this_) {
38+
return function(state){
39+
return function(){
40+
this_.replaceState({
41+
state: state
42+
});
43+
return function(){
44+
return state;
45+
}
4246
};
43-
};
47+
};
48+
}
49+
exports.writeState = writeState;
4450

45-
exports.readState = function(ctx) {
46-
return function() {
47-
return ctx.state.state;
48-
};
49-
};
51+
function readState(this_) {
52+
return function(){
53+
return this_.state.state;
54+
};
55+
}
56+
exports.readState = readState;
5057

51-
exports.createClass = function(ss) {
52-
var result = {};
53-
for (var s in ss) {
54-
if (ss.hasOwnProperty(s)) {
55-
if (s === "displayName") {
56-
result[s] = ss[s];
57-
}
58-
else if (s === "componentWillReceiveProps") {
59-
result[s] = (function(impl) {
60-
return function(nextProps) {
61-
return impl(this)(nextProps)();
62-
}
63-
})(ss[s]);
64-
}
65-
else if (s === "shouldComponentUpdate") {
66-
result[s] = (function(impl) {
67-
return function(nextProps, nextState) {
68-
return impl(this)(nextProps)(nextState.state)();
69-
}
70-
})(ss[s]);
71-
}
72-
else if (s === "componentWillUpdate") {
73-
result[s] = (function(impl) {
74-
return function(nextProps, nextState) {
75-
return impl(this)(nextProps)(nextState.state)();
76-
}
77-
})(ss[s]);
78-
}
79-
else if (s === "componentDidUpdate") {
80-
result[s] = (function(impl) {
81-
return function(prevProps, prevState) {
82-
return impl(this)(prevProps)(prevState.state)();
83-
}
84-
})(ss[s]);
85-
}
86-
else {
87-
result[s] = (function(impl) {
88-
return function() {
89-
return impl(this)();
90-
}
91-
})(ss[s]);
92-
}
93-
}
58+
function createClass(spec) {
59+
var result = {
60+
displayName: spec.displayName,
61+
render: function(){
62+
return spec.render(this)();
63+
},
64+
getInitialState: function(){
65+
return spec.getInitialState(this)();
66+
},
67+
componentWillMount: function(){
68+
return spec.componentWillMount(this)();
69+
},
70+
componentDidMount: function(){
71+
return spec.componentDidMount(this)();
72+
},
73+
componentWillReceiveProps: function(nextProps){
74+
return spec.componentWillReceiveProps(this)(nextProps)();
75+
},
76+
shouldComponentUpdate: function(nextProps, nextState){
77+
return spec.shouldComponentUpdate(this)(nextProps)(nextState.state)();
78+
},
79+
componentWillUpdate: function(nextProps, nextState){
80+
return spec.componentWillUpdate(this)(nextProps)(nextState.state)();
81+
},
82+
componentDidUpdate: function(prevProps, prevState){
83+
return spec.componentDidUpdate(this)(prevProps)(prevState.state)();
84+
},
85+
componentWillUnmount: function(){
86+
return spec.componentWillUnmount(this)();
9487
}
95-
result.getInitialState = function() {
96-
return {
97-
state: ss.getInitialState(this)()
98-
};
99-
};
100-
return React.createClass(result);
101-
};
88+
};
10289

103-
exports.handle = function(f) {
104-
return function(e) {
105-
return f(e)();
106-
};
107-
};
90+
return React.createClass(result);
91+
}
92+
exports.createClass = createClass;
10893

109-
exports.createElement = function(clazz) {
110-
return function(props) {
111-
return function(children){
112-
return React.createElement.apply(React, [clazz, props].concat(children));
113-
};
94+
function handle(f) {
95+
return function(e){
96+
return f(e)();
11497
};
11598
};
99+
exports.handle = handle;
116100

117-
exports.createFactory = function(clazz) {
118-
return React.createFactory(clazz);
119-
};
120-
121-
exports.render = function(element) {
122-
return function(container) {
123-
return function() {
124-
return React.render(element, container);
125-
}
101+
function createElement(class_) {
102+
return function(props){
103+
return function(children){
104+
return React.createElement.apply(React, [class_, props].concat(children));
105+
};
126106
};
127-
};
107+
}
108+
exports.createElement = createElement;
128109

129-
exports.renderToString = React.renderToString;
110+
function createFactory(class_) {
111+
return React.createFactory(class_);
112+
}
113+
exports.createFactory = createFactory;

src/React.purs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
module React
44
( ReactElement()
5+
, ReactComponent()
56
, ReactThis()
67

78
, EventHandler()
@@ -52,21 +53,20 @@ module React
5253
, createClass
5354
, createElement
5455
, createFactory
55-
56-
, render
57-
, renderToString
5856
) where
5957

6058
import Prelude (Unit(), ($), bind, pure, return, unit)
6159

62-
import DOM (DOM())
63-
import DOM.Node.Types (Element())
64-
6560
import Control.Monad.Eff (Eff())
6661

62+
import Unsafe.Coerce (unsafeCoerce)
63+
6764
-- | A virtual DOM node, or component.
6865
foreign import data ReactElement :: *
6966

67+
-- | A mounted react component
68+
foreign import data ReactComponent :: *
69+
7070
-- | A reference to a component, essentially React's `this`.
7171
foreign import data ReactThis :: * -> * -> *
7272

@@ -284,6 +284,10 @@ transformState ctx f = do
284284
-- | Create a React class from a specification.
285285
foreign import createClass :: forall props state eff. ReactSpec props state eff -> ReactClass props
286286

287+
-- | Create a stateless React class.
288+
createClassStateless :: forall props. (props -> ReactElement) -> ReactClass props
289+
createClassStateless = unsafeCoerce
290+
287291
-- | Create an event handler.
288292
foreign import handle :: forall eff ev props state result. (ev -> EventHandlerContext eff props state result) -> EventHandler ev
289293

@@ -292,9 +296,3 @@ foreign import createElement :: forall props. ReactClass props -> props -> Array
292296

293297
-- | Create a factory from a React class.
294298
foreign import createFactory :: forall props. ReactClass props -> props -> ReactElement
295-
296-
-- | Render a React element in a document element.
297-
foreign import render :: forall eff. ReactElement -> Element -> Eff (dom :: DOM | eff) ReactElement
298-
299-
-- | Render a React element as a string.
300-
foreign import renderToString :: ReactElement -> String

0 commit comments

Comments
 (0)