UNPKG

4.23 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 linkClass = undefined;
34
35/**
36 * @param {ReactElement} element
37 * @param {Object} styles CSS modules class map.
38 * @param {CSSModules~Options} userConfiguration
39 * @return {ReactElement}
40 */
41linkClass = function (element) {
42 var styles = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
43 var userConfiguration = arguments[2];
44
45 var appendClassName = undefined,
46 children = undefined,
47 clonedElement = undefined,
48 configuration = undefined,
49 newChildren = undefined,
50 newProps = undefined,
51 styleNames = undefined;
52
53 // @see https://github.com/gajus/react-css-modules/pull/30
54 if (!element) {
55 return element;
56 }
57
58 configuration = (0, _makeConfiguration2.default)(userConfiguration);
59
60 styleNames = element.props.styleName;
61
62 if (styleNames) {
63 styleNames = styleNames.split(' ');
64 styleNames = (0, _filter3.default)(styleNames);
65
66 if (configuration.allowMultiple === false && styleNames.length > 1) {
67 throw new Error('ReactElement styleName property defines multiple module names ("' + element.props.styleName + '").');
68 }
69
70 appendClassName = (0, _map3.default)(styleNames, function (styleName) {
71 if (styles[styleName]) {
72 return styles[styleName];
73 } else {
74 if (configuration.errorWhenNotFound === true) {
75 throw new Error('"' + styleName + '" CSS module is undefined.');
76 }
77
78 return '';
79 }
80 });
81
82 appendClassName = (0, _filter3.default)(appendClassName, 'length');
83
84 appendClassName = appendClassName.join(' ');
85 }
86
87 // element.props.children can be one of the following:
88 // 'text'
89 // ['text']
90 // [ReactElement, 'text']
91 // ReactElement
92
93 children = element.props.children;
94
95 if (_react2.default.isValidElement(children)) {
96 newChildren = linkClass(_react2.default.Children.only(children), styles, configuration);
97 } else if ((0, _isArray3.default)(children) || (0, _isIterable2.default)(children)) {
98 /* eslint-disable lodash3/prefer-lodash-method */
99 newChildren = _react2.default.Children.map(children, function (node) {
100 /* eslint-enable lodash3/prefer-lodash-method */
101 if (_react2.default.isValidElement(node)) {
102 return linkClass(node, styles, configuration);
103 } else {
104 return node;
105 }
106 });
107
108 // https://github.com/facebook/react/issues/4723#issuecomment-135555277
109 // Forcing children into an array produces the following error:
110 // Warning: A ReactFragment is an opaque type. Accessing any of its properties is deprecated. Pass it to one of the React.Children helpers.
111 // newChildren = _.values(newChildren);
112 }
113
114 if (appendClassName) {
115 if (element.props.className) {
116 appendClassName = element.props.className + ' ' + appendClassName;
117 }
118
119 newProps = {
120 className: appendClassName
121 };
122 }
123
124 if (newChildren) {
125 clonedElement = _react2.default.cloneElement(element, newProps, newChildren);
126 } else {
127 clonedElement = _react2.default.cloneElement(element, newProps);
128 }
129
130 return clonedElement;
131};
132
133exports.default = linkClass;
134module.exports = exports['default'];
135//# sourceMappingURL=linkClass.js.map