@@ -22,7 +22,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
22
22
23
23
static propTypes = {
24
24
composeTheme : PropTypes . oneOf ( [ COMPOSE_DEEPLY , COMPOSE_SOFTLY , DONT_COMPOSE ] ) ,
25
- theme : PropTypes . object
25
+ theme : PropTypes . object ,
26
+ namespace : PropTypes . string
26
27
}
27
28
28
29
static defaultProps = {
@@ -38,8 +39,19 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
38
39
return this . refs . wrappedInstance
39
40
}
40
41
42
+ getNamespacedTheme ( ) {
43
+ const { namespace, theme } = this . props
44
+ if ( ! namespace ) return theme
45
+ if ( namespace && ! theme ) throw new Error ( 'Invalid namespace use in react-css-themr. ' +
46
+ 'Namespace prop should be used only with theme prop.' )
47
+
48
+ return Object . keys ( theme )
49
+ . filter ( key => key . startsWith ( namespace ) )
50
+ . reduce ( ( p , c ) => ( { ...p , [ removeNamespace ( c , namespace ) ] : theme [ c ] } ) , { } )
51
+ }
52
+
41
53
getThemeNotComposed ( ) {
42
- if ( this . props . theme ) return this . props . theme
54
+ if ( this . props . theme ) return this . getNamespacedTheme ( )
43
55
if ( localTheme ) return localTheme
44
56
return this . getContextTheme ( )
45
57
}
@@ -52,8 +64,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
52
64
53
65
getTheme ( ) {
54
66
return this . props . composeTheme === COMPOSE_SOFTLY
55
- ? Object . assign ( { } , this . getContextTheme ( ) , localTheme , this . props . theme )
56
- : themeable ( themeable ( this . getContextTheme ( ) , localTheme ) , this . props . theme )
67
+ ? Object . assign ( { } , this . getContextTheme ( ) , localTheme , this . getNamespacedTheme ( ) )
68
+ : themeable ( themeable ( this . getContextTheme ( ) , localTheme ) , this . getNamespacedTheme ( ) )
57
69
}
58
70
59
71
render ( ) {
@@ -101,3 +113,8 @@ function validateComposeOption(composeTheme) {
101
113
)
102
114
}
103
115
}
116
+
117
+ function removeNamespace ( key , namespace ) {
118
+ const capitilized = key . substr ( namespace . length )
119
+ return capitilized . slice ( 0 , 1 ) . toLowerCase ( ) + capitilized . slice ( 1 )
120
+ }
0 commit comments