Add confirm popup component

This commit is contained in:
2022-03-23 17:44:37 +08:00
parent 8b44ae953e
commit 89ae499d4b
5 changed files with 125 additions and 23 deletions
+31 -13
View File
@@ -2,12 +2,16 @@ import { ReactNode, createElement } from "react";
import { Emitter } from "@Model/Emitter";
import { Localization } from "@Component/Localization/Localization";
type IPopupConstructor = new (controller: PopupController, id: string) => Popup;
/**
* 弹窗类型
*/
class Popup {
class Popup<P extends any = any> {
public props: P;
public constructor(props: P) {
this.props = props;
}
public zIndex() {
return this.index * 2 + this.controller.zIndex;
@@ -17,9 +21,15 @@ class Popup {
public height: number = 200;
public top: number = 0;
public top: number = NaN;
public left: number = 0;
public left: number = NaN;
public lastMouseTop: number = 0;
public lastMouseLeft: number = 0;
public isOnMouseDown: boolean = false;
/**
* 是否关闭
@@ -39,12 +49,12 @@ class Popup {
/**
* 唯一标识符
*/
public id: string;
public id: string = "";
/**
* 控制器
*/
public controller: PopupController;
public controller: PopupController = undefined as any;
/**
* 渲染层级
@@ -67,7 +77,7 @@ class Popup {
* 渲染函数
*/
public onRender(p: Popup): ReactNode {
return null;
return undefined;
}
/**
@@ -81,15 +91,15 @@ class Popup {
* 渲染节点
*/
public render(): ReactNode {
this.reactNode = this.onRender(this);
this.reactNode = this.onRender(this) ?? this.reactNode;
return this.reactNode;
};
public close() {
public close(): Popup | undefined {
return this.controller.closePopup(this);
}
public constructor(controller: PopupController, id: string) {
public init(controller: PopupController, id: string) {
this.controller = controller;
this.id = id;
}
@@ -134,8 +144,16 @@ class PopupController extends Emitter<IPopupControllerEvent> {
/**
* 实例化并开启一个弹窗
*/
public showPopup<P extends IPopupConstructor>(popup?: P): Popup {
let newPopup = new (popup ?? Popup)(this, `P-${this.idIndex ++}`);
public showPopup<P extends any, T extends Popup<P>>(
popup?: (new () => T) | Popup<P>, props?: P
): Popup<P> {
let newPopup: Popup;
if (popup instanceof Popup) {
newPopup = popup;
} else {
newPopup = new (popup ?? Popup)(props);
}
newPopup.init(this, `P-${this.idIndex ++}`);
this.popups.push(newPopup);
this.sortPopup();
return newPopup;