UNPKG

4.71 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _isArray2 = require('lodash/lang/isArray');
8
9var _isArray3 = _interopRequireDefault(_isArray2);
10
11var _map2 = require('lodash/collection/map');
12
13var _map3 = _interopRequireDefault(_map2);
14
15var _filter2 = require('lodash/collection/filter');
16
17var _filter3 = _interopRequireDefault(_filter2);
18
19var _react = require('react');
20
21var _react2 = _interopRequireDefault(_react);
22
23var _makeConfiguration = require('./makeConfiguration');
24
25var _makeConfiguration2 = _interopRequireDefault(_makeConfiguration);
26
27var _isIterable = require('./isIterable');
28
29var _isIterable2 = _interopRequireDefault(_isIterable);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33var generateAppendClassName = undefined,
34 linkClass = undefined,
35 parseStyleName = undefined;
36
37parseStyleName = function (styleNamePropertyValue, allowMultiple) {
38 var styleNames = undefined;
39
40 styleNames = styleNamePropertyValue.split(' ');
41 styleNames = (0, _filter3.default)(styleNames);
42
43 if (allowMultiple === false && styleNames.length > 1) {
44 throw new Error('ReactElement styleName property defines multiple module names ("' + styleNamePropertyValue + '").');
45 }
46
47 return styleNames;
48};
49
50generateAppendClassName = function (styles, styleNames, errorWhenNotFound) {
51 var appendClassName = undefined;
52
53 appendClassName = '';
54
55 appendClassName = (0, _map3.default)(styleNames, function (styleName) {
56 if (styles[styleName]) {
57 return styles[styleName];
58 } else {
59 if (errorWhenNotFound === true) {
60 throw new Error('"' + styleName + '" CSS module is undefined.');
61 }
62
63 return '';
64 }
65 });
66
67 appendClassName = (0, _filter3.default)(appendClassName, 'length');
68
69 appendClassName = appendClassName.join(' ');
70
71 return appendClassName;
72};
73
74/**
75 * @param {ReactElement} element
76 * @param {Object} styles CSS modules class map.
77 * @param {CSSModules~Options} userConfiguration
78 * @returns {ReactElement}
79 */
80linkClass = function (element) {
81 var styles = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
82 var userConfiguration = arguments[2];
83
84 var appendClassName = undefined,
85 children = undefined,
86 clonedElement = undefined,
87 configuration = undefined,
88 newChildren = undefined,
89 newProps = undefined,
90 styleNames = undefined;
91
92 // @see https://github.com/gajus/react-css-modules/pull/30
93 if (!element) {
94 return element;
95 }
96
97 configuration = (0, _makeConfiguration2.default)(userConfiguration);
98
99 styleNames = parseStyleName(element.props.styleName || '', configuration.allowMultiple);
100
101 if (styleNames.length) {
102 appendClassName = generateAppendClassName(styles, styleNames, configuration.errorWhenNotFound);
103 }
104
105 // element.props.children can be one of the following:
106 // 'text'
107 // ['text']
108 // [ReactElement, 'text']
109 // ReactElement
110
111 children = element.props.children;
112
113 if (_react2.default.isValidElement(children)) {
114 newChildren = linkClass(_react2.default.Children.only(children), styles, configuration);
115 } else if ((0, _isArray3.default)(children) || (0, _isIterable2.default)(children)) {
116 /* eslint-disable lodash3/prefer-lodash-method */
117 newChildren = _react2.default.Children.map(children, function (node) {
118 /* eslint-enable lodash3/prefer-lodash-method */
119 if (_react2.default.isValidElement(node)) {
120 return linkClass(node, styles, configuration);
121 } else {
122 return node;
123 }
124 });
125
126 // https://github.com/facebook/react/issues/4723#issuecomment-135555277
127 // Forcing children into an array produces the following error:
128 // Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
129 // newChildren = _.values(newChildren);
130 }
131
132 if (appendClassName) {
133 if (element.props.className) {
134 appendClassName = element.props.className + ' ' + appendClassName;
135 }
136
137 newProps = {
138 className: appendClassName,
139 styleName: null
140 };
141 }
142
143 if (newChildren) {
144 clonedElement = _react2.default.cloneElement(element, newProps, newChildren);
145 } else {
146 clonedElement = _react2.default.cloneElement(element, newProps);
147 }
148
149 return clonedElement;
150};
151
152exports.default = linkClass;
153module.exports = exports['default'];
154//# sourceMappingURL=linkClass.js.map