I try to create a custom textbox via Web Components
'use strict';
class TextBox extends HTMLElement {
constructor() {
super();
var shadow = this.attachShadow({ mode: 'open' });
let textbox = document.createElement("input");
shadow.appendChild(textbox);
textbox.addEventListener("change", this.validate);
textbox.value ="Init";
}
validate(event) {
console.log("input can be validated");
}
get value() {
console.log("get");
let textbox = this.shadowRoot.querySelector("input");
return textbox.value;
}
set value(newValue) {
console.log("set");
let textbox = this.shadowRoot.querySelector("input");
textbox.value = newValue;
}
}
customElements.define('test-textbox',TextBox);
Later I would like to embedd the custom input in a template and change its values before adding it:
let control= document.getElementById("control");
let clone = control.content.cloneNode(true);
clone.children[0].value = "tb value has been changed before adding";
document.getElementById("app").appendChild(clone);
But If do this in my code the value of the input stays on "Init", but the value of my custom control will have the property "tb value has been changed before adding".
Would it be better to extend from HTMLInput? The set routine also only fires once.