-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathReact.js
158 lines (134 loc) · 4.07 KB
/
React.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* global exports */
"use strict";
var React = require("react");
function createClass(baseClass) {
function bindProperty(instance, prop, value) {
switch (prop) {
case 'state':
case 'render':
case 'componentDidMount':
case 'componentWillUnmount':
instance[prop] = value;
break;
case 'componentDidCatch':
case 'componentWillUpdate':
case 'shouldComponentUpdate':
case 'getSnapshotBeforeUpdate':
instance[prop] = function (a, b) { return value(a)(b)(); };
break;
case 'componentDidUpdate':
instance[prop] = function (a, b, c) { return value(a)(b)(c)(); };
break;
case 'unsafeComponentWillMount':
instance['UNSAFE_componentWillMount'] = value;
break;
case 'unsafeComponentWillReceiveProps':
instance['UNSAFE_componentWillReceiveProps'] = function (a) { return value(a)(); };
break;
case 'unsafeComponentWillUpdate':
instance['UNSAFE_componentWillUpdate'] = function (a, b) { return value(a)(b)(); };
break;
default:
throw new Error('[purescript-react] 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;
};
};
}
function createClassWithDerivedState(classCtr) {
return function(displayName) {
return function(getDerivedStateFromProps) {
return function(ctrFn) {
var Constructor = componentImpl(displayName)(ctrFn);
Constructor.getDerivedStateFromProps = function(a, b) { return getDerivedStateFromProps(a)(b) };
return Constructor;
};
};
};
}
var componentImpl = createClass(React.Component);
exports.componentImpl = componentImpl;
exports.componentWithDerivedStateImpl = createClassWithDerivedState(componentImpl);
var pureComponentImpl = createClass(React.PureComponent);
exports.pureComponentImpl = pureComponentImpl
exports.pureComponentWithDerivedStateImpl = createClassWithDerivedState(pureComponentImpl);
exports.statelessComponent = function(x) { return x; };
exports.fragment = React.Fragment;
function getProps(this_) {
return function(){
return this_.props;
};
}
exports.getProps = getProps;
function setStateImpl(this_) {
return function(state){
return function(){
this_.setState(state);
};
};
}
exports.setStateImpl = setStateImpl;
function setStateWithCallbackImpl(this_) {
return function(state){
return function(cb){
return function() {
this_.setState(state, cb);
};
};
};
}
exports.setStateWithCallbackImpl = setStateWithCallbackImpl;
function getState(this_) {
return function(){
if (!this_.state) {
throw new Error('[purescript-react] Cannot get state within constructor');
}
return this_.state;
};
}
exports.getState = getState;
function forceUpdateWithCallback(this_) {
return function(cb) {
return function() {
this_.forceUpdate(cb);
};
};
}
exports.forceUpdateWithCallback = forceUpdateWithCallback;
function createElement(class_) {
return function(props){
return function(children){
return React.createElement.apply(React, [class_, props].concat(children));
};
};
}
exports.createElementImpl = createElement;
exports.createElementTagName = createElement;
function createLeafElement(class_) {
return function(props) {
return React.createElement(class_, props);
};
}
exports.createLeafElementImpl = createLeafElement;
function createElementDynamic(class_) {
return function(props) {
return function(children){
return React.createElement(class_, props, children);
};
};
};
exports.createElementDynamicImpl = createElementDynamic;
exports.createElementTagNameDynamic = createElementDynamic;