tsx:add fragment support

This commit is contained in:
pengfei.zhou
2021-09-02 12:03:19 +08:00
committed by osborn
parent aeff0a06dc
commit 03633a7bea
11 changed files with 170 additions and 155 deletions

View File

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

View File

@@ -1,7 +1,8 @@
import { Group } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
export const jsx = {
createElement: function (constructor, config, ...children) {
export var jsx;
(function (jsx) {
function createElement(constructor, config, ...children) {
const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) {
@@ -9,12 +10,23 @@ export const jsx = {
}
if (children && children.length > 0) {
if (e instanceof Group) {
children.forEach((child) => e.addChild(child));
children.forEach((child) => {
if (child instanceof Fragment) {
child.children.forEach(c => e.addChild(c));
}
else {
e.addChild(child);
}
});
}
else {
throw new Error(`Can only add child to group view, do not support ${constructor.name}`);
}
}
return e;
},
};
}
jsx.createElement = createElement;
class Fragment extends Group {
}
jsx.Fragment = Fragment;
})(jsx || (jsx = {}));