Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"purescript-unsafe-coerce": "^3.0.0",
"purescript-exceptions": "^3.1.0",
"purescript-maybe": "^3.0.0",
"purescript-nullable": "^3.0.0"
"purescript-nullable": "^3.0.0",
"purescript-foreign": "^4.0.1",
"purescript-typelevel-prelude": "^2.5.0"
},
"devDependencies": {
"purescript-console": "^3.0.0",
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "purescript-react",
"files": [],
"peerDependencies": {
"react": "^16.0.0",
"create-react-class": "^15.6.0"
"react": "^16.0.0"
}
}
154 changes: 54 additions & 100 deletions src/React.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,63 @@
"use strict";

var React = require("react");
var createReactClass = require("create-react-class");

function getProps(this_) {
return function(){
return this_.props;
function createClass(baseClass) {
function bindProperty(instance, prop, value) {
switch (prop) {
case 'componentDidMount':
case 'componentDidUpdate':
case 'componentWillMount':
case 'componentWillUnmount':
case 'render':
case 'state':
instance[prop] = value;
break;

case 'componentWillReceiveProps':
instance[prop] = function (a) { return value(a)(); };
break;

case 'componentDidCatch':
case 'componentWillUpdate':
case 'shouldComponentUpdate':
instance[prop] = function (a, b) { return value(a)(b)(); };
break;

default:
throw new Error('Not a component property: ' + prop);
}
}

return function (displayName) {
return function (ctrFn) {
var Constructor = function (props) {
baseClass.call(this, props);
var spec = ctrFn(this)();
for (var k in spec) {
bindProperty(this, k, spec[k]);
}
};

Constructor.displayName = displayName;
Constructor.prototype = Object.create(baseClass.prototype);
Constructor.prototype.constructor = Constructor;

return Constructor;
};
};
}
exports.getProps = getProps;

function getRefs(this_) {
exports.componentImpl = createClass(React.Component);

exports.pureComponentImpl = createClass(React.PureComponent);

function getProps(this_) {
return function(){
return this_.refs;
return this_.props;
};
}
exports.getRefs = getRefs;
exports.getProps = getProps;

function childrenToArray(children) {
var result = [];
Expand All @@ -40,33 +82,10 @@ function getChildren(this_) {
}
exports.getChildren = getChildren;

function readRefImpl (this_) {
return function(name) {
return function() {
return this_[name];
}
}
}
exports.readRefImpl = readRefImpl;

function writeRef(this_) {
return function(name) {
return function(node) {
return function() {
this_[name] = node;
return {};
}
}
}
}
exports.writeRef = writeRef;

function writeState(this_) {
return function(state){
return function(){
this_.setState({
state: state
});
this_.setState(state);
return state;
};
};
Expand All @@ -77,9 +96,7 @@ function writeStateWithCallback(this_, cb) {
return function(state){
return function(cb){
return function() {
this_.setState({
state: state
}, cb);
this_.setState(state, cb);
return state;
};
};
Expand All @@ -89,7 +106,7 @@ exports.writeStateWithCallback = writeStateWithCallback;

function readState(this_) {
return function(){
return this_.state.state;
return this_.state;
};
}
exports.readState = readState;
Expand All @@ -98,71 +115,13 @@ function transformState(this_){
return function(update){
return function(){
this_.setState(function(old, props){
return {state: update(old.state)};
return update(old);
});
};
};
}
exports.transformState = transformState;

function createClass(toNullable, spec) {
var didCatch = toNullable(spec.componentDidCatch)

var result = {
displayName: spec.displayName,
render: function(){
return spec.render(this)();
},
getInitialState: function(){
return {
state: spec.getInitialState(this)()
};
},
componentWillMount: function(){
return spec.componentWillMount(this)();
},
componentDidMount: function(){
return spec.componentDidMount(this)();
},
componentDidCatch: didCatch
? function(error, info) {return didCatch(this)(error)(info)(); }
: undefined,
componentWillReceiveProps: function(nextProps){
return spec.componentWillReceiveProps(this)(nextProps)();
},
shouldComponentUpdate: function(nextProps, nextState){
return spec.shouldComponentUpdate(this)(nextProps)(nextState.state)();
},
componentWillUpdate: function(nextProps, nextState){
return spec.componentWillUpdate(this)(nextProps)(nextState.state)();
},
componentDidUpdate: function(prevProps, prevState){
return spec.componentDidUpdate(this)(prevProps)(prevState.state)();
},
componentWillUnmount: function(){
return spec.componentWillUnmount(this)();
}
};

return createReactClass(result);
}
exports["createClass'"] = createClass;

function capitalize(s) {
if (!s)
return s;
return s.charAt(0).toUpperCase() + s.slice(1);
};

function createClassStateless(dict) {
return function (f) {
if (!f.displayName)
f.displayName = capitalize(f.name);
return f;
};
};
exports.createClassStateless = createClassStateless;

function forceUpdateCbImpl(this_, cb) {
this_.forceUpdate(function() {
return cb();
Expand Down Expand Up @@ -198,11 +157,6 @@ function createElementDynamic(class_) {
exports.createElementDynamic = createElementDynamic;
exports.createElementTagNameDynamic = createElementDynamic;

function createFactory(class_) {
return React.createFactory(class_);
}
exports.createFactory = createFactory;

function preventDefault(event) {
return function() {
event.preventDefault();
Expand Down
Loading