add tsx support

This commit is contained in:
pengfei.zhou
2021-09-01 10:47:40 +08:00
committed by osborn
parent cde8d1726f
commit 52b2b87e1c
20 changed files with 6856 additions and 6762 deletions

View File

@@ -19,4 +19,5 @@ export * from './layoutconfig'
export * from './log'
export * from './types'
export * from './uniqueId'
export * from './flexbox'
export * from './flexbox'
export * from './jsx'

40
doric-js/src/util/jsx.ts Normal file
View File

@@ -0,0 +1,40 @@
import { Group, View } from "../ui/view";
import { ClassType } from "./types";
export const jsx = {
createElement: function <T extends View>(
constructor: ClassType<T>,
config: Partial<T> | null,
...children: View[]
): T {
const e = new constructor();
if (config) {
for (let key in config) {
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
}
if (children && children.length > 0) {
if (e instanceof Group) {
children.forEach((child) => e.addChild(child));
} else {
throw new Error(
`Can only add child to group view, do not support ${constructor.name}`
);
}
}
return e;
},
}
declare global {
namespace JSX {
interface IntrinsicElements { }
interface ElementClass extends View { }
interface ElementAttributesProperty {
props: {};
}
interface ElementChildrenAttribute {
children: View[];
}
}
}