import _ from './utils'
/*
@class Element Virtrual dom
@param { String } tagName
@param { Object } attrs Element's attrs, å¦: { id: 'list' }
@param { Array } å¯ä»¥æ¯Element对象ï¼ä¹å¯ä»¥åªæ¯å符串ï¼å³textNode
*/
class Element {
constructor(tagName,attrs,children){
//å¦æåªæä¸¤ä¸ªåæ°
if (_.isArray(attrs)) {
children = attrs
attrs = {}
}
this.tagName = tagName
this.attrs = attrs || {}
this.children = children
//设置 this.key 屿§ï¼ä¸ºåé¢çlist diff ååå¤
this.key = attrs ? attrs.key : void 0
}
render () {
let el = document.createElement(this.tagName)
//设置èç¹å±æ§
for (let attrName in attrs){
let attrValue = attrs[attrName]
_.setAttr(el,attrName,attrValue)
}
//设置åèç¹çå
å®¹ï¼æå¯è½è¦éå½è®¾ç½®
let children = this.children || []
children.forEach(child=>{
let childEl = child instanceof Element ? child.render() : document.createTextNode(child)
el.appendChild(childEl)
})
return el
}
}
function el(tagName,attrs,children){
return new Element(tagName,attrs,children)
}
// module.exports = el