-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (61 loc) · 2.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import classNames from 'classnames';
import _ from 'lodash';
const mixin = {
_getComponentNameFromDisplayName() {
var displayName = this.bemComponentName || this.constructor.displayName;
return _.camelCase(displayName);
},
getComponentName() {
return this._componentName || (
this._componentName = this._getComponentNameFromDisplayName()
);
},
setComponentName(name) {
this._componentName = name;
},
asHelper(displayName) {
return new function() {
_.extend(this,
{ props: { className: _.camelCase(displayName) } },
_.mapValues(mixin, method => method.bind(this))
);
return _.ary(this.b_, 1);
};
},
b_(bemStrings, optIsWrapper) {
if (this.props.className && (!optIsWrapper || optIsWrapper == 'overflow')) {
this.setComponentName(this.props.className);
}
if (!_.isArray(bemStrings))
bemStrings = [ bemStrings || '' ];
var modificatorSeparator = '_',
elementSeparator = '-',
bemNotation = {};
if (this.props.className) {
if (optIsWrapper == 'inherit')
bemStrings = bemStrings.concat(this.props.className.split(' '));
else
bemNotation[this.props.className] = true;
}
bemStrings.forEach((bemString) => {
if (typeof bemString !== 'string') {
bemNotation['wrongBemString'] = true;
return;
}
var modSeparation = bemString.split(modificatorSeparator),
elementSeparation = modSeparation[0].split(elementSeparator),
modificator = _.get(modSeparation, '[1]', null),
element = _.get(elementSeparation, '[1]', null),
block = elementSeparation[0] || this.getComponentName();
modificator && Object.keys(this.state || {}).length
&& modificator in this.state && !this.state[modificator]
&& (modificator = null);
bemNotation[block] = !element;
bemNotation[block + modificatorSeparator + modificator] = !element && !!modificator;
bemNotation[block + elementSeparator + element] = !!element;
bemNotation[block + elementSeparator + element + modificatorSeparator + modificator] = !!element && !!modificator;
});
return classNames(bemNotation);
}
};
module.exports = mixin;