Add key field to UElement (ADR-004)

This commit is contained in:
2026-05-18 16:39:14 +00:00
parent c9c32a6aa6
commit 822ded6cf1
3 changed files with 61 additions and 2 deletions

View File

@@ -3,7 +3,8 @@ import type { UNode, UElement, URoot, UType, UComponent, UniversalProps } from "
let _idCounter = 0;
export function h(type: UType, props?: UniversalProps | null, ...children: UNode[]): UElement | URoot {
const resolvedProps: UniversalProps = props ? { ...props } : {};
const { key, ...restProps } = props ?? {};
const resolvedProps: UniversalProps = restProps;
const flatChildren = children.flat(Infinity as 1).filter((c: UNode) => c != null && c !== false) as UNode[];
if (type === "root") {
@@ -18,6 +19,7 @@ export function h(type: UType, props?: UniversalProps | null, ...children: UNode
type: type as string,
props: resolvedProps,
children: flatChildren,
...(key != null ? { key: key as string } : {}),
} as UElement;
}

View File

@@ -23,6 +23,7 @@ export const UJSX = Type.Module({
type: Type.String(),
props: Type.Ref("UniversalProps"),
children: Type.Array(Type.Ref("UNode")),
key: Type.Optional(Type.String()),
}),
URoot: Type.Object({
@@ -45,6 +46,7 @@ export type UElement = {
type: string;
props: UniversalProps;
children: UNode[];
key?: string;
};
export type URoot = {
type: "root";