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 2, 2017
1 parent cba2209 commit 9ac30f2
Show file tree
Hide file tree
Showing 3 changed files with 40 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'}/>"
```

43 changes: 33 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 = (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;
};

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

Expand All @@ -68,19 +90,20 @@ 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 (var key in json) {
if (!~['mix', 'content', 'attrs'].indexOf(key) && typeof Object(json[key]).block === 'string') {
var nestedJSX = setJsx(json[key]);

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);
for (i = 0; i < this.plugins.length; i++) {
this.plugins[i](nestedJSX, Object.assign({ block: json[key].block }, json[key]));
}

json[key] = nestedJSX;
}
}

for (i = 0; i < this.plugins.length; i++) {
Expand Down
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ describe('transform', () => {
).to.equal(`<Button2 mix={{ 'block': 'header', 'elem': 'button' }}/>`);
});

it('should provide custom prop as jsx', () => {
expect(
transform({ block: 'button2', custom: {block: 'header', elem: 'button' } }).JSX
).to.equal(`<Button2 custom={<HeaderButton/>}/>`);
});

it('should treat strings as text', () => {
expect(
transform(['Hello I am a string', { block: 'button2', content: 'Hello I am a string'}]).JSX
Expand Down

0 comments on commit 9ac30f2

Please sign in to comment.