Skip to content

Commit

Permalink
Implement prop-attr sync for Button Component #97
Browse files Browse the repository at this point in the history
  • Loading branch information
cwpeng committed Sep 21, 2021
1 parent 8831fdb commit db13846
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions wc/components/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,26 @@ class Button extends WComponent{
super();
}
init(){
this.render();
}
update(){
this.render();
}
render(){
const classList=[];
classList.push(this.display, this.size, this.outlined?"outline-"+this.color:this.color);
const attrs={};
const attrs={removes:[]};
if(this.disabled){
attrs["disabled"]=true;
}else{
attrs.removes.push("disabled");
}
if(this.btn){
DOM.modify(this.btn, {props:{className:classList.join(" ")}, attrs:attrs});
}else{
this.btn=DOM.create("button", {props:{className:classList.join(" ")}, attrs:attrs}, this.shadowRoot);
DOM.create("slot", {}, this.btn);
}
const btn=DOM.create("button", {props:{className:classList.join(" ")}, attrs:attrs}, this.shadowRoot);
DOM.create("slot", {}, btn);
}
}
Button.prototype.stylesheet=stylesheet;
Expand Down
5 changes: 5 additions & 0 deletions wc/util/DOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ const DOM={
return element;
},
setAttributes:function(element, attributes){
if(attributes.removes instanceof Array){
attributes.removes.forEach((name)=>{
element.removeAttribute(name);
});
}
for(const name in attributes){
element.setAttribute(name, attributes[name]);
}
Expand Down

0 comments on commit db13846

Please sign in to comment.