Fix bad constructor when adding attrs that are not part of the parent and fix tom-select on safari

This commit is contained in:
2024-10-16 14:59:02 +02:00
parent 677ff51ea5
commit 66dceefcf0
5 changed files with 26 additions and 21 deletions

View File

@ -4,7 +4,7 @@
* create a new web component
* create the desired type inside
* pass all attributes to the child component
* store is at as a widget inside the parent
* store is at as `node` inside the parent
*
* To use this, you must use the tag name twice, once for creating the class
* and the second time while calling super to pass it to the constructor
@ -12,20 +12,22 @@
export class InheritedComponent<
K extends keyof HTMLElementTagNameMap,
> extends HTMLElement {
widget: HTMLElementTagNameMap[K];
node: HTMLElementTagNameMap[K];
constructor(tagName: K) {
super();
this.widget = document.createElement(tagName);
this.node = document.createElement(tagName);
const attributes: Attr[] = []; // We need to make a copy to delete while iterating
for (const attr of this.attributes) {
attributes.push(attr);
if (attr.name in this.node) {
attributes.push(attr);
}
}
for (const attr of attributes) {
this.removeAttributeNode(attr);
this.widget.setAttributeNode(attr);
this.node.setAttributeNode(attr);
}
this.appendChild(this.widget);
this.appendChild(this.node);
}
}