@@ -3,7 +3,46 @@ import makeConfiguration from './makeConfiguration';
3
3
import isIterable from './isIterable' ;
4
4
import _ from 'lodash' ;
5
5
6
- let linkClass ;
6
+ let generateAppendClassName ,
7
+ linkClass ,
8
+ parseStyleName ;
9
+
10
+ parseStyleName = ( styleNamePropertyValue : string , allowMultiple : boolean ) : Array < string > => {
11
+ let styleNames ;
12
+
13
+ styleNames = styleNamePropertyValue . split ( ' ' ) ;
14
+ styleNames = _ . filter ( styleNames ) ;
15
+
16
+ if ( allowMultiple === false && styleNames . length > 1 ) {
17
+ throw new Error ( 'ReactElement styleName property defines multiple module names ("' + styleNamePropertyValue + '").' ) ;
18
+ }
19
+
20
+ return styleNames ;
21
+ } ;
22
+
23
+ generateAppendClassName = ( styles , styleNames : Array < string > , errorWhenNotFound: boolean): string => {
24
+ let appendClassName ;
25
+
26
+ appendClassName = '' ;
27
+
28
+ appendClassName = _ . map ( styleNames , ( styleName ) => {
29
+ if ( styles [ styleName ] ) {
30
+ return styles [ styleName ] ;
31
+ } else {
32
+ if ( errorWhenNotFound === true ) {
33
+ throw new Error ( '"' + styleName + '" CSS module is undefined.' ) ;
34
+ }
35
+
36
+ return '' ;
37
+ }
38
+ } ) ;
39
+
40
+ appendClassName = _ . filter ( appendClassName , 'length' ) ;
41
+
42
+ appendClassName = appendClassName . join ( ' ' ) ;
43
+
44
+ return appendClassName ;
45
+ } ;
7
46
8
47
/**
9
48
* @param { ReactElement } element
@@ -27,31 +66,10 @@ linkClass = (element, styles = {}, userConfiguration) => {
27
66
28
67
configuration = makeConfiguration ( userConfiguration ) ;
29
68
30
- styleNames = element . props . styleName ;
31
-
32
- if ( styleNames ) {
33
- styleNames = styleNames . split ( ' ' ) ;
34
- styleNames = _ . filter ( styleNames ) ;
35
-
36
- if ( configuration . allowMultiple === false && styleNames . length > 1 ) {
37
- throw new Error ( 'ReactElement styleName property defines multiple module names ("' + element . props . styleName + '").' ) ;
38
- }
39
-
40
- appendClassName = _ . map ( styleNames , ( styleName ) => {
41
- if ( styles [ styleName ] ) {
42
- return styles [ styleName ] ;
43
- } else {
44
- if ( configuration . errorWhenNotFound === true ) {
45
- throw new Error ( '"' + styleName + '" CSS module is undefined.' ) ;
46
- }
47
-
48
- return '' ;
49
- }
50
- } ) ;
51
-
52
- appendClassName = _ . filter ( appendClassName , 'length' ) ;
69
+ styleNames = parseStyleName ( element . props . styleName || '' , configuration . allowMultiple ) ;
53
70
54
- appendClassName = appendClassName . join ( ' ' ) ;
71
+ if ( styleNames . length ) {
72
+ appendClassName = generateAppendClassName ( styles , styleNames , configuration . errorWhenNotFound ) ;
55
73
}
56
74
57
75
// element.props.children can be one of the following:
@@ -87,8 +105,8 @@ linkClass = (element, styles = {}, userConfiguration) => {
87
105
}
88
106
89
107
newProps = {
90
- styleName : null ,
91
- className : appendClassName
108
+ className : appendClassName ,
109
+ styleName : null
92
110
} ;
93
111
}
94
112
0 commit comments