-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from bem-sdk/yeti-or.nestedCustomProps
Nested custom props
- Loading branch information
Showing
7 changed files
with
125 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
demo | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var pascalCase = require('pascal-case'); | ||
|
||
var reactMappings = require('./reactMappings'); | ||
var valToStr = require('./helpers').valToStr; | ||
|
||
function JSXNode(tag, props, children) { | ||
this.tag = tag || 'div'; | ||
this.props = props || {}; | ||
this.children = children || []; | ||
this.bemEntity = null; | ||
this.isJSON = false; | ||
this.isSimple = false; | ||
this.isText = false; | ||
this.simpleVal = undefined; | ||
} | ||
|
||
var propsToStr = props => Object.keys(props).reduce((acc, k) => { | ||
if (typeof props[k] === 'string') { | ||
return acc + ` ${k}=${valToStr(props[k])}` | ||
} else { | ||
return acc + ` ${k}={${valToStr(props[k])}}` | ||
} | ||
}, ''); | ||
|
||
var tagToClass = tag => reactMappings[tag] ? tag : pascalCase(tag); | ||
|
||
JSXNode.prototype.toString = function() { | ||
if (this.isText) { | ||
return this.simpleVal; | ||
} | ||
if (this.isSimple) { | ||
return this.simpleVal; | ||
} | ||
if (this.isJSON) { | ||
return valToStr(this.props); | ||
} | ||
|
||
var tag = tagToClass(this.tag); | ||
var children = [].concat(this.children) | ||
.filter(Boolean) | ||
// remove empty text nodes | ||
.filter(child => !(child.isText && child.simpleVal === '')); | ||
|
||
var str = children.length ? | ||
`<${tag}${propsToStr(this.props)}>\n${children.join('\n')}\n</${tag}>` : | ||
`<${tag}${propsToStr(this.props)}/>`; | ||
return str; | ||
}; | ||
|
||
|
||
module.exports = JSXNode; | ||
module.exports.tagToClass = tagToClass; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters