Add key field to UElement (ADR-004)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user