tsx:add ref and parent support

This commit is contained in:
pengfei.zhou
2021-09-02 11:39:51 +08:00
committed by osborn
parent c1f39de8e3
commit aeff0a06dc
13 changed files with 265 additions and 62 deletions

View File

@@ -14,6 +14,12 @@ export declare type NativeViewModel = {
[index: string]: Model;
};
};
export declare class Ref<T extends View> {
private view?;
set current(v: T);
get current(): T;
}
export declare function makeRef<T extends View>(): Ref<T>;
export declare abstract class View implements Modeling {
width: number;
height: number;
@@ -132,6 +138,8 @@ export declare abstract class View implements Modeling {
*/
flexConfig?: FlexConfig;
set props(props: Partial<this>);
set parent(v: Group);
set ref(ref: Ref<this>);
doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
clearAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
cancelAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;

View File

@@ -23,6 +23,20 @@ export function ViewComponent(constructor) {
const name = Reflect.getMetadata(PROP_KEY_VIEW_TYPE, constructor) || Object.getPrototypeOf(constructor).name;
Reflect.defineMetadata(PROP_KEY_VIEW_TYPE, name, constructor);
}
export class Ref {
set current(v) {
this.view = v;
}
get current() {
if (!!!this.view) {
throw new Error("Ref is empty");
}
return this.view;
}
}
export function makeRef() {
return new Ref;
}
export class View {
constructor() {
this.width = 0;
@@ -203,6 +217,12 @@ export class View {
set props(props) {
this.apply(props);
}
set parent(v) {
this.in(v);
}
set ref(ref) {
ref.current = this;
}
doAnimation(context, animation) {
return this.nativeChannel(context, "doAnimation")(animation.toModel()).then((args) => {
for (let key in args) {

View File

@@ -1,11 +1,11 @@
import { Group } from "../ui/view";
import { layoutConfig } from "./layoutconfig";
export const jsx = {
createElement: function (constructor, config, ...children) {
const e = new constructor();
e.layoutConfig = layoutConfig().fit();
if (config) {
for (let key in config) {
Reflect.set(e, key, Reflect.get(config, key, config), e);
}
e.apply(config);
}
if (children && children.length > 0) {
if (e instanceof Group) {