@carbonorm/carbonreact 4.0.10 → 4.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@carbonorm/carbonreact",
3
3
  "license": "MIT",
4
- "version": "4.0.10",
4
+ "version": "4.0.11",
5
5
  "browser": "dist/index.umd.js",
6
6
  "module": "dist/index.esm.js",
7
7
  "main": "dist/index.cjs.js",
@@ -49,11 +49,13 @@ export function isJsonString(str: string) {
49
49
 
50
50
  }
51
51
 
52
- abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<{
52
+ export interface iCarbonReactProps<P = {}, S extends iCarbonReactState = iCarbonReactState> {
53
53
  children?: ReactNode | ReactNode[],
54
54
  instanceId?: string,
55
- websocket?: Omit<iCarbonWebSocketProps<P,S>, "instance"> | false
56
- } & P, S> {
55
+ websocket?: Omit<iCarbonWebSocketProps<P, S>, "instance"> | false
56
+ }
57
+
58
+ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactState> extends Component<iCarbonReactProps<P, S> & P, S> {
57
59
 
58
60
  private static persistentStateMap = new Map<string, { [key: string]: any; }>();
59
61
 
@@ -68,9 +70,11 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
68
70
  static getInstance<T extends CarbonReact<any, any>>(): T {
69
71
  return this._instance as T;
70
72
  }
73
+
71
74
  static get instance() {
72
75
  return this.getInstance();
73
76
  }
77
+
74
78
  static set instance(instance: CarbonReact<any, any>) {
75
79
  this._instance = instance;
76
80
  }
@@ -95,14 +99,20 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
95
99
  static whyDidYouRender = true;
96
100
 
97
101
 
98
- protected constructor(props: {
99
- children?: ReactNode | ReactNode[];
100
- shouldStatePersist?: boolean | undefined;
101
- websocket?: boolean | iCarbonWebSocketProps<P,S> | undefined;
102
- } & P) {
102
+ protected constructor(props) {
103
103
 
104
104
  super(props);
105
105
 
106
+ if (!props.instanceId && (new.target as typeof CarbonReact)._instance) {
107
+
108
+ // todo - instanceId being unique should solve this, but.... how
109
+ // This is a singleton pattern, we can only have one instance of this class
110
+ throw new Error(`${new.target.name} instance already exists! CarbonReact extended classes can only be referenced once in DOM`);
111
+
112
+ }
113
+
114
+ (new.target as typeof CarbonReact).instance = (this);
115
+
106
116
  this.target = new.target;
107
117
 
108
118
  console.log('CarbonORM TSX CONSTRUCTOR');
@@ -191,8 +201,8 @@ abstract class CarbonReact<P = {}, S extends iCarbonReactState = iCarbonReactSta
191
201
  return <>
192
202
  <GlobalHistory/>
193
203
  {this.props.websocket &&
194
- <CarbonWebSocket<P,S> {...(false !== this.props.websocket ? this.props.websocket : {})}
195
- instance={this}/>}
204
+ <CarbonWebSocket<P, S> {...(false !== this.props.websocket ? this.props.websocket : {})}
205
+ instance={this}/>}
196
206
  <Context value={this.state}>
197
207
  {this.props.children}
198
208
  </Context>
@@ -18,10 +18,10 @@ export interface iAlertButtonOptions {
18
18
  color: "default" | "primary" | "secondary" | "inherit" | "danger" | "info" | "success" | "warning" | undefined,
19
19
  }
20
20
 
21
- export interface iAlert<P,S extends iCarbonReactState> {
21
+ export interface iAlert<P, S extends iCarbonReactState> {
22
22
  title: string,
23
23
  text: string,
24
- instance: CarbonReact<P,S>,
24
+ instance: CarbonReact<P, S>,
25
25
  component?: ReactNode,
26
26
  icon?: "warning" | "error" | "success" | "info" | "question" | null,
27
27
  buttons?: (iAlertButtonOptions)[] | undefined, //['No thanks!', 'Yes, Delete it'],
@@ -41,13 +41,11 @@ export function addAlert<P, S extends iCarbonReactState>(props: iAlert<P, S>) {
41
41
  }));
42
42
  }
43
43
 
44
- export default function Alert<P,S extends iCarbonReactState>({
45
- instance
46
- }: { instance: CarbonReact<P,S> }) {
44
+ export default function Alert<P, S extends iCarbonReactState>({instance}: { instance: CarbonReact<P, S> }) {
47
45
 
48
46
  const {alertsWaiting, backendThrowable} = instance.state
49
47
 
50
- let alert: iAlert<P,S> | undefined = undefined;
48
+ let alert: iAlert<P, S> | undefined = undefined;
51
49
 
52
50
  const alertWaiting = alertsWaiting.length + backendThrowable.length
53
51