fix: fixed uinode types

This commit is contained in:
Daniel Ledda
2022-04-03 00:17:49 +02:00
parent e77b89ebee
commit f31cc87d20

View File

@@ -4,13 +4,12 @@ export type UINodeOptions = {
}; };
type IRenderAttributes< type IRenderAttributes<T extends keyof HTMLElementTagNameMap> = Partial<{
T extends keyof HTMLElementTagNameMap, [K in keyof HTMLElementTagNameMap[T]]: HTMLElementTagNameMap[T][K] | Ref<HTMLElementTagNameMap[T][K]>
K extends keyof HTMLElementTagNameMap[T] }> & {
> = Partial<Record<K, HTMLElementTagNameMap[T][K]> & { classes?: string[],
classes: string[], saveTo?: Ref<HTMLElementTagNameMap[T] | null>,
saveTo: Ref<HTMLElementTagNameMap[T] | null>, };
}>;
export default abstract class UINode { export default abstract class UINode {
protected node: HTMLElement | null = null; protected node: HTMLElement | null = null;
@@ -50,10 +49,9 @@ export default abstract class UINode {
} }
export function h< export function h<
T extends keyof HTMLElementTagNameMap, T extends keyof HTMLElementTagNameMap>(
K extends keyof HTMLElementTagNameMap[T]>(
type: T, type: T,
attributes: IRenderAttributes<T, K>, attributes: IRenderAttributes<T>,
subNodes?: (Node | UINode | Ref<any>)[], subNodes?: (Node | UINode | Ref<any>)[],
): HTMLElementTagNameMap[T] { ): HTMLElementTagNameMap[T] {
const element = document.createElement(type); const element = document.createElement(type);
@@ -64,7 +62,13 @@ export function h<
} else if (key === "saveTo") { } else if (key === "saveTo") {
attributes.saveTo!.val = element; attributes.saveTo!.val = element;
} else { } else {
element[key as keyof HTMLElementTagNameMap[T]] = (attributes as any)[key]; const attribute = (attributes as any)[key];
if (attribute instanceof Ref) {
element[key as keyof HTMLElementTagNameMap[T]] = attribute.val;
attribute.watch((newVal) => element[key as keyof HTMLElementTagNameMap[T]] = newVal);
} else {
element[key as keyof HTMLElementTagNameMap[T]] = attribute;
}
} }
} }
} }