@@ -48,6 +48,11 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
48
48
composeTheme : optionComposeTheme
49
49
}
50
50
51
+ constructor ( ...args ) {
52
+ super ( ...args )
53
+ this . theme_ = this . calcTheme ( this . props )
54
+ }
55
+
51
56
getWrappedInstance ( ) {
52
57
invariant ( optionWithRef ,
53
58
'To access the wrapped instance, you need to specify ' +
@@ -57,8 +62,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
57
62
return this . refs . wrappedInstance
58
63
}
59
64
60
- getNamespacedTheme ( ) {
61
- const { themeNamespace, theme } = this . props
65
+ getNamespacedTheme ( props ) {
66
+ const { themeNamespace, theme } = props
62
67
if ( ! themeNamespace ) return theme
63
68
if ( themeNamespace && ! theme ) throw new Error ( 'Invalid themeNamespace use in react-css-themr. ' +
64
69
'themeNamespace prop should be used only with theme prop.' )
@@ -68,8 +73,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
68
73
. reduce ( ( result , key ) => ( { ...result , [ removeNamespace ( key , themeNamespace ) ] : theme [ key ] } ) , { } )
69
74
}
70
75
71
- getThemeNotComposed ( ) {
72
- if ( this . props . theme ) return this . getNamespacedTheme ( )
76
+ getThemeNotComposed ( props ) {
77
+ if ( props . theme ) return this . getNamespacedTheme ( props )
73
78
if ( config . localTheme ) return config . localTheme
74
79
return this . getContextTheme ( )
75
80
}
@@ -80,30 +85,50 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
80
85
: { }
81
86
}
82
87
83
- getTheme ( ) {
84
- return this . props . composeTheme === COMPOSE_SOFTLY
85
- ? { ...this . getContextTheme ( ) , ...config . localTheme , ...this . getNamespacedTheme ( ) }
86
- : themeable ( themeable ( this . getContextTheme ( ) , config . localTheme ) , this . getNamespacedTheme ( ) )
88
+ getTheme ( props ) {
89
+ return props . composeTheme === COMPOSE_SOFTLY
90
+ ? {
91
+ ...this . getContextTheme ( ) ,
92
+ ...config . localTheme ,
93
+ ...this . getNamespacedTheme ( props )
94
+ }
95
+ : themeable (
96
+ themeable ( this . getContextTheme ( ) , config . localTheme ) ,
97
+ this . getNamespacedTheme ( props )
98
+ )
99
+ }
100
+
101
+ calcTheme ( props ) {
102
+ const { composeTheme } = props
103
+ return composeTheme
104
+ ? this . getTheme ( props )
105
+ : this . getThemeNotComposed ( props )
106
+ }
107
+
108
+ componentWillReceiveProps ( nextProps ) {
109
+ if (
110
+ nextProps . composeTheme !== this . props . composeTheme ||
111
+ nextProps . theme !== this . props . theme ||
112
+ nextProps . themeNamespace !== this . props . themeNamespace
113
+ ) {
114
+ this . theme_ = this . calcTheme ( nextProps )
115
+ }
87
116
}
88
117
89
118
render ( ) {
90
- const { composeTheme , ...rest } = this . props
119
+ const { ...rest } = this . props
91
120
let renderedElement
92
121
93
122
if ( optionWithRef ) {
94
123
renderedElement = React . createElement ( ThemedComponent , {
95
124
...rest ,
96
125
ref : 'wrappedInstance' ,
97
- theme : composeTheme
98
- ? this . getTheme ( )
99
- : this . getThemeNotComposed ( )
126
+ theme : this . theme_
100
127
} )
101
128
} else {
102
129
renderedElement = React . createElement ( ThemedComponent , {
103
130
...rest ,
104
- theme : composeTheme
105
- ? this . getTheme ( )
106
- : this . getThemeNotComposed ( )
131
+ theme : this . theme_
107
132
} )
108
133
}
109
134
0 commit comments