This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-07-04 14:21:32 +08:00

70 lines
2.1 KiB
TypeScript

import { Group, View } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
import { ClassType } from "./types";
export namespace jsx {
export function createElement<T extends View>(
constructor: ClassType<T>,
config: Partial<T> | null,
...children: any[]
) {
if (!!(constructor as any).isViewClass) {
const e = new constructor();
if (e instanceof Fragment) {
return children
}
e.layoutConfig = layoutConfig().fit()
if (config) {
e.apply(config)
}
if (children && children.length > 0) {
if (children.length === 1) {
children = children[0]
}
if (Reflect.has(e, "innerElement")) {
Reflect.set(e, "innerElement", children, e)
} else {
throw new Error(
`Do not support ${constructor.name} for ${children}`
);
}
}
return e;
} else {
const f = constructor as Function
const e = Reflect.apply(f, undefined, [config])
if (e instanceof Fragment) {
return children
}
if (children && children.length > 0) {
if (children.length === 1) {
children = children[0]
}
if (Reflect.has(e, "innerElement")) {
Reflect.set(e, "innerElement", children, e)
} else {
throw new Error(
`Do not support add child for ${e.viewType()}`
);
}
}
return e
}
}
export class Fragment extends Group { }
}
declare global {
namespace JSX {
interface Element extends View { }
interface ElementClass extends View { }
interface ElementAttributesProperty {
props: {};
}
interface ElementChildrenAttribute {
innerElement: any;
}
interface IntrinsicElements { }
}
}