UNPKG

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