Skip to content

Commit 51bae4e

Browse files
committed
Improved separation of functions.
1 parent 4aeabc1 commit 51bae4e

File tree

4 files changed

+49
-32
lines changed

4 files changed

+49
-32
lines changed

src/linkClass.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,46 @@ import makeConfiguration from './makeConfiguration';
33
import isIterable from './isIterable';
44
import _ from 'lodash';
55

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+
};
746

847
/**
948
* @param {ReactElement} element
@@ -27,31 +66,10 @@ linkClass = (element, styles = {}, userConfiguration) => {
2766

2867
configuration = makeConfiguration(userConfiguration);
2968

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);
5370

54-
appendClassName = appendClassName.join(' ');
71+
if (styleNames.length) {
72+
appendClassName = generateAppendClassName(styles, styleNames, configuration.errorWhenNotFound);
5573
}
5674

5775
// element.props.children can be one of the following:
@@ -87,8 +105,8 @@ linkClass = (element, styles = {}, userConfiguration) => {
87105
}
88106

89107
newProps = {
90-
styleName: null,
91-
className: appendClassName
108+
className: appendClassName,
109+
styleName: null
92110
};
93111
}
94112

tests/extendReactClass.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
} from 'chai';
66

77
import React from 'react';
8-
import ReactDOM from 'react-dom';
98
import TestUtils from 'react-addons-test-utils';
109
import jsdom from 'jsdom';
1110
import extendReactClass from './../src/extendReactClass';

tests/linkClass.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ describe('linkClass', () => {
291291
</div>;
292292

293293
subject = linkClass(subject, {
294-
foo: 'foo-1',
295-
bar: 'bar-1'
294+
bar: 'bar-1',
295+
foo: 'foo-1'
296296
});
297297

298298
expect(subject.props.children[0].props.className).to.deep.equal('bar-1');

tests/reactCssModules.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ describe('reactCssModules', () => {
146146
context('parent component is using react-css-modules and interpolates props.children', () => {
147147
// @see https://github.com/gajus/react-css-modules/issues/76
148148
it('unsets the styleName property', () => {
149-
let Foo,
150-
Bar,
149+
let Bar,
150+
Foo,
151151
subject;
152152

153153
Foo = class extends React.Component {

0 commit comments

Comments
 (0)