Skip to content

Commit

Permalink
Resolve nested entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Smirnov committed Jun 1, 2017
1 parent cba2209 commit 07e0e2a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var bemjson = {

var jsxTree = bemjsonToJSX.process(bemjson);

console.log(jsxTree.JSX));
console.log(jsxTree.JSX);
// → "<Button2 theme={'normal'} size={'m'} text={'hello world'}/>"
```

45 changes: 35 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function JSXNode(tag, props, children) {
var propsToStr = props => Object.keys(props).reduce((acc, k) => {
if (typeof props[k] === 'string') {
return acc + ` ${k}=${valToStr(props[k])}`
} else if (props[k] instanceof JSXNode) {
return acc + ` ${k}={${render(props[k])}}`
} else {
return acc + ` ${k}={${valToStr(props[k])}}`
}
Expand Down Expand Up @@ -59,6 +61,26 @@ Transformer.prototype.process = function(bemjson) {
var root = nodes[0];

var node;

var setJsx = function(json) {
var jsx = new JSXNode();
var _blockName = json.block || node.blockName;

if (typeof json === 'string') {
jsx.isText = true;
jsx.simpleText = json;
}

if (json.tag) {
jsx.tag = json.tag;
} else if (json.block || json.elem) {
jsx.bemEntity = new BemEntity({ block: _blockName, elem: json.elem });
jsx.tag = this.bemNaming.stringify(jsx.bemEntity);
}

return jsx;
}.bind(this);

while((node = nodes.shift())) {
var json = node.json, i;

Expand All @@ -68,19 +90,22 @@ Transformer.prototype.process = function(bemjson) {
}
} else {
var res = undefined;
var jsx = new JSXNode();
var blockName = json.block || node.blockName;

if (typeof json === 'string') {
jsx.isText = true;
jsx.simpleText = json;
}
var jsx = setJsx(json);

for (key in json) {
if (key !== 'mix' && typeof Object(json[key]).block === 'string') {
var nestedJSX = setJsx(json[key]);

for (i = 0; i < this.plugins.length; i++) {
var plugin = this.plugins[i];

if (json.tag) {
jsx.tag = json.tag;
} else if (json.block || json.elem) {
jsx.bemEntity = new BemEntity({ block: blockName, elem: json.elem });
jsx.tag = this.bemNaming.stringify(jsx.bemEntity);
plugin(nestedJSX, Object.assign({ block: json[key].block }, json[key]));
}

json[key] = nestedJSX;
}
}

for (i = 0; i < this.plugins.length; i++) {
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var btj = require('./lib/index');

var bemjson = {
block: 'button2',
mods: { theme: 'normal', size: 'm' },
text: 'hello world',
icon: {block: 'test', mods: {'test-mod': 'yes'}}
};

var jsxTree = btj().process(bemjson);

console.log(jsxTree.JSX);
// → "<Button2 theme={'normal'} size={'m'} text={'hello world'}/>"

0 comments on commit 07e0e2a

Please sign in to comment.