From f31cc87d20b9a45c20af8b925162385333a75b81 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Sun, 3 Apr 2022 00:17:49 +0200 Subject: [PATCH] fix: fixed uinode types --- src/ui/UINode.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/ui/UINode.ts b/src/ui/UINode.ts index 0e42d48..2f35b8a 100644 --- a/src/ui/UINode.ts +++ b/src/ui/UINode.ts @@ -4,13 +4,12 @@ export type UINodeOptions = { }; -type IRenderAttributes< - T extends keyof HTMLElementTagNameMap, - K extends keyof HTMLElementTagNameMap[T] - > = Partial & { - classes: string[], - saveTo: Ref, -}>; +type IRenderAttributes = Partial<{ + [K in keyof HTMLElementTagNameMap[T]]: HTMLElementTagNameMap[T][K] | Ref +}> & { + classes?: string[], + saveTo?: Ref, +}; export default abstract class UINode { protected node: HTMLElement | null = null; @@ -50,10 +49,9 @@ export default abstract class UINode { } export function h< - T extends keyof HTMLElementTagNameMap, - K extends keyof HTMLElementTagNameMap[T]>( + T extends keyof HTMLElementTagNameMap>( type: T, - attributes: IRenderAttributes, + attributes: IRenderAttributes, subNodes?: (Node | UINode | Ref)[], ): HTMLElementTagNameMap[T] { const element = document.createElement(type); @@ -64,7 +62,13 @@ export function h< } else if (key === "saveTo") { attributes.saveTo!.val = element; } 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; + } } } }