1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _isArray2 = require('lodash/isArray');
|
8 |
|
9 | var _isArray3 = _interopRequireDefault(_isArray2);
|
10 |
|
11 | var _isObject2 = require('lodash/isObject');
|
12 |
|
13 | var _isObject3 = _interopRequireDefault(_isObject2);
|
14 |
|
15 | var _map2 = require('lodash/map');
|
16 |
|
17 | var _map3 = _interopRequireDefault(_map2);
|
18 |
|
19 | var _filter2 = require('lodash/filter');
|
20 |
|
21 | var _filter3 = _interopRequireDefault(_filter2);
|
22 |
|
23 | var _react = require('react');
|
24 |
|
25 | var _react2 = _interopRequireDefault(_react);
|
26 |
|
27 | var _makeConfiguration = require('./makeConfiguration');
|
28 |
|
29 | var _makeConfiguration2 = _interopRequireDefault(_makeConfiguration);
|
30 |
|
31 | var _isIterable = require('./isIterable');
|
32 |
|
33 | var _isIterable2 = _interopRequireDefault(_isIterable);
|
34 |
|
35 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
36 |
|
37 | var generateAppendClassName = undefined,
|
38 | _linkClass = undefined,
|
39 | parseStyleName = undefined;
|
40 |
|
41 | parseStyleName = function parseStyleName(styleNamePropertyValue, allowMultiple) {
|
42 | var styleNames = undefined;
|
43 |
|
44 | styleNames = styleNamePropertyValue.split(' ');
|
45 | styleNames = (0, _filter3.default)(styleNames);
|
46 |
|
47 | if (allowMultiple === false && styleNames.length > 1) {
|
48 | throw new Error('ReactElement styleName property defines multiple module names ("' + styleNamePropertyValue + '").');
|
49 | }
|
50 |
|
51 | return styleNames;
|
52 | };
|
53 |
|
54 | generateAppendClassName = function generateAppendClassName(styles, styleNames, errorWhenNotFound) {
|
55 | var appendClassName = undefined;
|
56 |
|
57 | appendClassName = '';
|
58 |
|
59 | appendClassName = (0, _map3.default)(styleNames, function (styleName) {
|
60 | if (styles[styleName]) {
|
61 | return styles[styleName];
|
62 | } else {
|
63 | if (errorWhenNotFound === true) {
|
64 | throw new Error('"' + styleName + '" CSS module is undefined.');
|
65 | }
|
66 |
|
67 | return '';
|
68 | }
|
69 | });
|
70 |
|
71 | appendClassName = (0, _filter3.default)(appendClassName, 'length');
|
72 |
|
73 | appendClassName = appendClassName.join(' ');
|
74 |
|
75 | return appendClassName;
|
76 | };
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | _linkClass = function linkClass(element) {
|
85 | var styles = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
|
86 | var userConfiguration = arguments[2];
|
87 |
|
88 | var appendClassName = undefined,
|
89 | children = undefined,
|
90 | clonedElement = undefined,
|
91 | configuration = undefined,
|
92 | newChildren = undefined,
|
93 | newProps = undefined,
|
94 | styleNames = undefined;
|
95 |
|
96 |
|
97 | if (!(0, _isObject3.default)(element)) {
|
98 | return element;
|
99 | }
|
100 |
|
101 | configuration = (0, _makeConfiguration2.default)(userConfiguration);
|
102 |
|
103 | styleNames = parseStyleName(element.props.styleName || '', configuration.allowMultiple);
|
104 |
|
105 | if (styleNames.length) {
|
106 | appendClassName = generateAppendClassName(styles, styleNames, configuration.errorWhenNotFound);
|
107 | }
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | children = element.props.children;
|
116 |
|
117 | if (_react2.default.isValidElement(children)) {
|
118 | newChildren = _linkClass(_react2.default.Children.only(children), styles, configuration);
|
119 | } else if ((0, _isArray3.default)(children) || (0, _isIterable2.default)(children)) {
|
120 |
|
121 | newChildren = _react2.default.Children.map(children, function (node) {
|
122 |
|
123 | if (_react2.default.isValidElement(node)) {
|
124 | return _linkClass(node, styles, configuration);
|
125 | } else {
|
126 | return node;
|
127 | }
|
128 | });
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | }
|
135 |
|
136 | if (appendClassName) {
|
137 | if (element.props.className) {
|
138 | appendClassName = element.props.className + ' ' + appendClassName;
|
139 | }
|
140 |
|
141 | newProps = {
|
142 | className: appendClassName,
|
143 | styleName: null
|
144 | };
|
145 | }
|
146 |
|
147 | if (newChildren) {
|
148 | clonedElement = _react2.default.cloneElement(element, newProps, newChildren);
|
149 | } else {
|
150 | clonedElement = _react2.default.cloneElement(element, newProps);
|
151 | }
|
152 |
|
153 | return clonedElement;
|
154 | };
|
155 |
|
156 | exports.default = _linkClass;
|
157 | module.exports = exports['default'];
|
158 |
|