I am currently trying to implement a relay WebComponentto allow the company to easily create interfaces without any framework(a decision made by the architecture).
Here is my current code:
<ul>
<company-repeat datas='[{"name": "NameValeur", "value": "valeurId"}, {"name": "NameObject", "value": "objectId"}]'>
<li>${name}</option>
</company-repeat>
</ul>
<select name="" id="">
<company-repeat datas='[{"name": "NameValeur", "value": "valeurId"}, {"name": "NameObject", "value": "objectId"}]'>
<option value="${value}">${name}</option>
</company-repeat>
</select>
listIt works correctly, since it does not have restrictions on what tag is allowed inside, but the choice does not allow customElement company-repeatit to expand, to break this function and simply display it<option value="${value}">${name}</option>
Here is the source code of my WebComponent
class CompanyRepeater extends HTMLElement {
connectedCallback() {
this.render();
}
render() {
let datas = JSON.parse(this.getAttribute('datas'));
let elementType = this.getAttribute('element');
this.template = this.innerHTML;
console.log(elementType);
let htmlContent = elementType !== null ? `<${elementType.toLowerCase()}>` : '';
datas.forEach(elem => {
htmlContent += this.interpolate(this.template, elem)}
);
htmlContent += elementType !== null ? `</${elementType.toLowerCase()}>` : '';
this.innerHTML = htmlContent;
}
interpolate(template, obj) {
for(var key in obj) {
const pattern = "${" + key + "}";
if(template.indexOf(pattern) > -1) {
template = template.replace(pattern, obj[key]);
delete(obj[key]);
}
};
return template;
}
}
customElements.define('company-repeat', CompanyRepeater);
Now my question is: how can I make it work, regardless of what the parent is? I added a property elementto my repeater, but this does not allow me to declare more of the attribute, and it will not work inside the table.
, WebComponent.