feat:complete the implement of JSX

This commit is contained in:
pengfei.zhou
2021-09-03 13:42:25 +08:00
committed by osborn
parent 266d20782a
commit bec0d44af0
23 changed files with 322 additions and 150 deletions

View File

@@ -1,40 +1,31 @@
import { Group, View } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
import { loge } from "./log";
import { ClassType } from "./types";
export namespace jsx {
function addElement(group: Group, v: any) {
if (v instanceof Array) {
v.forEach(e => addElement(group, e))
} else if (v instanceof Fragment) {
v.children.forEach(e => addElement(group, e))
} else if (v instanceof View) {
group.addChild(v)
} else {
throw new Error(
`Can only use view as child`
);
}
}
export function createElement<T extends View>(
constructor: ClassType<T>,
config: Partial<T> | null,
...children: View[]
...children: any[]
) {
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 (e instanceof Group) {
children.forEach((child) => {
addElement(e, child)
});
if (children.length === 1) {
children = children[0]
}
if (Reflect.has(e, "innerElement")) {
Reflect.set(e, "innerElement", children, e)
} else {
throw new Error(
`Can only add child to group view, do not support ${constructor.name}`
`Do not support ${constructor.name} for ${children}`
);
}
}
@@ -45,13 +36,14 @@ export namespace jsx {
declare global {
namespace JSX {
interface IntrinsicElements { }
interface Element extends View { }
interface ElementClass extends View { }
interface ElementAttributesProperty {
props: {};
}
interface ElementChildrenAttribute {
children: View[];
innerElement: any;
}
interface IntrinsicElements { }
}
}