1 | import * as React from "react";
|
2 | import {ReactElement} from "react";
|
3 | import {GlobalContext, GlobalContextContent} from "../react/global-context";
|
4 |
|
5 | export interface StatefulBaseComponentConstructor<P, S extends object> extends React.ComponentClass<P>, React.ComponentLifecycle<P, S> {
|
6 | new (props?: P, context?: any): StatefulBaseComponent<P, S>;
|
7 | }
|
8 |
|
9 |
|
10 | export abstract class StatefulBaseComponent<P, S extends object> extends React.Component<P, S> implements GlobalContext {
|
11 | public readonly context: any;
|
12 | private static _bindingMethods;
|
13 |
|
14 | public abstract state: S;
|
15 |
|
16 | constructor(props: P, context) {
|
17 | super(props, context);
|
18 |
|
19 | if (this.constructor.hasOwnProperty('_bindingMethods')) {
|
20 | this.constructor['_bindingMethods'].forEach((name) => {
|
21 | this[name] = this[name].bind(this);
|
22 | });
|
23 | }
|
24 |
|
25 | this.afterConstruct(props, context);
|
26 | }
|
27 |
|
28 | afterConstruct<T>(props: P, context: T) {
|
29 | }
|
30 |
|
31 | abstract render(): ReactElement<any>|null;
|
32 | }
|
33 |
|
34 | export interface BaseComponentConstructor<P> extends React.ComponentClass<P>, React.ComponentLifecycle<P, {}> {
|
35 | new (props?: P, context?: any): BaseComponent<P>;
|
36 | }
|
37 |
|
38 | export interface ReactComponentConstructor<P> extends React.ComponentClass<P>, React.ComponentLifecycle<P, any> {
|
39 | new (props?: P, context?: any): React.Component<P, any>;
|
40 | }
|
41 |
|
42 |
|
43 | export abstract class BaseComponent<P> extends StatefulBaseComponent<P, {}> implements GlobalContext {
|
44 | state: {};
|
45 | }
|
46 |
|
47 | export function BindThis<T>(target: StatefulBaseComponent<any, any>,
|
48 | propertyKey: string|symbol,
|
49 | descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> {
|
50 | if (descriptor === undefined) {
|
51 | throw new TypeError('@BindThis only allow to decorate method.');
|
52 | }
|
53 |
|
54 | if (!target.constructor.hasOwnProperty('_bindingMethods')) {
|
55 | Object.defineProperty(target.constructor, '_bindingMethods', {
|
56 | configurable: true,
|
57 | enumerable: false,
|
58 | value: [],
|
59 | });
|
60 | }
|
61 | target.constructor['_bindingMethods'].push(propertyKey);
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | return descriptor;
|
67 | }
|