UNPKG

2.27 kBPlain TextView Raw
1import * as React from "react";
2import {ReactElement} from "react";
3import {GlobalContext, GlobalContextContent} from "../react/global-context";
4
5export 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/* do not use this, without special problem */
10export abstract class StatefulBaseComponent<P, S extends object> extends React.Component<P, S> implements GlobalContext {
11 public readonly context: any;
12 private static _bindingMethods; // not used any where
13
14 public abstract state: S;
15
16 constructor(props: P, context) {
17 super(props, context);
18 // console.log('constructor of', this.constructor.name);
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
34export interface BaseComponentConstructor<P> extends React.ComponentClass<P>, React.ComponentLifecycle<P, {}> {
35 new (props?: P, context?: any): BaseComponent<P>;
36}
37
38export interface ReactComponentConstructor<P> extends React.ComponentClass<P>, React.ComponentLifecycle<P, any> {
39 new (props?: P, context?: any): React.Component<P, any>;
40}
41
42/** Redux base component */
43export abstract class BaseComponent<P> extends StatefulBaseComponent<P, {}> implements GlobalContext {
44 state: {};
45}
46
47export 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 // console.log('BindThis of', target.constructor.name);
64 // console.log(target.constructor['_bindingMethods']);
65
66 return descriptor;
67}