jsx support use function as component

This commit is contained in:
pengfei.zhou
2022-07-04 14:13:41 +08:00
committed by osborn
parent 6cafcbc688
commit 35c6d03d14
18 changed files with 2934 additions and 4714 deletions

View File

@@ -1,7 +1,7 @@
import { Group, View } from "../ui/view";
import { ClassType } from "./types";
export declare namespace jsx {
function createElement<T extends View>(constructor: ClassType<T>, config: Partial<T> | null, ...children: any[]): any[] | T;
function createElement<T extends View>(constructor: ClassType<T>, config: Partial<T> | null, ...children: any[]): any;
class Fragment extends Group {
}
}

View File

@@ -3,26 +3,47 @@ import { layoutConfig } from "./layoutconfig";
export var jsx;
(function (jsx) {
function createElement(constructor, config, ...children) {
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 (!!constructor.isViewClass) {
const e = new constructor();
if (e instanceof Fragment) {
return children;
}
if (Reflect.has(e, "innerElement")) {
Reflect.set(e, "innerElement", children, e);
e.layoutConfig = layoutConfig().fit();
if (config) {
e.apply(config);
}
else {
throw new Error(`Do not support ${constructor.name} for ${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 ${constructor.name} for ${children}`);
}
}
return e;
}
else {
const f = constructor;
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;
}
return e;
}
jsx.createElement = createElement;
class Fragment extends Group {