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

This commit is contained in:
klmp200 committed 2024-10-17 18:15:55 +02:00
1 parent 677ff51ea5
commit 66dceefcf0
5 files changed
+26 -21

No files matched your search

+8 -6
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);
}
}