1 | 'use strict';
|
2 |
|
3 | var _lodashCollectionFilter2 = require('lodash/collection/filter');
|
4 |
|
5 | var _lodashCollectionFilter3 = _interopRequireDefault(_lodashCollectionFilter2);
|
6 |
|
7 | var _lodashLangIsArray2 = require('lodash/lang/isArray');
|
8 |
|
9 | var _lodashLangIsArray3 = _interopRequireDefault(_lodashLangIsArray2);
|
10 |
|
11 | Object.defineProperty(exports, '__esModule', {
|
12 | value: true
|
13 | });
|
14 |
|
15 | var _react = require('react');
|
16 |
|
17 | var _react2 = _interopRequireDefault(_react);
|
18 |
|
19 | var _makeConfiguration = require('./makeConfiguration');
|
20 |
|
21 | var _makeConfiguration2 = _interopRequireDefault(_makeConfiguration);
|
22 |
|
23 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
24 |
|
25 | var linkClass = undefined;
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | linkClass = 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 |
|
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 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
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 |
|
99 |
|
100 |
|
101 |
|
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 |
|
123 | exports['default'] = linkClass;
|
124 | module.exports = exports['default'];
|
125 |
|