2
2
3
3
import React , { PureComponent } from 'react' ;
4
4
import PropTypes from 'prop-types' ;
5
- import { theme } from './ThemeProvider' ;
5
+ import { channel } from './ThemeProvider' ;
6
6
import type { Theme } from '../types/Theme' ;
7
7
8
8
type State = {
9
9
theme : Theme ,
10
10
} ;
11
11
12
- export default function withTheme < T : * > (Comp: ReactClass< T > ): ReactClass< T > {
12
+ export default function withThemeName < T : * > (
13
+ Comp: ReactClass< T >
14
+ ): ReactClass< T > {
13
15
class ThemedComponent extends PureComponent < void , * , State > {
14
16
static displayName = `withTheme(${ Comp . displayName || Comp . name } )` ;
15
17
@@ -18,38 +20,55 @@ export default function withTheme<T: *>(Comp: ReactClass<T>): ReactClass<T> {
18
20
} ;
19
21
20
22
static contextTypes = {
21
- [ theme ] : PropTypes . object ,
23
+ [ channel ] : PropTypes . object ,
22
24
} ;
23
25
24
26
constructor ( props , context ) {
25
27
super ( props , context ) ;
28
+
29
+ const theme = this . context [ channel ] && this . context [ channel ] . get ( ) ;
30
+
31
+ if ( typeof theme !== 'object' && typeof this . props . theme !== 'object' ) {
32
+ throw new Error (
33
+ `Couldn't find theme in the context or props. ` +
34
+ `You need to wrap your component in '<ThemeProvider />' or pass a 'theme' prop`
35
+ ) ;
36
+ }
37
+
26
38
this . state = {
27
- theme : this . _merge ( context [ theme ] , props . theme ) ,
39
+ theme : this . _merge ( theme , this . props . theme ) ,
28
40
} ;
29
41
}
30
42
31
43
state : State ;
32
44
33
45
componentDidMount ( ) {
34
- if ( typeof this . state . theme !== 'object' || this . state . theme === null ) {
35
- throw new Error (
36
- "Couldn't find theme in the context or props. " +
37
- "You need to wrap your component in '<ThemeProvider />' or pass a ' theme' prop"
46
+ this . _subscription =
47
+ this . context [ channel ] &&
48
+ this . context [ channel ] . subscribe ( theme =>
49
+ this . setState ( { theme : this . _merge ( theme , this . props . theme ) } )
38
50
) ;
39
- }
40
51
}
41
52
42
- componentWillReceiveProps ( nextProps , nextContext : any ) {
43
- if (
44
- nextProps . theme !== this . props . theme ||
45
- nextContext [ theme ] !== this . context [ theme ]
46
- ) {
53
+ componentWillReceiveProps(nextProps: *) {
54
+ if ( this . props . theme !== nextProps . theme ) {
47
55
this . setState ( {
48
- theme : this . _merge ( nextContext [ theme ] , nextProps . theme ) ,
56
+ theme : this . _merge (
57
+ this . context [ channel ] && this . context [ channel ] . get ( ) ,
58
+ nextProps . theme
59
+ ) ,
49
60
} ) ;
50
61
}
51
62
}
52
63
64
+ componentWillUnmount ( ) {
65
+ this . _subscription && this . _subscription . remove ( ) ;
66
+ }
67
+
68
+ getWrappedInstance() {
69
+ return this . _root ;
70
+ }
71
+
53
72
setNativeProps(...args) {
54
73
return this . _root . setNativeProps ( ...args ) ;
55
74
}
@@ -62,6 +81,7 @@ export default function withTheme<T: *>(Comp: ReactClass<T>): ReactClass<T> {
62
81
}
63
82
} ;
64
83
84
+ _subscription : Object ;
65
85
_root : any ;
66
86
67
87
render ( ) {
0 commit comments