1
- import React from 'react' ;
1
+ import React , { PureComponent } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import hoistStatics from 'hoist-non-react-statics' ;
4
- import * as _ from 'lodash' ;
5
- import normalizeStyle from './StyleNormalizer/normalizeStyle' ;
4
+ import _ from 'lodash' ;
6
5
7
- import Theme , { ThemeShape } from './Theme ' ;
6
+ import normalizeStyle from './StyleNormalizer/normalizeStyle ' ;
8
7
import { resolveComponentStyle } from './resolveComponentStyle' ;
8
+ import Theme , { ThemeShape } from './Theme' ;
9
9
10
10
// TODO - remove withRef warning in next version
11
11
@@ -46,7 +46,12 @@ function getTheme(context) {
46
46
* @returns {StyledComponent } The new component that will handle
47
47
* the styling of the wrapped component.
48
48
*/
49
- export default ( componentStyleName , componentStyle = { } , mapPropsToStyleNames , options = { } ) => {
49
+ export default function connectStyle (
50
+ componentStyleName ,
51
+ componentStyle = { } ,
52
+ mapPropsToStyleNames ,
53
+ options = { }
54
+ ) {
50
55
function getComponentDisplayName ( WrappedComponent ) {
51
56
return WrappedComponent . displayName || WrappedComponent . name || 'Component' ;
52
57
}
@@ -75,7 +80,7 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
75
80
) ;
76
81
}
77
82
78
- class StyledComponent extends React . PureComponent {
83
+ class StyledComponent extends PureComponent {
79
84
static contextTypes = {
80
85
theme : ThemeShape ,
81
86
// The style inherited from the parent
@@ -112,8 +117,10 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
112
117
113
118
constructor ( props , context ) {
114
119
super ( props , context ) ;
120
+
115
121
const styleNames = this . resolveStyleNames ( props ) ;
116
122
const resolvedStyle = this . resolveStyle ( context , props , styleNames ) ;
123
+
117
124
this . setWrappedInstance = this . setWrappedInstance . bind ( this ) ;
118
125
this . transformProps = this . transformProps . bind ( this ) ;
119
126
@@ -137,10 +144,11 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
137
144
} ;
138
145
}
139
146
140
- componentWillReceiveProps ( nextProps , nextContext ) {
141
- const styleNames = this . resolveStyleNames ( nextProps ) ;
142
- if ( this . shouldRebuildStyle ( nextProps , nextContext , styleNames ) ) {
143
- const resolvedStyle = this . resolveStyle ( nextContext , nextProps , styleNames ) ;
147
+ componentDidUpdate ( prevProps ) {
148
+ const styleNames = this . resolveStyleNames ( this . props ) ;
149
+
150
+ if ( this . shouldRebuildStyle ( prevProps , styleNames ) ) {
151
+ const resolvedStyle = this . resolveStyle ( this . context , this . props , styleNames ) ;
144
152
145
153
this . setState ( {
146
154
style : resolvedStyle . componentStyle ,
@@ -152,7 +160,7 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
152
160
153
161
setNativeProps ( nativeProps ) {
154
162
if ( ! this . isRefDefined ( ) ) {
155
- console . warn ( 'setNativeProps can\'nt be used on stateless components' ) ;
163
+ console . warn ( 'setNativeProps can\'t be used on stateless components' ) ;
156
164
return ;
157
165
}
158
166
if ( this . wrappedInstance . setNativeProps ) {
@@ -164,19 +172,17 @@ export default (componentStyleName, componentStyle = {}, mapPropsToStyleNames, o
164
172
this . wrappedInstance = component ;
165
173
}
166
174
167
- hasStyleNameChanged ( nextProps , styleNames ) {
168
- return mapPropsToStyleNames && this . props !== nextProps &&
169
- // Even though props did change here,
170
- // it doesn't necessary means changed props are those which affect styleName
175
+ hasStyleNameChanged ( prevProps , styleNames ) {
176
+ return mapPropsToStyleNames && this . props !== prevProps &&
177
+ // Even though props did change here, it doesn't necessarily mean
178
+ // props that affect styleName have changed
171
179
! _ . isEqual ( this . state . styleNames , styleNames ) ;
172
180
}
173
181
174
- shouldRebuildStyle ( nextProps , nextContext , styleNames ) {
175
- return ( nextProps . style !== this . props . style ) ||
176
- ( nextProps . styleName !== this . props . styleName ) ||
177
- ( nextContext . theme !== this . context . theme ) ||
178
- ( nextContext . parentStyle !== this . context . parentStyle ) ||
179
- ( this . hasStyleNameChanged ( nextProps , styleNames ) ) ;
182
+ shouldRebuildStyle ( prevProps , styleNames ) {
183
+ return ( prevProps . style !== this . props . style ) ||
184
+ ( prevProps . styleName !== this . props . styleName ) ||
185
+ ( this . hasStyleNameChanged ( prevProps , styleNames ) ) ;
180
186
}
181
187
182
188
resolveStyleNames ( props ) {
0 commit comments