1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _isArray2 = require('lodash/lang/isArray');
|
8 |
|
9 | var _isArray3 = _interopRequireDefault(_isArray2);
|
10 |
|
11 | var _map2 = require('lodash/collection/map');
|
12 |
|
13 | var _map3 = _interopRequireDefault(_map2);
|
14 |
|
15 | var _filter2 = require('lodash/collection/filter');
|
16 |
|
17 | var _filter3 = _interopRequireDefault(_filter2);
|
18 |
|
19 | var _react = require('react');
|
20 |
|
21 | var _react2 = _interopRequireDefault(_react);
|
22 |
|
23 | var _makeConfiguration = require('./makeConfiguration');
|
24 |
|
25 | var _makeConfiguration2 = _interopRequireDefault(_makeConfiguration);
|
26 |
|
27 | var _isIterable = require('./isIterable');
|
28 |
|
29 | var _isIterable2 = _interopRequireDefault(_isIterable);
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 | var linkClass = undefined;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | linkClass = 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 |
|
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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
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 |
|
99 | newChildren = _react2.default.Children.map(children, function (node) {
|
100 |
|
101 | if (_react2.default.isValidElement(node)) {
|
102 | return linkClass(node, styles, configuration);
|
103 | } else {
|
104 | return node;
|
105 | }
|
106 | });
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
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 |
|
133 | exports.default = linkClass;
|
134 | module.exports = exports['default'];
|
135 |
|