2
2
"use strict" ;
3
3
4
4
var React = require ( "react" ) ;
5
- var createReactClass = require ( "create-react-class" ) ;
6
5
7
- function getProps ( this_ ) {
8
- return function ( ) {
9
- return this_ . props ;
10
- } ;
11
- }
12
- exports . getProps = getProps ;
6
+ function createClass ( baseClass ) {
7
+ function bindProperty ( instance , prop , value ) {
8
+ switch ( prop ) {
9
+ case 'componentDidMount' :
10
+ case 'componentDidUpdate' :
11
+ case 'componentWillMount' :
12
+ case 'componentWillUnmount' :
13
+ case 'render' :
14
+ case 'state' :
15
+ instance [ prop ] = value ;
16
+ break ;
17
+
18
+ case 'componentWillReceiveProps' :
19
+ instance [ prop ] = function ( a ) { return value ( a ) ( ) ; } ;
20
+ break ;
21
+
22
+ case 'componentDidCatch' :
23
+ case 'componentWillUpdate' :
24
+ case 'shouldComponentUpdate' :
25
+ instance [ prop ] = function ( a , b ) { return value ( a ) ( b ) ( ) ; } ;
26
+ break ;
27
+
28
+ default :
29
+ throw new Error ( 'Not a component property: ' + prop ) ;
30
+ }
31
+ }
13
32
14
- function getRefs ( this_ ) {
15
- return function ( ) {
16
- return this_ . refs ;
33
+ return function ( displayName ) {
34
+ return function ( ctrFn ) {
35
+ var Constructor = function ( props ) {
36
+ baseClass . call ( this , props ) ;
37
+ var spec = ctrFn ( this ) ( ) ;
38
+ for ( var k in spec ) {
39
+ bindProperty ( this , k , spec [ k ] ) ;
40
+ }
41
+ } ;
42
+
43
+ Constructor . displayName = displayName ;
44
+ Constructor . prototype = Object . create ( baseClass . prototype ) ;
45
+ Constructor . prototype . constructor = Constructor ;
46
+
47
+ return Constructor ;
48
+ } ;
17
49
} ;
18
50
}
19
- exports . getRefs = getRefs ;
20
51
21
- function childrenToArray ( children ) {
22
- var result = [ ] ;
52
+ exports . componentImpl = createClass ( React . Component ) ;
23
53
24
- React . Children . forEach ( children , function ( child ) {
25
- result . push ( child ) ;
26
- } ) ;
54
+ exports . pureComponentImpl = createClass ( React . PureComponent ) ;
27
55
28
- return result ;
29
- }
30
- exports . childrenToArray = childrenToArray ;
56
+ exports . statelessComponent = function ( x ) { return x ; } ;
31
57
32
- function getChildren ( this_ ) {
58
+ function getProps ( this_ ) {
33
59
return function ( ) {
34
- var children = this_ . props . children ;
35
-
36
- var result = childrenToArray ( children ) ;
37
-
38
- return result ;
60
+ return this_ . props ;
39
61
} ;
40
62
}
41
- exports . getChildren = getChildren ;
63
+ exports . getProps = getProps ;
42
64
43
- function readRefImpl ( this_ ) {
44
- return function ( name ) {
45
- return function ( ) {
46
- return this_ [ name ] ;
47
- }
48
- }
49
- }
50
- exports . readRefImpl = readRefImpl ;
65
+ exports . childrenToArray = React . Children . toArray
51
66
52
- function writeRef ( this_ ) {
53
- return function ( name ) {
54
- return function ( node ) {
55
- return function ( ) {
56
- this_ [ name ] = node ;
57
- return { } ;
58
- }
59
- }
60
- }
61
- }
62
- exports . writeRef = writeRef ;
67
+ exports . childrenCount = React . Children . count ;
63
68
64
69
function writeState ( this_ ) {
65
70
return function ( state ) {
66
71
return function ( ) {
67
- this_ . setState ( {
68
- state : state
69
- } ) ;
72
+ this_ . setState ( state ) ;
70
73
return state ;
71
74
} ;
72
75
} ;
@@ -77,9 +80,7 @@ function writeStateWithCallback(this_, cb) {
77
80
return function ( state ) {
78
81
return function ( cb ) {
79
82
return function ( ) {
80
- this_ . setState ( {
81
- state : state
82
- } , cb ) ;
83
+ this_ . setState ( state , cb ) ;
83
84
return state ;
84
85
} ;
85
86
} ;
@@ -89,7 +90,7 @@ exports.writeStateWithCallback = writeStateWithCallback;
89
90
90
91
function readState ( this_ ) {
91
92
return function ( ) {
92
- return this_ . state . state ;
93
+ return this_ . state ;
93
94
} ;
94
95
}
95
96
exports . readState = readState ;
@@ -98,71 +99,13 @@ function transformState(this_){
98
99
return function ( update ) {
99
100
return function ( ) {
100
101
this_ . setState ( function ( old , props ) {
101
- return { state : update ( old . state ) } ;
102
+ return update ( old ) ;
102
103
} ) ;
103
104
} ;
104
105
} ;
105
106
}
106
107
exports . transformState = transformState ;
107
108
108
- function createClass ( toNullable , spec ) {
109
- var didCatch = toNullable ( spec . componentDidCatch )
110
-
111
- var result = {
112
- displayName : spec . displayName ,
113
- render : function ( ) {
114
- return spec . render ( this ) ( ) ;
115
- } ,
116
- getInitialState : function ( ) {
117
- return {
118
- state : spec . getInitialState ( this ) ( )
119
- } ;
120
- } ,
121
- componentWillMount : function ( ) {
122
- return spec . componentWillMount ( this ) ( ) ;
123
- } ,
124
- componentDidMount : function ( ) {
125
- return spec . componentDidMount ( this ) ( ) ;
126
- } ,
127
- componentDidCatch : didCatch
128
- ? function ( error , info ) { return didCatch ( this ) ( error ) ( info ) ( ) ; }
129
- : undefined ,
130
- componentWillReceiveProps : function ( nextProps ) {
131
- return spec . componentWillReceiveProps ( this ) ( nextProps ) ( ) ;
132
- } ,
133
- shouldComponentUpdate : function ( nextProps , nextState ) {
134
- return spec . shouldComponentUpdate ( this ) ( nextProps ) ( nextState . state ) ( ) ;
135
- } ,
136
- componentWillUpdate : function ( nextProps , nextState ) {
137
- return spec . componentWillUpdate ( this ) ( nextProps ) ( nextState . state ) ( ) ;
138
- } ,
139
- componentDidUpdate : function ( prevProps , prevState ) {
140
- return spec . componentDidUpdate ( this ) ( prevProps ) ( prevState . state ) ( ) ;
141
- } ,
142
- componentWillUnmount : function ( ) {
143
- return spec . componentWillUnmount ( this ) ( ) ;
144
- }
145
- } ;
146
-
147
- return createReactClass ( result ) ;
148
- }
149
- exports [ "createClass'" ] = createClass ;
150
-
151
- function capitalize ( s ) {
152
- if ( ! s )
153
- return s ;
154
- return s . charAt ( 0 ) . toUpperCase ( ) + s . slice ( 1 ) ;
155
- } ;
156
-
157
- function createClassStateless ( dict ) {
158
- return function ( f ) {
159
- if ( ! f . displayName )
160
- f . displayName = capitalize ( f . name ) ;
161
- return f ;
162
- } ;
163
- } ;
164
- exports . createClassStateless = createClassStateless ;
165
-
166
109
function forceUpdateCbImpl ( this_ , cb ) {
167
110
this_ . forceUpdate ( function ( ) {
168
111
return cb ( ) ;
@@ -185,24 +128,26 @@ function createElement(class_) {
185
128
} ;
186
129
} ;
187
130
}
188
- exports . createElement = createElement ;
131
+ exports . createElementImpl = createElement ;
189
132
exports . createElementTagName = createElement ;
190
133
134
+ function createLeafElement ( class_ ) {
135
+ return function ( props ) {
136
+ return React . createElement ( class_ , props ) ;
137
+ } ;
138
+ }
139
+ exports . createLeafElementImpl = createLeafElement ;
140
+
191
141
function createElementDynamic ( class_ ) {
192
142
return function ( props ) {
193
143
return function ( children ) {
194
144
return React . createElement ( class_ , props , children ) ;
195
145
} ;
196
146
} ;
197
147
} ;
198
- exports . createElementDynamic = createElementDynamic ;
148
+ exports . createElementDynamicImpl = createElementDynamic ;
199
149
exports . createElementTagNameDynamic = createElementDynamic ;
200
150
201
- function createFactory ( class_ ) {
202
- return React . createFactory ( class_ ) ;
203
- }
204
- exports . createFactory = createFactory ;
205
-
206
151
function preventDefault ( event ) {
207
152
return function ( ) {
208
153
event . preventDefault ( ) ;
0 commit comments