Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve nested entities #23

Merged
merged 1 commit into from
Jun 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// → "<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