Skip to content

Commit c24fd43

Browse files
committed
Trim CSS modules declared in styleNames.
1 parent 39b9b66 commit c24fd43

File tree

4 files changed

+33
-54
lines changed

4 files changed

+33
-54
lines changed

dist/index.js

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
'use strict';
22

3-
var _lodashLangIsObject2 = require('lodash/lang/isObject');
4-
5-
var _lodashLangIsObject3 = _interopRequireDefault(_lodashLangIsObject2);
6-
73
Object.defineProperty(exports, '__esModule', {
84
value: true
95
});
106

11-
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
12-
13-
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
14-
15-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
16-
17-
function _inherits(subClass, superClass) { if (typeof superClass !== 'function' && superClass !== null) { throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
18-
19-
var _linkClass = require('./linkClass');
7+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
208

21-
var _linkClass2 = _interopRequireDefault(_linkClass);
9+
var _extendReactClass = require('./extendReactClass');
2210

23-
var _react = require('react');
11+
var _extendReactClass2 = _interopRequireDefault(_extendReactClass);
2412

25-
var _react2 = _interopRequireDefault(_react);
13+
var _wrapStatelessFunction = require('./wrapStatelessFunction');
2614

27-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
15+
var _wrapStatelessFunction2 = _interopRequireDefault(_wrapStatelessFunction);
2816

2917
var decoratorConstructor = undefined,
3018
functionConstructor = undefined;
@@ -40,41 +28,7 @@ var decoratorConstructor = undefined,
4028
functionConstructor = function (Component, defaultStyles, options) {
4129
var decoratedClass = undefined;
4230

43-
decoratedClass = (function (_Component) {
44-
_inherits(_class, _Component);
45-
46-
function _class() {
47-
_classCallCheck(this, _class);
48-
49-
_get(Object.getPrototypeOf(_class.prototype), 'constructor', this).apply(this, arguments);
50-
}
51-
52-
_createClass(_class, [{
53-
key: 'render',
54-
value: function render() {
55-
var renderResult = undefined,
56-
styles = undefined;
57-
58-
if (this.props.styles) {
59-
styles = this.props.styles;
60-
} else if ((0, _lodashLangIsObject3['default'])(defaultStyles)) {
61-
styles = defaultStyles;
62-
} else {
63-
styles = {};
64-
}
65-
66-
renderResult = _get(Object.getPrototypeOf(_class.prototype), 'render', this).call(this);
67-
68-
if (renderResult) {
69-
return (0, _linkClass2['default'])(renderResult, styles, options);
70-
}
71-
72-
return _react2['default'].createElement('noscript');
73-
}
74-
}]);
75-
76-
return _class;
77-
})(Component);
31+
decoratedClass = Component.isReactClass ? (0, _extendReactClass2['default'])(Component, defaultStyles, options) : (0, _wrapStatelessFunction2['default'])(Component, defaultStyles, options);
7832

7933
if (Component.displayName) {
8034
decoratedClass.displayName = Component.displayName;

dist/linkClass.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'use strict';
22

3+
var _lodashCollectionFilter2 = require('lodash/collection/filter');
4+
5+
var _lodashCollectionFilter3 = _interopRequireDefault(_lodashCollectionFilter2);
6+
37
var _lodashLangIsArray2 = require('lodash/lang/isArray');
48

59
var _lodashLangIsArray3 = _interopRequireDefault(_lodashLangIsArray2);
@@ -42,6 +46,7 @@ linkClass = function (element, styles, userConfiguration) {
4246

4347
if (styleNames) {
4448
styleNames = styleNames.split(' ');
49+
styleNames = (0, _lodashCollectionFilter3['default'])(styleNames);
4550

4651
if (configuration.allowMultiple === false && styleNames.length > 1) {
4752
throw new Error('ReactElement styleName property defines multiple module names ("' + element.props.styleName + '").');

src/linkClass.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ linkClass = (element, styles = {}, userConfiguration) => {
2424

2525
if (styleNames) {
2626
styleNames = styleNames.split(' ');
27+
styleNames = _.filter(styleNames);
2728

2829
if (configuration.allowMultiple === false && styleNames.length > 1) {
2930
throw new Error(`ReactElement styleName property defines multiple module names ("${element.props.styleName}").`);

test/linkClass.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import jsdom from 'jsdom';
1010
import linkClass from './../src/linkClass';
1111

1212
describe('linkClass', () => {
13-
context('when ReactElement does not define styleName', () => {
13+
context('ReactElement does not define styleName', () => {
1414
it('does not affect element properties', () => {
1515
expect(linkClass(<div></div>)).to.deep.equal(<div></div>);
1616
});
@@ -64,7 +64,7 @@ describe('linkClass', () => {
6464
});
6565
});
6666

67-
context('when styleName matches an existing CSS module', () => {
67+
context('styleName matches an existing CSS module', () => {
6868
context('when a descendant element has styleName', () => {
6969
it('assigns a generated className', () => {
7070
let subject;
@@ -126,6 +126,25 @@ describe('linkClass', () => {
126126
});
127127
});
128128

129+
context('styleName includes multiple whitespace characters', () => {
130+
it('resolves CSS modules', () => {
131+
let subject;
132+
133+
subject = <div>
134+
<p styleName=' foo bar '></p>
135+
</div>;
136+
137+
subject = linkClass(subject, {
138+
foo: 'foo-1',
139+
bar: 'bar-1'
140+
}, {
141+
allowMultiple: true
142+
});
143+
144+
expect(subject.props.children.props.className).to.equal('foo-1 bar-1');
145+
});
146+
});
147+
129148
describe('options.allowMultiple', () => {
130149
context('when multiple module names are used', () => {
131150
context('when false', () => {

0 commit comments

Comments
 (0)