feat:complete the implement of JSX
This commit is contained in:
@@ -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 { }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user