@@ -22,7 +22,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
2222
2323 static propTypes = {
2424 composeTheme : PropTypes . oneOf ( [ COMPOSE_DEEPLY , COMPOSE_SOFTLY , DONT_COMPOSE ] ) ,
25- theme : PropTypes . object
25+ theme : PropTypes . object ,
26+ themeNamespace : PropTypes . string
2627 }
2728
2829 static defaultProps = {
@@ -38,8 +39,19 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
3839 return this . refs . wrappedInstance
3940 }
4041
42+ getNamespacedTheme ( ) {
43+ const { themeNamespace, theme } = this . props
44+ if ( ! themeNamespace ) return theme
45+ if ( themeNamespace && ! theme ) throw new Error ( 'Invalid themeNamespace use in react-css-themr. ' +
46+ 'themeNamespace prop should be used only with theme prop.' )
47+
48+ return Object . keys ( theme )
49+ . filter ( key => key . startsWith ( themeNamespace ) )
50+ . reduce ( ( result , key ) => ( { ...result , [ removeNamespace ( key , themeNamespace ) ] : theme [ key ] } ) , { } )
51+ }
52+
4153 getThemeNotComposed ( ) {
42- if ( this . props . theme ) return this . props . theme
54+ if ( this . props . theme ) return this . getNamespacedTheme ( )
4355 if ( localTheme ) return localTheme
4456 return this . getContextTheme ( )
4557 }
@@ -52,8 +64,8 @@ export default (componentName, localTheme, options = {}) => (ThemedComponent) =>
5264
5365 getTheme ( ) {
5466 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 ( ) )
5769 }
5870
5971 render ( ) {
@@ -101,3 +113,8 @@ function validateComposeOption(composeTheme) {
101113 )
102114 }
103115}
116+
117+ function removeNamespace ( key , themeNamespace ) {
118+ const capitilized = key . substr ( themeNamespace . length )
119+ return capitilized . slice ( 0 , 1 ) . toLowerCase ( ) + capitilized . slice ( 1 )
120+ }
0 commit comments